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?
As demonstrated in the Multiple Endpoints sample, a service can host multiple endpoints, each with different addresses and possibly also different bindings. This sample shows that it is possible to host multiple endpoints at the same address.
The service configuration has been modified to define two endpoints that support the ICalculator contract, but each at a different address using a different binding.
A service may have multiple endpoints within a single host, but every endpoint must have a unique combination of address, binding and contract.
The base address plays no role in the address. The actual endpoint address is http://localhost:8001/hello/servicemodelsamples . The fourth endpoint address specifies an absolute address and a different transport—TCP.
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"/>
<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.
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;
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With