0

我有一个使用 WCF 和 wsHttpBindings 公开 Web 服务的 Web 应用程序。可以在不同的机器和不同的 url 上安装应用程序。这意味着每个 WCF 服务位置都不同。

我正在构建一个 Windows 服务,它将引用每个应用程序并执行一项任务。每个任务都需要调用 Web 应用程序上的服务。我知道绑定都是在 app.config 中设置的,但是有没有更简单的方法来动态调用服务,或者我将如何构建 app.config?

<webApplication WebServiceUrl="http://location1.com/LunarChartRestService.svc" />
<webApplication WebServiceUrl="http://location2.com/LunarChartRestService.svc"/>
4

2 回答 2

1

您的客户端的配置文件可能如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <client>
      <endpoint name="Endpoint1"
                address="http://location1.com/LunarChartRestService.svc"
                binding="wsHttpBinding"
                contract="(whatever-your-contract-is)" />
      <endpoint name="Endpoint2"
                address="http://location2.com/LunarChartRestService.svc"
                binding="wsHttpBinding"
                contract="(whatever-your-contract-is)" />
      <endpoint name="Endpoint3"
                address="http://location3.com/LunarChartRestService.svc"
                binding="wsHttpBinding"
                contract="(whatever-your-contract-is)" />
    </client>
  </system.serviceModel>
</configuration>

然后在代码中,您可以根据其名称创建这样的端点(客户端代理),因此您可以选择您需要的任何位置。也没有什么能阻止您创建多个客户端代理!因此,您可以使用多个客户端代理连接到多个服务器端点,没问题。

Alternatively, you can of course also create an instance of "WsHttpBinding" and "EndpointAddress" in code, and set the necessary properties (if any), and then call the constructor for the client proxy with this ready made objects, thus overriding the whole app.config circus and creating whatever you feel is needed:

EndpointAddress epa = 
    new EndpointAddress(new Uri("http://location1.com/LunarChartRestService.svc"));
WSHttpBinding binding = new WSHttpBinding();

Marc

于 2009-05-22T05:10:09.620 回答
0

根据您的描述,听起来好像所有服务器都公开了相同的服务合同。如果是这样,您可以在 web.config 中声明多个端点,并在运行时根据端点名称选择一个。

当然,您可能不想处理 WCF 配置的那部分,而宁愿只拥有一个更简单的 URL 列表并完成它。这也是完全可能的;您只需要在代码端做更多的工作来实例化客户端代理/通道对象。

于 2009-05-22T02:07:45.297 回答