user2561923
user2561923

Reputation: 3

Sharing web service methods

I am writing web service client in .Net c# which consumes stage and production web services.Functionality of web methods is same in both stage and production. Client wants capability of using web methods from both production and stage web services for some data validation. I can do this generating two separate proxy classes also two separate code bases. Is there a better way so that I can eliminate redundant code by doing something like below

if (clintRequest=="production")
    produtionTypeSoapClient client= new produtionTypeSoapClient()
else
    stageSoapClient client= new stagetypeSoapClient()
//Instantiate object. Now call web methods

client.authenticate 
client.getUsers
client.getCities

Upvotes: 0

Views: 130

Answers (1)

ClosedEyesSeeing
ClosedEyesSeeing

Reputation: 335

You should be able to get away with just one client. If the contract is the same, you can specify the endpoint configuration and the remote address programmatically.

Let us say you have something like this:

1) Staging - http://staging/Remote.svc
2) Production - http://production/Remote.svc

If you are using Visual Studio you should be able to generate a client from either endpoint.

You should, then, be able to do something like this:

C# Code:

OurServiceClient client;
if (clientRequest == "Staging")
    client = new OurServiceClient("OurServiceClientImplPort", "http://staging/Remote.svc");
else
    client = new OurServiceClient("OurServiceClientImplPort", "http://production/Remote.svc");

This should allow you to use one set of objects to pass around. The 'OurServiceClientImplPort' section above is referencing the config file for the endpoint:

Config:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="OurServiceClientSoapBinding" openTimeout="00:02:00" receiveTimeout="00:10:00" sendTimeout="00:02:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
              <readerQuotas maxDepth="128" maxStringContentLength="9830400" maxArrayLength="9830400" maxBytesPerRead="40960" maxNameTableCharCount="32768"/>
              <security mode="TransportCredentialOnly">
                <transport clientCredentialType="Basic" realm=""/>
              </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <!-- This can be either of the addresses, as you'll override it in code -->
        <endpoint address="http://production/Remote.svc" binding="basicHttpBinding" bindingConfiguration="OurServiceClientSoapBinding" contract="OurServiceClient.OurServiceClient" name="OurServiceClientImplPort"/>
    </client>
</system.serviceModel>

Upvotes: 2

Related Questions