2009-05-22 47 views
0

我有一个使用WCF和wsHttpBindings公开Web服务的Web应用程序。可以在不同的机器和不同的URL上安装应用程序。这意味着每个WCF服务位置都会有所不同。使用WCF在运行时确定wsHttpBinding

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

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

回答

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> 

然后在代码中,你可以根据它的名字创建这样一个端点(客户端代理),因此你可以选择你需要的任何一个位置。没有什么能阻止你创建多个客户端代理,无论是!因此,您可以使用多个客户端代理连接到多个服务器端点,没问题。或者,你当然也可以在代码中创建一个“WsHttpBinding”和“EndpointAddress”的实例,并设置必要的属性(如果有的话),然后用这个现成的对象调用客户端代理的构造函数,从而覆盖整个马戏团的app.config和创造任何你觉得需要:

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

马克

0

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

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

+0

你知道这样做的“简单的方法”的任何实例,并在访问/使用多个端点运行? – mickyjtwin 2009-05-22 02:14:41

+0

Web应用程序上的服务设置也是wshttp,而不是基本的。 – mickyjtwin 2009-05-22 02:23:14