单个WCF服务中的多个基地址 [英] Multiple Base Addresses in single WCF Service

查看:96
本文介绍了单个WCF服务中的多个基地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个环境,其中在同一服务器上托管的多个站点将使用单个服务进行呼叫。例如:

I have an environment where multiple sites hosted on the same server will use a single service to make its calls. For example:

http://domain1.com /Api/Service.svc

http://domain2.com/Api/Service.svc

Api应用程序已设置为映射到同一物理站点的每个站点中的虚拟目录目录,因此源仅位于一个位置。问题在于WCF不喜欢为其服务端点使用多个基址。为了使该服务完全正常运行,我必须添加一个基地址前缀过滤器:

The Api application has been setup as a virtual directory in each site mapped to the same physical directory, so that the source is only located in one place. The problem is that WCF doesn't like having multiple base addresses for its service endpoints. To get the service to work at all, I had to add a base address prefix filter:

<serviceHostingEnvironment>      
  <baseAddressPrefixFilters>        
    <add prefix="http://domain1.com/Api" />
    <!--<add prefix="http://domain2.com/Api" />-->
  </baseAddressPrefixFilters>
</serviceHostingEnvironment>

但这仅适用于domain1,因为只允许使用一个baseAddressPrefixFilter(它们不应调用如果只允许使用baseAddressPrefixFilter s 。我尝试构建一个自定义ServiceHostFactory来解决该问题,但是在激活过程中调用ServiceHostFactory之前遇到了过滤器问题。

However this only works for domain1, because you're only allowed one baseAddressPrefixFilter (they shouldn't call it baseAddressPrefixFilters if you're only allowed one). I tried building a custom ServiceHostFactory to get around it, but I run into the filter problem before the ServiceHostFactory is called in the Activation process.

关于如何使单个服务在2个域上运行的任何想法?

Any ideas on how to get a single service to work on 2 domains like this?

推荐答案

好吧,将整个URL放入端点地址是我从未想到的事情,因此这使我进入了某个地方。使用自定义ServiceHostFactory后,该方法适用于domain1,但不适用于domain2。我收到了一个以前从未见过的新错误消息:

Ok, putting the whole URL in the endpoint address was something I hadn't thought of, so that's getting me somewhere. After using the custom ServiceHostFactory, that worked for domain1, but not for domain2. I got a new error message I haven't seen before:

没有协议绑定与给定地址' http://domain2.com/Api/Poll.svc/soap 。协议绑定是在IIS或WAS配置中的站点级别配置的。

"No protocol binding matches the given address 'http://domain2.com/Api/Poll.svc/soap'. Protocol bindings are configured at the Site level in IIS or WAS configuration."

更新:

好,我知道了(最后!)。我可以将主机节点添加到服务定义中,并避免在每个端点中使用绝对URL。我还删除了BaseAddressPrefixFilter,但将自定义ServiceHostFactory保留在解决方案中。

Ok, I figured it out (finally!). I can add a host node to the service definition and avoid using absolute urls in each endpoint. I also removed the BaseAddressPrefixFilter, but kept the custom ServiceHostFactory in the solution.

  <service name="Poll">
    <host>
      <baseAddresses>
        <add baseAddress="http://domain1.com/Api"/>
        <add baseAddress="http://domain2.com/Api"/>
      </baseAddresses>
    </host>
    <endpoint address="soap" binding="basicHttpBinding" bindingConfiguration="soapBinding"
      contract="IPoll" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />        
  </service>

我担心我将不得不为每个绑定的每个域编写一个终结点,这会已经有很多多余的配置要管理。此解决方案很棒,因为我不必这样做,它更加简洁。

I was worried I was going to have to write an endpoint for each domain for each binding, which would have been a lot of excess config to manage. This solution is great because I don't have to do that, its a little more concise.

作为参考,这是我的ServiceHostFactory类。它非常简单,但这是必需的。一旦有了这个,您还必须修改.svc文件的标记以包括Factory:Factory = Api.ServiceHostFactory

For reference, here's my ServiceHostFactory class. Its pretty simple, but it is required. Once you have this, you also have to modify the markup of your .svc file to include the Factory: Factory="Api.ServiceHostFactory"

   public class MyServiceHostFactory : System.ServiceModel.Activation.ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {                        
        ServiceHost host;

        host = new ServiceHost(serviceType, baseAddresses[0]);

        return host;
    }
}

这篇关于单个WCF服务中的多个基地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆