简体   繁体   English

Silverlight和Duplex WCF服务

[英]Silverlight and Duplex WCF Service

I have added a WCF service reference to Silverlight application and here's what the binding from web.config that I have looks like 我向Silverlight应用程序添加了WCF服务引用,这就是我从web.config中获得的绑定外观

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

And I have this snippet to create a service client instance 我有这个片段来创建服务客户端实例

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

My concern is that why do I have to provide an absolute url in code. 我担心的是为什么我必须在代码中提供一个绝对URL。 I have a winforms application that uses the same service and I can just do new DuplexCallerIdServiceClient() to create a service client instance which seems ideal. 我有一个使用相同服务的new DuplexCallerIdServiceClient()应用程序,我可以做一个new DuplexCallerIdServiceClient()来创建一个看起来很理想的服务客户端实例。 Is there any way I can work around it. 有什么办法可以解决吗? I cannot change the binding settings. 我无法更改绑定设置。

Thanks 谢谢

You do not have to hardcode the service URL. 您不必对服务URL进行硬编码。 Replace the hard coded string that either is passed in as an argument or makes a function call (or gets some object's property) to populate the constructor with a valid service URL. 替换作为参数传递或进行函数调用(或获取某些对象的属性)的硬编码字符串,以使用有效的服务URL填充构造函数。

Here's one way among many: 这是众多方法中的一种:

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

Where Info is a singleton object, Instance gets the singleton's instance and ServiceUrl is a string property that comes from... wherever. 其中Info是一个单例对象,Instance获取该单例的实例,ServiceUrl是一个字符串属性,该属性来自于任何地方。 Database, config file, hard coded to start etc... 数据库,配置文件,硬编码启动等...

PS Careful with the Singleton pattern, but as config info entities they can be very useful. PS小心Singleton模式,但作为配置信息实体,它们可能非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM