【问题标题】:Use new ChannelFactory<TChannel>(string) if the config is in a string?如果配置在字符串中,请使用 new ChannelFactory<TChannel>(string)?
【发布时间】:2011-04-19 22:32:26
【问题描述】:

我想使用 ChannelFactory 为给定端点创建 WCF 客户端。

问题是我没有 web.config 或 app.config,但我在字符串中确实有整个 &lt;system.serviceModel&gt; XML 块。

当我已经有了配置时,我不想手动解析它并以编程方式创建 BIndings 和 Endpoints。

有没有办法告诉 ChannelFactory 只使用该块作为它的配置?或者至少创建一个ServiceEndpoint

【问题讨论】:

    标签: c# .net wcf web-config


    【解决方案1】:

    this blog entry 中描述了一种技术。

    它不像一行代码那么简单,但至少它在比原始 XML 更高的层次上工作。

    【讨论】:

    • 那行得通。不像我想的那么简单,但它很有效,让我省了很多麻烦。
    【解决方案2】:

    您可以手动创建绑定和端点地址来创建 CHannelFactory 的实例,例如:

       BasicHttpBinding binding = new BasicHttpBinding() { 
           Name = "Bindingname"
           // Goes all the necessary members to set.
       };
    
       EndpointAddress endpoint = new EndpointAddress("http://serviceendpoint.com");
       ChannelFactory<IContract> factory = new ChannelFactory<IContract>(binding, endpoint);
    

    然后你可以尝试使用 factory.CreateChannel() 在你的 ServiceEnpoint、Behaviors 等之前探索它的成员。

    希望对您有所帮助,谢谢。

    【讨论】:

    • 谢谢,但正如我所写:I do not want to manually parse that out and programatically create the Bindings and Endpoints when I already have the configuration.。我已经在使用编程方式,但我想停止重复代码,因为我已经有一个 &lt;system.serviceModel&gt; XML 块。
    • 我的错,我认为您可以将其转换为泛型以减少为每个合约创建 ChannelFactory&lt;TChannel&gt; 实例的代码。
    【解决方案3】:

    找到了更好的解决方案:ConfigurationChannelFactory&lt;TChannel&gt; 允许从自定义配置创建 WCF 通道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 2013-01-23
      • 2012-11-06
      相关资源
      最近更新 更多