繁体   English   中英

共享Web服务方法

[英]Sharing web service methods

我在.Net c#中编写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

你应该只能与一个客户一起逃脱。 如果合同相同,则可以以编程方式指定端点配置和远程地址。

让我们说你有这样的事情:

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM