2013-07-08 167 views
0

我正在編寫.Net c#中的Web服務客戶端,它消耗舞臺和生產Web服務。Web方法的功能在階段和生產中都是相同的。 客戶希望能夠使用來自生產和舞臺Web服務的Web方法進行一些數據驗證。我可以這樣做,生成兩個獨立的代理類,也是兩個獨立的代碼庫。有沒有更好的辦法,這樣我可以做類似下面共享Web服務方法

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

client.authenticate 
client.getUsers 
client.getCities 
+0

您正在使用的Web引用,或一個服務引用(首選)? –

回答

2

你應該能夠只用一個客戶端脫身消除冗餘代碼。如果合同相同,則可以通過編程方式指定端點配置和遠程地址。

讓我們說,你有這樣的事情:

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

如果您正在使用Visual Studio,你應該能夠生成兩個端點的客戶端。

你應該,那麼,能夠做這樣的事情:

C#代碼:

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

這應該允許您使用一組對象以繞過。該「OurServiceClientImplPort」部分以上引用的配置文件端點:

配置:

<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>