Access SOAP webservice with ServiceStack

2019-02-18 03:43发布

问题:

I'm creating my client/server application intercommunication with ServiceStack, and is working great, but I need also to access an external SOAP web service.

I tried to use the Soap12ServiceClient to access it, but I couldn't find any example, and then I went the add service reference WCF way that actually worked, but creating a ton of code.

Is it possible to use Soap12ServiceClient in the same easy way I use JsonServiceClient to send a message/request and receive the message/response? If so, can you help or point me to a sample?

回答1:

I'm not sure where you're stuck as all of ServiceStack's C# Service Clients implement the same IServiceClient so they can be used in the same way. Here is an example of all of ServiceStack's built-in C# Service Clients calling the same Hello World service:

[TestFixture]
public class HelloWorldServiceClientTests
{
    public static IEnumerable ServiceClients
    {
        get
        {
            return new IServiceClient[] {
                new JsonServiceClient(Config.ServiceStackBaseUri),
                new JsvServiceClient(Config.ServiceStackBaseUri),
                new XmlServiceClient(Config.ServiceStackBaseUri),
                new Soap11ServiceClient(Config.ServiceStackBaseUri),
                new Soap12ServiceClient(Config.ServiceStackBaseUri)
            };
        }
    }

    [Test, TestCaseSource("ServiceClients")]
    public void HelloWorld_with_Sync_ServiceClients(IServiceClient client)
    {
        var response = client.Send<HelloResponse>(new Hello { Name = "World!" });

        Assert.That(response.Result, Is.EqualTo("Hello, World!"));
    }
}

Although SOAP works similar to any other C# client, it's un-common to use it in this way because if you're able to use a generic C# SOAP service client you're also likely able to use any of the other service clients which are all faster, more resilient and more versionable than SOAP - which has effectively has no redeeming quality over the other formats other than its ability to generate client proxies which you said you don't want to do anyway.

If you're undecided which endpoint or format you should use I recommend reading my Interview on InfoQ which discusses the disadvantages of SOAP and the benefits of using the other formats.