tags:

views:

3724

answers:

5

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

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>

However this only works for domain1, because you're only allowed one baseAddressPrefixFilter (they shouldn't call it baseAddressPrefixFilter**s** 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.

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

A: 

IIRC, you can have different prefixes - but only one per protocol - so you could have a tcp prefix, and perhaps an https prefix (need to check that one). However, you should also be able to give the full address on the service, rather than using the base-address and relative portion? You might need multiple endpoints, though.

Marc Gravell
Marc, what is the reasoning behind this "single prefix per protocol" limitation?
Stephen Newman
+2  A: 

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:

"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."

Update:

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"/&gt;
        <add baseAddress="http://domain2.com/Api"/&gt;
      </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.

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;
    }
}
Steve
A: 

I had a similar problem with load balancing a WCF service, maybe this will help you solve your problem.

Michael Kniskern
A: 

So Could you post some of the code that you used in your custom servicehostfactory? I've been struggling with the same problem for days. You seem to have figure things out but I still can't get beyond this problem. Thanks

Hey steve, just got your comment. I've updated the post to include the ServiceHostFactory class. I hope that helps.
Steve
A: 

My situation is slightly different.

Consider this case, I need to upload a file through http webservice. I have a webservice DocumentMediaService.svc and it's running on, let's say 4097 port. It has two methods bool CanPublish() and bool Publish(Stream stream)

I first call CanPublish() to see if I can publish. If I can, then I call Publish()

Now, the CanPublish() call would be a regular ws method call, however the Publish() should go through a StreamedHttp mode.

Is there a way to mix two such endpoints in the same service, and based on my call I can switch between end points?

I am new to WCF so bear with me.

Lakshmi Kumar