【问题标题】:How to consume multiple WCF services from one client如何从一个客户端使用多个 WCF 服务
【发布时间】:2010-10-15 17:26:19
【问题描述】:

我还在学习整个 WCF 的东西,所以请耐心等待。

我有两个使用 C# 和 VS 2008 创建的自托管服务:
服务 #1 将两个数字相加并返回结果。
服务 #2 返回数字的平方。

我希望客户端能够将两个数字发送到服务 1,得到总和,然后将总和发送到服务 2 并得到平方。

我为这两个服务生成了两个代理,并且我可以在它们上使用 Intellisense,所以这部分应该可以工作。

现在如何配置我的 app.config 文件,以便我可以与这两个服务进行通信?现在,每次我尝试这样做时都会遇到异常。

[如果我在应用程序文件中一次只有一个配置,则客户端可以正常工作,并尝试仅调用该服务器。]

我想这是一个非常愚蠢的问题,答案可能是“以_____方式构造配置文件”,但谷歌似乎根本没有示例/指南。

有人知道怎么做吗?

注意:Consume multiple WCF services from one client 客户虽然听起来像重复不是我要找的。​​p>

编辑:感谢 marc_s,我得到了它的工作

由于这两个服务都在不同的应用程序中运行,我不需要拆分服务器配置文件,但这是我对客户端配置文件所做的:首先使用 SvrUtil.exe 自动生成配置文件,然后将它们合并这样:

<bindings>
  <wsHttpBinding>

    <binding>
    ...
    </binding>

    <binding>
    ...
    </binding>

  </wsHttpBinding>
</bindings>

...

  <endpoint>

...

【问题讨论】:

  • +1 - 投票支持将您的代表从 1,999 提高到 2K,因此您现在可以编辑其他人的答案!哇!
  • @Jeremy:哈哈谢谢!令人难以置信的是,它在 1999 年停留了多久。

标签: c# .net wcf wcf-client


【解决方案1】:

你只是有端点冲突吗?例如,确保两个服务未配置为侦听相同的端口号。如果您可以发布您的配置文件(或经过清理的版本)会有所帮助。

【讨论】:

  • 我确实让它们都在同一个端口上运行,但我可以从服务 B 调用服务 A,两者都监听端口 8000。假设我确实在不同的端口上运行它们,我该如何构建app.config?
  • 你会有多个 元素,依此类推。你不能只复制两个完整的配置部分,你必须合并它们。
【解决方案2】:

我知道您已要求 App.Config 答案,但认为这可能会有所帮助。我通常会首先以编程方式配置客户端连接,因为它更简单,一旦你开始工作,你可以将它移动到你的 App.Config。

这是一个如何配置 WCF 客户端的示例。

BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress(serverURL);
MyServiceClient myServiceProxy = new MyServiceClient(binding, address);

然后,您的 App.Config 中可能会有如下内容。

<client>
    <endpoint address="http://localhost/service1.asmx"
            binding="basicHttpBinding"
            bindingConfiguration="basicHttpBinding" 
            contract="IService1"
            name="Service1" />
    <endpoint address="http://localhost/service2.asmx" 
            binding="basicHttpBinding"
            bindingConfiguration="basicHttpBinding"
            contract="IService2"
            name="Service2" />
</client>

【讨论】:

    【解决方案3】:

    如果您想在不同的端点/端口上运行这两个服务,请执行以下操作:

    服务器端:

    <service name="Service1">
        <endpoint address="http://localhost:8001/service1.asmx"
                binding="basicHttpBinding"
                contract="IService1" />
    </service>
    <service name="Service2">
        <endpoint address="http://localhost:8002/service2.asmx" 
                binding="basicHttpBinding"
                contract="IService2" />
    </service>
    

    客户端:

    <client>
        <endpoint address="http://localhost:8001/service1.asmx"
                binding="basicHttpBinding"
                contract="IService1"
                name="Service1" />
        <endpoint address="http://localhost:8002/service2.asmx" 
                binding="basicHttpBinding"
                contract="IService2"
                name="Service2" />
    </client>
    

    这应该会在服务器上为您提供两个独立的端点,以及一个可以与两者对话的客户端。

    马克

    【讨论】:

    • 谢谢,成功了!合并配置文件有很多排列,我只是决定询问,而不是涵盖所有排列:P
    猜你喜欢
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多