简体   繁体   中英

How do I use HttpClient to call a WCF service

I'm using the ServiceModel class library.
In.NET Core3.1, an error was reported that I could not load the ServiceModel class library.

I want to implement a dynamic library that calls the WCF service dynamically, without adding a reference in the VS.

Since the user of the class library packaged is using Net Core3.1.
I would like to ask if it is possible to make dynamic calls to the WCF service.
HttpClient would be the best option.

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. Then modify at runtime these XML requests and POST the message to the.svc with the application/soap+xml content type. That's how it is usually done from languages where there is no native SOAP wrapper or proxy generator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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