【问题标题】:WCF channelfactory with settings from the config file?WCF channelfactory 与配置文件中的设置?
【发布时间】:2016-11-18 15:05:00
【问题描述】:

我有一个使用 WCF 的服务器和客户端解决方案。客户端将在运行时向服务询问有关活动服务器的 URL 并能够设置它,我使用 ChannelFactory。但是,我仍然需要使用配置文件中的所有其他 WCF 设置。我就是这样做的:

var clientSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;

            var address = string.Empty;
            for(int i = 0; i < clientSection.Endpoints.Count; i++)
            {
                if(clientSection.Endpoints[i].Name == endpointConfigurationName)
                {
                    var endpointAddress = new EndpointAddress(clientSection.Endpoints[i].Address.ToString());
                    var netHttpBinding = new NetHttpBinding(clientSection.Endpoints[i].BindingConfiguration);
                    var serviceEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(T)), netHttpBinding, endpointAddress);

                    var channelFactory = new ChannelFactory<T>(serviceEndpoint);

                    break;
                }
            }

问题是我有 2 个 BehaviorExtensions 被一些类似这样的端点使用。

<services>
<endpoint binding="netHttpBinding" behaviorConfiguration="protoEndpointBehavior" address="BinaryHttpProto" bindingNamespace="http://MyApp.ServiceContracts/2007/11" contract="MyApp.ServiceContracts.IMyAppClientService" />
</services>

<behaviors>
<endpointBehaviors>
        <behavior name="protoEndpointBehavior">
          <protobuf />
        </behavior>
      </endpointBehaviors>
    </behaviors>

<extensions>
      <behaviorExtensions>
        <add name="protobuf" type="ProtoBuf.ServiceModel.ProtoBehaviorExtension, protobuf-net, Version=2.0.0.668, Culture=neutral, PublicKeyToken=257b51d87d2e4d67" />
      </behaviorExtensions>
    </extensions>

问题是我如何从 clientSection.Endpoints 读取这个?并将其设置在 channelFactory 上?我知道我可以像这样手动创建:

serviceEndpoint.EndpointBehaviors.Add(new ProtoEndpointBehavior());
            serviceEndpoint.EndpointBehaviors.Add(new CustomMessageInspectorBehavior());

但是这将是一个硬编码的静态,它将适用于所有端点,我需要能够从配置中更改它。

【问题讨论】:

    标签: c# .net wcf


    【解决方案1】:

    您不需要自己创建 ChannelFactory。只需创建一个继承自ClientBase&lt;T&gt; 的 ClientService 类。 ClientBase&lt;T&gt; 的构造函数接受 EndpointName 并自动添加与此 Endpoint 关联的行为。 ClientBase&lt;T&gt; 还让您可以访问ChannelFactory&lt;T&gt;,并且您可以打开任意数量的频道。您唯一需要做的就是在配置中为您要使用的每个端点添加一个名称。

    <endpoint binding="..." name="MyEndPoint" ... />
    

    【讨论】:

    • 谢谢,但看起来我需要为这个 ClientBase 类中的每个服务方法添加代码?像这样: return base.Channel.MySimpleMethod(request);这是真的?我有数百个网络方法。
    • 是的,这是不利的一面,也是正常的方式。我们只用两种方法解决了这个问题。我们的 WebService 有 2 种方法,一种用于有结果的请求,一种用于没有结果的请求。所有其他的东西都是通过请求处理的,我们只需要查看给定的 RequestType 并调用可以处理请求的处理程序。
    • 对不起,真的不明白吗?数百个 webmethod 怎么可能只有 2 个?都需要有自己的方法名、请求和响应吗?
    • 你的方法只需要接受一个请求,然后你继承这个请求,比如CustomerSearchRequest,并带有你喜欢在搜索中使用的属性。然后您需要某种基本处理程序,它检查请求的类型并调用可以处理它的处理程序,例如CustomerSearchRequestHandler,处理程序会产生响应。如果您愿意,您可以使 SearchRequest 通用,您不需要为要搜索的每种类型创建 SearchRequest。
    【解决方案2】:

    我不得不在代码中创建所有内容,混合解决方案并不好,在我使用大量自定义内容的情况下不是这样。

    【讨论】:

    • 恭喜。但是,您的答案仍然只有一些示例和解释才有用..
    猜你喜欢
    • 2011-06-30
    • 2010-12-22
    • 2012-05-07
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    相关资源
    最近更新 更多