当以编程方式更新wcf服务的web.config时,可以通过执行添加行为...
ServiceModelSectionGroup secgroup = (ServiceModelSectionGroup)_webConfig.GetSectionGroup("system.serviceModel");
ServiceBehaviorElement SerBeh3 = new ServiceBehaviorElement();
SerBeh3.Name = "AuthenticationSvcWrapBehavior";
secgroup.Behaviors.ServiceBehaviors.Add(SerBeh3);
我的问题是你如何添加绑定部分?
我想要做的就是创建一个名称为Mode的模式和Transport.ClientCredentialType,然后将BindingConfiguration设置为端点的所述名称。
答案 0 :(得分:2)
我想出了如何在配置中添加绑定部分。也许我很密集,但我认为关于配置改变的文档很糟糕......
//Update Service model for wcf services
ServiceModelSectionGroup secgroup = (ServiceModelSectionGroup)_webConfig.GetSectionGroup("system.serviceModel");
//Add the binding section with the settings that enable HTTPS communications
secgroup.Bindings.BasicHttpBinding.Bindings.Add(CreateBasicHttpBinding("SecureWebBinding",
BasicHttpSecurityMode.Transport,
HttpClientCredentialType.None));
private BasicHttpBindingElement CreateBasicHttpBinding(string name, BasicHttpSecurityMode mode, HttpClientCredentialType credentialType)
{
BasicHttpBindingElement basicHttpBinding = new BasicHttpBindingElement();
basicHttpBinding.Name = name;
basicHttpBinding.Security.Mode = BasicHttpSecurityMode.Transport;
basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
return basicHttpBinding;
}
答案 1 :(得分:0)
您可以像编辑任何其他XML文件一样对其进行编辑,但我认为更改只有在您重新启动应用程序后才会生效。
我个人不再使用WCF的XML配置了。纯代码解决方案的优点大大超过了我的缺点。
答案 2 :(得分:0)
public static BasicHttpBinding GetBinding(string Url, int timeoutSeconds)
{
BasicHttpBinding binding = null;
UriBuilder urb = new UriBuilder(Url);
switch (urb.Scheme)
{
case "http":
binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
break;
case "https":
binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
break;
default:
throw new ArgumentException("unknown scheme : " + urb.Scheme);
}
binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
binding.MaxBufferPoolSize = int.MaxValue;
binding.MaxReceivedMessageSize = int.MaxValue;
binding.MaxBufferSize = int.MaxValue;
if (timeoutSeconds > 0)
binding.SendTimeout = TimeSpan.FromSeconds(timeoutSeconds);
return binding;
}
来电者致电
EndpointAddress addr = new EndpointAddress(url);
Binding bind = DataProviderUtilities.GetBinding(_url, timeOutSeconds);
yourserviceClient foo = new yourServiceClient(addr, bind);