开发者

WCF set bindings on service at runtime

开发者 https://www.devze.com 2022-12-26 07:16 出处:网络
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 t

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 serve开发者_运维百科r side).


Yes, 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消