简体   繁体   English

我可以使用 c# rest 库来调用 Web 服务吗?

[英]Can I use the c# rest library to call a web service?

Can I use the C# REST library to make these web service calls instead of discovering the web service like we do below?我可以使用 C# REST 库来进行这些 Web 服务调用,而不是像下面那样发现 Web 服务吗?

ex of a web service we're calling我们正在调用的 Web 服务的前例

https://trustonline.delawarecpf.com/tows/webservicefk.svc

The problem is, we have a .cs file for each web service with pretty much all the same code to call up the web service and fetch/add/edit data.问题是,我们为每个 Web 服务都有一个 .cs 文件,其中包含几乎所有相同的代码来调用 Web 服务和获取/添加/编辑数据。 The only differences are the web services each file references are different.唯一的区别是每个文件引用的 Web 服务都不同。

Ex.前任。 in each file we access a user like this在每个文件中,我们像这样访问一个用户

var user = new User();

where User is用户在哪里

ServiceReferenceFK.User() // from the FK service reference

or或者

ServiceReferenceAB.User() // from the AB service reference

But I'd rather use the REST library to fetch/edit/add/delete data so to eliminate all the redundant files.但我宁愿使用 REST 库来获取/编辑/添加/删除数据,以便消除所有冗余文件。 And move to a single file for all services.并移动到所有服务的单个文件。

Currently we do this in solution explorer under project目前我们在项目下的解决方案资源管理器中执行此操作

在此处输入图片说明

then we add a service reference like this below and hit discover.然后我们添加一个像下面这样的服务引用并点击发现。 VS finds the url and gets us all the code to use. VS 找到 url 并为我们提供所有要使用的代码。

在此处输入图片说明

You could use an abstract base class.您可以使用抽象基类。 To make it work your abstract class needs to be [serializable] or [Datacontract] or it won't be able to expose the class to the client, eg为了使其工作,您的抽象类需要[serializable][Datacontract]否则它将无法将该类公开给客户端,例如

[DataContract]
public abstract class MyBase
{
    [DataMember]
    public abstract int DoAdd { get; set; }

    public void MyOtherFunc()
    {
        Console.WriteLine("Done");
    }

}
[DataContract]
public class MyWCF : MyBase
{
    int a;
    [DataMember]
    public override int DoAdd
    {
        get { return a; }
        set { a = value; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM