Calling a SOAP service in .net Core

前端 未结 7 1919
失恋的感觉
失恋的感觉 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:59

    I used the .net core 3 svutil to generate the wrapper classes from an old school asmx SOAP / WSDL. See - https://docs.microsoft.com/en-us/dotnet/core/additional-tools/dotnet-svcutil-guide?tabs=dotnetsvcutil2x

    I then wrapped the remote and used the code below to set the authentication. I believe this works for Kerberos and will fall back to NTLM (depending on the headers returned from the server).

    public class DocumentManagerWrapper {

    public DocumentManagerWrapper(string serviceUrl,
        String username,
        String password)
    {
        _serviceUrl = serviceUrl;
        _username = username;
        _password = password;
    }
    
    private String _serviceUrl;
    private String _username;
    private String _password;
    

    private DocumentManagerSoap GetSoapConnection() { BasicHttpSecurityMode mode = _serviceUrl.StartsWith("https") ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.TransportCredentialOnly; BasicHttpBinding binding = new BasicHttpBinding(mode);

        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
    
        EndpointAddress address = new EndpointAddress(_serviceUrl);
    
        ChannelFactory channel = new ChannelFactory(binding,
            address);
        channel.Credentials.Windows.ClientCredential.UserName = _username;
        channel.Credentials.Windows.ClientCredential.Password = _password;
        DocumentManagerSoap soap = channel.CreateChannel();
        
        return soap;
     }
    

    Note - DocumentManagerSoapChannel and DocumentManagerSoap classes are generated by svcutil.

提交回复
热议问题