【问题标题】:c# proxy class for test and production web service用于测试和生产 Web 服务的 c# 代理类
【发布时间】:2012-08-02 13:36:53
【问题描述】:

我使用从 Visual Studio 2010 生成的 Web 服务代理类从我的 .net 2.0 客户端访问 Web 服务。

这一切正常,但我的问题是,Web 服务有一个用于测试系统的 wsdl 和一个用于生产系统的 wsdl。

虽然 Web 服务在结构上完全相同,但它们的 xml 命名空间却不同。

在我的代码中,我可以设置 web 服务的服务 URL 并且通信正常(我使用 TraceExtension 将 SOAP 消息转储到日志文件),但是在反序列化时,我得到了一个异常。

我认为这是因为我使用来自测试系统的 WSDL 文件生成了代理类,并添加了如下几个属性: [System.Xml.Serialization.SoapTypeAttribute(Namespace="testservice url here")]

我当然可以复制我的项目并向生产服务添加网络引用,但这会复制我的代码,并且我必须在未来在两个项目中进行任何进一步的更改,这当然容易出错。

这是针对测试系统服务调用登录方法的有效响应:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://TEST_SERVER_NAME/ProjectService">
    <SOAP-ENV:Body>
        <ns1:loginResponse xmlns:ns1="http://TEST_SERVER_NAME/ProjectService">
            <login_result xsi:type="tns:LoginResult">
                <errorcode xsi:type="xsd:integer">0</errorcode>
                <errortext xsi:type="xsd:string">Successfully logged in</errortext>
                <logincode xsi:type="xsd:string">wF3N5vdPsueL0aYlp41i6B8VTZEJztqx</logincode>
            </login_result>
        </ns1:loginResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这是来自生产网络服务的相同响应:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://PRODUCTION_SERVER_NAME/ProjectService">
    <SOAP-ENV:Body>
        <ns1:loginResponse xmlns:ns1="http://TEST_SERVER_NAME/ProjectService">
            <login_result xsi:type="tns:LoginResult">
                <errorcode xsi:type="xsd:integer">0</errorcode>
                <errortext xsi:type="xsd:string">Successfully logged in</errortext>
                <logincode xsi:type="xsd:string">wOmdlZMkIXVL4H8uTGU69hgpNsK1Cz3Q</logincode>
            </login_result>
        </ns1:loginResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

如您所见,两个响应仅在 xmlns:tns 属性上有所不同,而 xmlns:ns1 属性始终为http://TEST_SERVER_NAME/ProjectService(我猜这是来自带有测试系统的 WSDL 的生成代理类) . 登录码只是一个身份验证令牌,并且总是不同的,所以没关系。

这个问题还有其他解决办法吗?

【问题讨论】:

    标签: c# visual-studio-2010 web-services wsdl proxy-classes


    【解决方案1】:

    真的迟到了,但我使用预处理器指令来解决这个问题:

    #define SANDBOX //Change from sandbox to production to switch between both systems.
    
    #if SANDBOX
        using NetSuite.com.netsuite.sandbox.webservices;
    #endif
    
    #if PRODUCTION
        using NetSuite.com.netsuite.webservices;
    #endif
    

    确保将这两个引用作为 Web 引用添加到您的项目中,并将您的 SANDBOX 更改为 PRODUCTION 以使用生产网络服务,并将 SANDBOX 用于沙箱。

    这可能不是最优雅的解决方案,但它又快又脏,完全符合我的需要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 2017-11-12
      • 1970-01-01
      相关资源
      最近更新 更多