从配置中设置行为?

时间:2016-11-10 16:43:20

标签: c# .net wcf

我有一个WCF客户端&服务解决方案,我在这里创建客户端通道:

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;
            }
        }

这很好但现在我也需要设置行为。在配置文件中,Endpoint有一个behaviorConfiguration属性,需要在上面设置,这是怎么做的?

1 个答案:

答案 0 :(得分:0)

将行为添加到ChannelFactory(客户端)对象:

channelFactory.Endpoint.EndpointBehaviors.Add(new WebHttpBehavior());

并添加ServceHost(服务器)对象:

   serviceHost.Endpoint.EndpointBehaviors.Add(new ServiceMetadataBehavior());

您可以在此处阅读更多内容:MSDN

相关问题