如何使用 HttpClient 调用 WCF 服务

时间:2021-04-27 07:45:38

标签: c# wcf

我正在使用 ServiceModel 类库。
在.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("创建服务工厂出现异常.");
    }
}

1 个答案:

答案 0 :(得分:0)

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

相关问题