I know it is an old question, but I have had the same problem and found no answer on the SO. As it took me a couple days of digging to solve the problem, I think it is worth posting it here.
You generally have 3 options here:
1) Configure your service host completely in code (ignoring *.config) as per here
2) Make a script to modify *.config or use other config as per here
3) Use both *.config and programmatic binding configuration
Option (3) is tricky as you have to modify your service host after it has been created but before it would be open (something like host.State == Created
). This because modification of bindings of an already open host has no effect. More details here.
To get (3) work you have to use custom host factory. Sample markup:
<%@ ServiceHost
Language="C#"
Debug="true"
CodeBehind="MyService.svc.cs"
Service="Namespace.MyService, MyService"
Factory="Namespace.MyWcfHostFactory, MyService"
%>
MyWcfHostFactory
should inherit ServiceHostFactory
and extend/override CreateServiceHost
method.
I am using DI framework (Autofac) which simplifies things a bit. So MyWcfHostFactory
just inherits AutofacHostFactory
. Startup code (called from either Global.asax
Application_Start
or static constructor of MyWcfHostFactory
):
var builder = new ContainerBuilder();
// Register your service implementations.
builder.RegisterType< Namespace.MyService>();
// Set the dependency resolver.
var container = builder.Build();
AutofacHostFactory.Container = container;
AutofacHostFactory.HostConfigurationAction = (host => PerformHostConfiguration(host));
In PerformHostConfiguration
you can overwrite or modify bindings. Code below takes 1st registered endpoint and replaces its binding with https one:
/// <summary> This is used as a delegate to perform wcf host configuration - set Behaviors, global error handlers, auth, etc </summary>
private static void PerformHostConfiguration(ServiceHostBase host) {
var serviceEndpoint = host.Description.Endpoints.First();
serviceEndpoint.Binding = new BasicHttpBinding {
ReaderQuotas = {MaxArrayLength = int.MaxValue},
MaxBufferSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue,
Security = new BasicHttpSecurity {
Mode = BasicHttpSecurityMode.Transport, //main setting for https
},
};
}