How can WCF support multiple IIS Binding specified per site in WCF (.NET Framework 3.0 solution)

本文介绍如何在.NET Framework 3.0中通过扩展System.ServiceModel.Activation.ServiceHostFactory并提供自定义服务主机工厂来解决WCFCF多绑定问题。自定义服务主机工厂能够选择合适的基地址,从而在IIS中支持每个站点的多个绑定。

Supporting Multiple IIS Bindings Per Site


Ref: http://blogs.msdn.com/rampo/archive/2007/06/15/supporting-multiple-iis-bindings-per-site.aspx

MultiplBinding

If you see this error when you are hosting in IIS then most probably you have multiple IIS bindings per scheme in your web site. WCF in .Net Framework 3.0 ,out of the box, does not support multiple binding per site, but this can enabled by extending the System.ServiceModel.Activation.ServiceHostFactory,  and providing a CustomServiceHostFactory. CustomServiceHostFactory  picks the appropriate base address for the application.  The Factory attribute in the ServiceHost directive is set to the CustomSerivceHostFactory.

The only drawback is every developer hosting their application will need to change the svc file to provide the CustomServiceHostFactory, which picks the appropriate base address.

 

Sample Code

public class MultipleIISBindingSupportServiceHostFactory : ServiceHostFactory

{

protected override ServiceHost CreateServiceHost( Type serviceType, Uri [] baseAddresses)

 {

      Uri [] requiredAddress = GetAppropriateBase(baseAddresses);

       return base .CreateServiceHost(serviceType, requiredAddress);

 

 

   }

Uri [] GetAppropriateBase( Uri [] baseAddresses)

{

        List < Uri > retAddress = new List < Uri >();

        foreach ( Uri address in baseAddresses)

         {

            if (address.DnsSafeHost == "localhost" )

            {

               retAddress.Add(address);

             }

 

         }

return retAddress.ToArray();

 }

}

In the svc file, fill in the Factory attribute

<%@ServiceHost Factory="NS.MultipleIISBindingSupportServiceHostFactory" Service="CalculatorService" %>

转载于:https://www.cnblogs.com/javafun/archive/2008/03/20/1114084.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值