![](/img/trans.png)
[英]How to add a WCF service reference in a .NET Core 3.1 application?
[英]How to consume WCF service programmatic in .Net core 3.1 version
我有一个 WCF 服务,它是使用.Net framework 4.7开发的。
现在,我必须使用.Net Core3.1 Web 应用程序以编程方式验证和解析 WCF 服务,而无需在Visual Studio 解决方案资源管理器中添加 WCF 服务作为服务参考/添加连接服务选项
我们也可以使用通道工厂来调用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();
在客户端,我们需要有一个 ServiceContract:
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
这个ServiceContract和服务端的ServiceContract是一样的。
因为在core中调用的是WCF,所以需要添加如下两个包:
如果使用NetTcpBinding,需要添加如下package:
另外在core中调用WCF也有一些限制。 你可以参考这个链接:
https://github.com/do.net/wcf/blob/master/release-notes/SupportedFeatures-v2.1.0.md
如果问题仍然存在,请随时告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.