2011-10-21 145 views
2

我添加了一个WCF服务引用Silverlight应用程序和这里是从web.config中,我有看起来像Silverlight和双工WCF服务

<bindings> 
    <wsDualHttpBinding> 
    <binding name="wsDualHttpBinding"> 
     <security mode="None" /> 
    </binding> 
    </wsDualHttpBinding> 
    <pollingDuplexHttpBinding> 
    <binding name="multipleMessagesPerPollPollingDuplexHttpBinding" 
     duplexMode="MultipleMessagesPerPoll" /> 
    </pollingDuplexHttpBinding> 
</bindings> 

约束力和我有这样的片段,以创建服务客户端实例

var serviceClient = new DuplexCallerIdServiceClient(
     new PollingDuplexHttpBinding(PollingDuplexMode.MultipleMessagesPerPoll), 
     new EndpointAddress("http://localhost:51445/Service/MyService.svc")); 

我担心的是为什么我必须在代码中提供绝对网址。我有一个使用相同服务的winforms应用程序,我只需要执行new DuplexCallerIdServiceClient()即可创建一个看起来很理想的服务客户端实例。有什么办法可以解决它吗?我无法更改绑定设置。

谢谢

+1

您可以调用不带参数的服务客户端构造函数的原因是该地址是在app.config中指定的。如果你不喜欢这个网址是绝对的,你可以使用一个相对的网址,比如'../MyService.svc' – vorrtex

回答

0

您不必硬编码服务URL。替换作为参数传入的硬编码字符串或进行函数调用(或获取某个对象的属性)以使用有效的服务URL填充构造函数。

这里还有许多的一种方式:

var serviceClient = new DuplexCallerIdServiceClient(
    new PollingDuplexHttpBinding(PollingDuplexMode.MultipleMessagesPerPoll), 
    new EndpointAddress(Info.Instance.ServiceURL)); 

如果信息是一个单独的对象,实例都有单身的实例和的serviceUrl是来自......的地方的字符串属性。数据库,配置文件,硬编码开始等...

P.S.小心Singleton模式,但作为配置信息实体,它们可能非常有用。