【问题标题】:Silverlight and Duplex WCF ServiceSilverlight 和双工 WCF 服务
【发布时间】:2011-10-21 03:30:03
【问题描述】:

我添加了一个对 Silverlight 应用程序的 WCF 服务引用,这就是我所拥有的来自 web.config 的绑定的样子

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

我有这个 sn-p 来创建一个服务客户端实例

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

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

谢谢

【问题讨论】:

  • 之所以可以不带参数调用服务客户端构造函数,是因为在app.config中指定了地址。如果您不喜欢 url 是绝对的,您可以使用相对 url,例如 '../MyService.svc'

标签: c# silverlight wcf silverlight-4.0 pollingduplexhttpbinding


【解决方案1】:

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

这是众多方法中的一种:

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

Info 是一个单例对象,Instance 获取该单例的实例,ServiceUrl 是一个字符串属性,它来自...任何地方。数据库、配置文件、硬编码启动等...

附:小心单例模式,但作为配置信息实体,它们可能非常有用。

【讨论】:

    猜你喜欢
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    相关资源
    最近更新 更多