Reputation: 115
When programmatically updating the web.config for a wcf service it's possible to add a behavior by doing...
ServiceModelSectionGroup secgroup = (ServiceModelSectionGroup)_webConfig.GetSectionGroup("system.serviceModel");
ServiceBehaviorElement SerBeh3 = new ServiceBehaviorElement();
SerBeh3.Name = "AuthenticationSvcWrapBehavior";
secgroup.Behaviors.ServiceBehaviors.Add(SerBeh3);
My question is how you add the binding section?
All I want to do is create a binding with name, Mode and Transport.ClientCredentialType then set the BindingConfiguration to said name for the endpoint.
Upvotes: 2
Views: 3794
Reputation: 115
I figured out how to add the binding section in the config. Maybe I'm dense but I think the documentation on config changes sucks...
//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;
}
Upvotes: 2
Reputation: 435
Um.. I should add this as a comment, but apparently my brain just went off the deep end, and I cannot locate the reply link on comments any more :( (Sad brain) Anyhoo, I was surprised by your assertion that M$ deprecates setting wcf config from code, until I read the link- I would say their intention is that that config files sre prefererrable to code config which is hard coded in the code. When your binding values come from a dynamic sw config system, code configuration is waaay better thatn file configuration. heres my code to create a basit http binding, either with or without ssl:
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;
}
the caller calls
EndpointAddress addr = new EndpointAddress(url);
Binding bind = DataProviderUtilities.GetBinding(_url, timeOutSeconds);
yourserviceClient foo = new yourServiceClient(addr, bind);
Upvotes: 0
Reputation: 70287
You can edit it like any other XML file, but I don't think the changes will take effect until you restart the application.
Personally I don't use XML configuration for WCF anymore. The advantages of a pure code solution greatly outweight the drawbacks for me.
Upvotes: 0