简体   繁体   English

如何使用 HttpClient 调用 WCF 服务

[英]How do I use HttpClient to call a WCF service

I'm using the ServiceModel class library.我正在使用 ServiceModel class 库。
In.NET Core3.1, an error was reported that I could not load the ServiceModel class library.在.NET Core3.1中,报错无法加载ServiceModel class库。

I want to implement a dynamic library that calls the WCF service dynamically, without adding a reference in the VS.我想实现一个动态调用WCF服务的动态库,而无需在VS中添加引用。

Since the user of the class library packaged is using Net Core3.1.由于打包的 class 库的用户使用的是 Net Core3.1。
I would like to ask if it is possible to make dynamic calls to the WCF service.我想问是否可以对 WCF 服务进行动态调用。
HttpClient would be the best option. 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("创建服务工厂出现异常.");
    }
}

Use SOAP UI to get the SOAP requests from the WSDL.使用 SOAP UI 从 WSDL 获取 SOAP 请求。 Then modify at runtime these XML requests and POST the message to the.svc with the application/soap+xml content type.然后在运行时修改这些 XML 请求并使用 application/soap+xml 内容类型将消息 POST 到.svc。 That's how it is usually done from languages where there is no native SOAP wrapper or proxy generator.这通常是从没有本地 SOAP 包装器或代理生成器的语言中完成的。

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

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