Calling a SOAP service in .net Core

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

    Ok this answer is for those who are trying to connect to a WCF service from a .net Core project.

    Here is the solution to my problem, using the new .net Core WCF syntax/library.

    BasicHttpBinding basicHttpBinding = null;
    EndpointAddress endpointAddress = null;
    ChannelFactory factory = null;
    IAService serviceProxy = null;
    
    try
    {
        basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
        basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
        endpointAddress = new EndpointAddress(new Uri("https://someurl.com/ws/TheEndpoint.pub.ws:AService"));
        factory = new ChannelFactory(basicHttpBinding, endpointAddress);
    
        factory.Credentials.UserName.UserName = "usrn";
        factory.Credentials.UserName.Password = "passw";
        serviceProxy = factory.CreateChannel();
    
        using (var scope = new OperationContextScope((IContextChannel)serviceProxy))
        {
            var result = await serviceProxy.getSomethingAsync("id").ConfigureAwait(false);
        }
    
        factory.Close();
        ((ICommunicationObject)serviceProxy).Close();
    }
    catch (MessageSecurityException ex)
    {
         throw;
    }
    catch (Exception ex)
    {
        throw;
    }
    finally
    {
        // *** ENSURE CLEANUP (this code is at the WCF GitHub page *** \\
        CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
    }
    

    UPDATE

    I got the following exception using the code above

    This OperationContextScope is being disposed out of order.

    Which seems to be something that is broken (or needs addressing) by the WCF team.

    So I had to do the following to make it work (based on this GitHub issue)

    basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
    basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
    
    factory = new ChannelFactory(basicHttpBinding, new EndpointAddress(new Uri("https://someurl.com/ws/TheEndpoint.pub.ws:AService")));
    factory.Credentials.UserName.UserName = "usern";
    factory.Credentials.UserName.Password = "passw";
    serviceProxy = factory.CreateChannel();
    ((ICommunicationObject)serviceProxy).Open();
    var opContext = new OperationContext((IClientChannel)serviceProxy);
    var prevOpContext = OperationContext.Current; // Optional if there's no way this might already be set
    OperationContext.Current = opContext;
    
    try
    {
        var result = await serviceProxy.getSomethingAsync("id").ConfigureAwait(false);
    
        // cleanup
        factory.Close();
        ((ICommunicationObject)serviceProxy).Close();
    }
    finally
    {
      // *** ENSURE CLEANUP *** \\
      CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
      OperationContext.Current = prevOpContext; // Or set to null if you didn't capture the previous context
    }
    

    But your requirements will probably be different. So here are the resources you might need to help you connecting to your WCF service are here:

    • WCF .net core at GitHub
    • BasicHttpBinding Tests
    • ClientCredentialType Tests

    The tests helped me a lot but they where somewhat hard to find (I had help, thank you Zhenlan for answering my wcf github issue)

提交回复
热议问题