Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consuming a web service using a different interface

I have a wsdl file that is constructed in an extremely unhelpful way. It is huge,in some cases several megabytes in size, and when I use the various Visual Studio tools to generate a wrapper from it, the resulting codebase is so large that it tends to crash Visual Studio on weaker machines. The compilation time is ludicrous, and the resulting class uses properties where a more dynamic mode of access is absolutely necessary (i.e. some sort of indexer). There is no option of any changes on the server side.

The wsdl files are far larger than could be processed by hand, and there is an arbitrary number of them. The solution I've employed so far, is to use reflection or late binding with the resulting, auto-generated class. However, since I'm dealing here with a wrapper that wraps what's basically a client for SOAP messages it would make sense if there is another way.

Essentially, I want to create a wrapper that exposes a more dynamic interface, especially where fields are concerned. The task isn't entirely straightforward, so I'm looking for suggestions regarding what to do, and various classes, customizable code generators, WSDL explorers/parsers, etc, that will make the task less time-consuming. Should I construct my own SOAP client? What would I base it on? What .NET features can help me with this task?

like image 206
GregRos Avatar asked Jun 19 '12 13:06

GregRos


People also ask

What is meant by consuming a web service?

"Consume" means that the Web service successfully fulfills the web client's request. Context of Use: An end user performs a task on a web client that requires consumption of a Web service.

What is a web services interface?

Web service interface definition - describes the generic web service, including the port type, messages, parts of the messages, and bindings. Web service implementation - definition of the service and ports.

What do you need to consume a web service?

A web service can be consumed by a client application. Different types of client applications can comsume a web service. In today's software environment, almost every application need a web service to enhance its functionality.

Which of the following acts as a interface between web service application and is a part of UDDI?

WSDL is a part of UDDI. It acts as a interface between web service applications.


2 Answers

You could hand craft an interface that supports a subset of the methods available on the WebService and do away with the need to generate a service reference.

You would have to create the right soap signature for the methods including dto's and namespaces. The downside of this is that you would be forced to manage any changes to the service manually.

Here is a simple example showing a proxy client created with the ISubsetInterface communication with a service exposing the IServiceInterface. To achieve this the Name property has to match the name of the IServiceInterface contract which in this case defaults to "IServiceInterface" but your implementation might require manipulation of the Namespace and Actions. The simplest way to know what you need to manipulate is to look at the generated wsdl.

[TestFixture]
public class When_using_a_subset_of_a_WCF_interface
{
    [Test]
    public void Should_call_interesting_method()
    {
        var serviceHost = new ServiceHost(typeof(Service));

        serviceHost.AddServiceEndpoint( typeof(IServiceInterface), new BasicHttpBinding(), "http://localhost:8081/Service" );
        serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;

        serviceHost.Open();

        using( var channelFactory = new ChannelFactory<ISubsetInterface>( new BasicHttpBinding(), "http://localhost:8081/Service") )
        {
            var client = channelFactory.CreateChannel();

            client.InterestingMethod().Should().Be( "foo" );
        }

        serviceHost.Close();
    }

    [ServiceContract]
    interface IServiceInterface
    {
        [OperationContract]
        string InterestingMethod();
        [OperationContract]
        string UninterestingMethod();
    }

    [ServiceContract(Name = "IServiceInterface")]
    interface ISubsetInterface
    {
        [OperationContract]
        string InterestingMethod();
    }

    class Service : IServiceInterface
    {
        public string InterestingMethod() { return "foo"; }

        public string UninterestingMethod() { throw new NotImplementedException(); }
    }
}
like image 153
Bronumski Avatar answered Oct 30 '22 02:10

Bronumski


I suggest to use a shared assembly which contains the DataContracts.

Then use the ChannelFactory<T> class to create an instance of the server Interface. Then you can make calls to the Server without any WSDL.

like image 21
GameScripting Avatar answered Oct 30 '22 02:10

GameScripting