简体   繁体   English

.Net core 3.1版本中如何编程消费WCF服务

[英]How to consume WCF service programmatic in .Net core 3.1 version

I have a WCF service, which was developed using the .Net framework 4.7 .我有一个 WCF 服务,它是使用.Net framework 4.7开发的。

Now I have to validate & Parse the WCF Service programmatically using .Net Core3.1 Web Application without adding the WCF Service as a Service Reference/Add Connected Service options in Visual Studio Solution Explorer现在,我必须使用.Net Core3.1 Web 应用程序以编程方式验证和解析 WCF 服务,而无需Visual Studio 解决方案资源管理器中添加 WCF 服务作为服务参考/添加连接服务选项

We can also use the channel factory to call WCF services, this method does not need to add a service reference,here is a demo:我们也可以使用通道工厂来调用WCF服务,这个方法不需要添加服务引用,这里有一个demo:

            BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
            var address = new EndpointAddress("http://localhost:801/Service1.svc/Service");
           
            var factory = new ChannelFactory<IService1>(basicHttpBinding, address);
            IService1 channel = factory.CreateChannel();
            channel.GetData(1);
            Console.WriteLine(channel.GetData(1));
            Console.ReadLine();

On the client side, we need to have a ServiceContract:在客户端,我们需要有一个 ServiceContract:

[ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }

This ServiceContract is the same as the ServiceContract on the server side.这个ServiceContract和服务端的ServiceContract是一样的。

Because you are calling WCF in core, you need to add the following two packages:因为在core中调用的是WCF,所以需要添加如下两个包:

在此处输入图像描述

If you use NetTcpBinding, you need to add the following package:如果使用NetTcpBinding,需要添加如下package:

在此处输入图像描述

In addition, there are some limitations when calling WCF in core.另外在core中调用WCF也有一些限制。 You can refer to this link:你可以参考这个链接:

https://github.com/do.net/wcf/blob/master/release-notes/SupportedFeatures-v2.1.0.md https://github.com/do.net/wcf/blob/master/release-notes/SupportedFeatures-v2.1.0.md

Feel free to let me know if the problem persists.如果问题仍然存在,请随时告诉我。

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

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