use*_*193 11 c# wcf .net-core .net-core-3.1 asp.net-core-3.1
我有一个 WCF 服务,它是使用.Net Framework 4.7开发的。
现在,我必须使用.Net Core3.1 Web 应用程序以编程方式验证和解析 WCF 服务 ,而无需在Visual Studio 解决方案资源管理器中添加 WCF 服务作为服务引用/添加连接服务选项
我们还可以使用通道工厂来调用WCF服务,这种方法不需要添加服务引用,下面是一个演示:
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();
Run Code Online (Sandbox Code Playgroud)
在客户端,我们需要有一个ServiceContract:
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
Run Code Online (Sandbox Code Playgroud)
这个ServiceContract和服务器端的ServiceContract是一样的。
因为是在core中调用WCF,所以需要添加以下两个包:
如果使用NetTcpBinding,则需要添加以下包:
另外,在core中调用WCF时也有一些限制。您可以参考这个链接:
https://github.com/dotnet/wcf/blob/master/release-notes/SupportedFeatures-v2.1.0.md
如果问题仍然存在,请随时告诉我。