【问题标题】:Configuring multiple SOAP services in Web.config在 Web.config 中配置多个 SOAP 服务
【发布时间】:2018-07-31 10:51:13
【问题描述】:

我的应用程序需要使用几个我无法修改的 SOAP 服务(它们被导入 Visual Studio 并且类是自动生成的)。我已经将每个服务包装在我自己的自定义类中(以便我可以扩展功能),并且每个包装器都存在于它自己的类库项目中。然后我的主应用程序实例化我的包装器以使用这些服务。像这样:

MyProject.Service1 (class library)
    - instantiates and uses RemoteService1
    - has app.config file

MyProject.Service2 (class library)
    - instantiates and uses RemoteService2
    - has app.config file

MyProject (web application)
    - instantiates and uses MyProject.Service1 and MyProject.Service2
    - has Web.config file

当 Visual Studio 为远程服务自动生成类时,app.config 文件已更新。这是MyProject.Service1

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="RemoteService1HttpBinding">
                    <textMessageEncoding messageVersion="Soap12" />
                    <httpTransport />
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint
                address="http://path.com/to/RemoveService1"
                binding="customBinding" 
                bindingConfiguration="RemoteService1HttpBinding"
                contract="RemoteService1Contract" 
                name="RemoteService1Name" />
        </client>
    </system.serviceModel>
</configuration>

然后我手动将绑定和客户端信息复制粘贴到MyProject Web.config:

<system.serviceModel>
    <bindings>
        <customBinding>
            <binding name="RemoteService1HttpBinding">
                <textMessageEncoding messageVersion="Soap12" />
                <httpTransport />
            </binding>
       </customBinding>
    </bindings>
    <client>
        <endpoint
            address="http://path.com/to/RemoveService1"
            binding="customBinding" 
            bindingConfiguration="RemoteService1HttpBinding"
            contract="RemoteService1Contract" 
            name="RemoteService1Name" />
    </client>
</system.serviceModel>

效果很好。我能够实例化一个新的MyService.Service1 对象并调用RemoteService1 SOAP 服务。

问题是我无法配置第二个服务。我尝试手动将自动生成的 app.config 设置添加到 Web.config 文件中,但出现此错误:

The element &lt;customBinding&gt; may only appear once in this section.

我应该如何配置多个 SOAP 服务?

【问题讨论】:

  • 啊,你是对的!那效果很好。与具有多个端点的 &lt;client /&gt; 相同。如果您将评论复制到答案中,我会接受。

标签: c# asp.net .net wcf soap


【解决方案1】:

只要您给每个绑定一个不同的名称,您就可以在绑定部分中拥有任意数量的绑定。在您的端点上,您指定要使用的绑定的类型和名称。 binding 属性是指绑定的类型,bindingConfiguration 是指您要使用的那种类型的绑定的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-10
    • 2012-06-23
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多