WCF channelfactory与配置文件中的设置?

女妖

我有一个使用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());

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

女妖

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章