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 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.
Any ideas on how to get a single service to work on 2 domains like this?
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.
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’