如何使用HttpClient来调用WCF服务



我正在使用ServiceModel类库
In。NET Core3.1,报告了一个错误,即我无法加载ServiceModel类库。

我想实现一个动态库,该库动态调用WCF服务,而不在VS中添加引用。

由于打包的类库的用户使用的是Net Core3.1。
我想问一下是否可以对WCF服务进行动态调用
HttpClient将是最好的选择。

public static T CreateServiceByUrl<T>(string url)
{
return CreateServiceByUrl<T>(url, "basicHttpBinding");///Unable to load the class library
}
public static T CreateServiceByUrl<T>(string url, string bing)
{
try
{
if (string.IsNullOrEmpty(url)) throw new NotSupportedException("This url is not Null or Empty!");
EndpointAddress address = new EndpointAddress(url);
Binding binding = CreateBinding(bing);
ChannelFactory<T> factory = new ChannelFactory<T>(binding, address);
return factory.CreateChannel();
}
catch (Exception ex)
{
throw new Exception("创建服务工厂出现异常.");
}
}

使用SOAP UI从WSDL获取SOAP请求。然后在运行时修改这些XML请求,并使用application/soap+XML内容类型将消息POST到.svc。在没有本地SOAP包装器或代理生成器的语言中,通常就是这样做的。

最新更新