Calling a SOAP service in .net Core

前端 未结 7 1930
失恋的感觉
失恋的感觉 2020-11-28 07:12

I´m porting a .net 4.6.2 code to a .net Core project, that calls a SOAP service. In the new code I´m using C# (because of some config reasons I just can´t r

7条回答
  •  青春惊慌失措
    2020-11-28 07:58

    So I had to do this and used the WCF Web Service Reference Provider Tool.

    The apparent need, according to responses like those here, for all the roundabout business with Bindings and Factories and Proxies seemed strange, considering that this all appeared to be part of the imported class.

    Not being able to find a straightforward official "HowTo", I will post my findings as to the simplest setup I was able to cobble together to fit my requirements with Digest authentication:

        ServiceName_PortClient client = new ServiceName_PortClient();
        //GetBindingForEndpoint returns a BasicHttpBinding
        var httpBinding = client.Endpoint.Binding as BasicHttpBinding;
        httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Digest;
        client.ClientCredentials.HttpDigest.ClientCredential = new NetworkCredential("Username", "Password", "Digest");
        var result = await client.GetResultAsync();
    

    Now, if you don't need to do any authentication simply doing:

        ServiceName_PortClient client = new ServiceName_PortClient();
        var result = await client.GetResultAsync();
    

    Should be sufficient.

    The ServiceName_PortClient class was generated as such by the import tool, where ServiceName was the name of the service I was importing.

    Of course it seems to be more in the spirit of the imported code to place the configuration in a partial ServiceName_PortClient class along the lines of:

        public partial class ServiceName_PortClient
        {
            static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials)
            {
                var httpBinding = serviceEndpoint.Binding as BasicHttpBinding;
                httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Digest;
                clientCredentials.HttpDigest.ClientCredential = new NetworkCredential("Username", "Password", "Realm");
            }
        }
    

提交回复
热议问题