0

私は、ステージと本番の Web サービスを使用する .Net c# で Web サービス クライアントを作成しています。Web メソッドの機能は、ステージと本番の両方で同じです。お客様は、一部のデータ検証のために、実稼働 Web サービスとステージ Web サービスの両方から Web メソッドを使用する機能を望んでいます。これにより、2 つの別個のプロキシ クラスと 2 つの別個のコード ベースを生成できます。以下のようなことをして冗長なコードを排除できるようにするためのより良い方法はありますか

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

client.authenticate 
client.getUsers
client.getCities
4

1 に答える 1

2

たった 1 人のクライアントで問題を解決できるはずです。コントラクトが同じ場合は、エンドポイント構成とリモート アドレスをプログラムで指定できます。

次のようなものがあるとしましょう。

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");

これにより、オブジェクトの 1 つのセットを使用して渡すことができるようになります。上記の「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>
于 2013-07-08T19:39:27.837 に答える