My app has to be installed on my client's webservers. Some clients want to use SSL and some do not. My app has a WCF service and I currently have to go into the web.config for each install and switch the security mode from <security mode="Transport">
to <security mode="TransportCredentialOnly">
depending on the client's SSL situation. I am able to set the client bindings at runtime. However, I would like to know if there is a way to set the service bindings at runtime(on the server side).
views:
124answers:
1Yes, absolutely! It depends on how you're hosting your WCF services. Saying it has to be installed on the webservers, I would assume you're hosting in IIS.
In that case, you need to create your own descendant of ServiceHostFactory
- which really isn't that big a deal.
Your CustomServiceHostFactory
is needed to return an instance of your ServiceHost
, properly configured to your needs, to IIS.
In the CreateServiceHost
method of the custom factory, you basically set up your ServiceHost
and configure all its endpoints, behaviors, bindings, etc. - all in code, all under your full control. You can do whatever you need to do here, to configure your service just as needed.
In order to host your service. You'll need to adapt the MyService.svc
file to include that CustomServiceHostFactory
as the factory to use:
<% @ ServiceHost Language="C#" Service="YourService"
Factory="CustomServiceHostFactory" %>
and that's it!
Check out Extending Hosting Using ServiceHostFactory on MSDN for more details, and see the A Custom ServiceHostFactory article on CodeProject for a sample.