2016-11-10 104 views
0

我有,我会创建这样的客户通道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属性,这需要在上面设置,这是如何完成的?

+0

HTTPS ://blogs.msdn.microsoft.com/carlosfigueira/2011/06/28/wcf-extensibility-behavior-configuration-extensions/ –

回答

0

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

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

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

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

你可以在这里阅读更多:MSDN

+0

谢谢,但问题是行为不exis ts,而不是有channelFactory.Endpoint.EndpointBehaviors.Add(IEndpointBehavior)。我不知道如何从clientSection.Endpoints [i]设置它。所有信息都存在于配置文件中,但似乎很难读取并手动设置。 – Banshee

+0

是的你是对的,客户端('ChannelFactory')有'EndpointBehaviors',服务器'ServiceHost'有'Endpoint',坏了。我会编辑答案。 –