Geeks With Blogs
Nilesh Baviskar

Requirement was to create Multi-tenant WCF Service for custom development using WCF and Dynamics CRM 2011. There is just one SVC file but multiple URLs to access it e.g. in Dyanamics Organization Org1 have URL http://CRMserver_name/Org1/XRMServices/2011/Organization.svc and Org2 have http://CRMserver_name/Org2/XRMServices/2011/Organization.svc

I was trying this using URL Routing but problem is, it is creating REST services which we don’t want. We should be able to access these services just like normal WCF service. So if we add the URL http://CRMserver_name/Org1/XRMServices/2011/Organization.svc in WCF test client it should work.

After some research I’m going with safe way to achieve Multi-tenant WCF Services. I’m going to create separate WCF projects for each tenant and host them in separate IIS virtual dirs.

e.g. For Tenant1 - http://localhost/ Tenant1/Service1.svc and for Tenant2 - http://localhost/ Tenant2/Service1.svc
Note that SVC name is same. Then I’m removing IService1.cs from Tenant2 and adding existing file as link from Tenant1. So my solution explorer looks like following.

Solution Explorer

Here WcfService3 is for Tenant1 and WcfService1 is for Tenant2 (Sorry for this confusing names. I might fix it if someone need). So my WcfService3 properties look like following.

Project Prop

Now for both http://localhost/ Tenant1/Service1.svc and http://localhost/ Tenant2/Service1.svc service code is at single place which is in WcfServices3->Service1.svc.cs. Following code will create the database connection object for specific method call.

private static string dbServerName = "YourDBServerName";
        private static SqlConnection dbConnection = createDBConnection();
        private static SqlConnection createDBConnection()
            Uri fullUri = OperationContext.Current.IncomingMessageHeaders.To;
            Uri baseAddress = new Uri(@"http://localhost");
            UriTemplate template = new UriTemplate(@"/{orgName}/Service1.svc");
            // retrieve the value of the artist segment
            UriTemplateMatch match = template.Match(baseAddress, fullUri);
            String orgName = match.BoundVariables["orgName"];       
            SqlConnection objConnection = new SqlConnection();
            objConnection.ConnectionString = createConnString(orgName);
            return objConnection;
        private static string createConnString(string orgName)
            return (String.Format("Data Source={0};Initial Catalog={1}_MSCRM;User Id=UserId1;Password=PasswordForUserId1;", dbServerName, orgName));
Hope this helps someone.
Posted on Wednesday, August 24, 2011 3:56 AM | Back to top

Comments on this post: Multi-tenant WCF Services

# re: Multi-tenant WCF Services
Requesting Gravatar...
Hello, it's very interesting. Have you tried to use DataService/RIA the same way? Should some CRM handlers be "disabled" in the virtual folders?
Left by Pavel on Mar 27, 2012 12:54 AM

# re: Multi-tenant WCF Services
Requesting Gravatar...
Thanks Pavel,

I've not tried with Data or RIA services but I think it should still work the same way.

We don't have to do anything else other than mentioned steps. Are you getting any error?

Left by Nilesh on Apr 15, 2012 8:15 AM

# re: Multi-tenant WCF Services
Requesting Gravatar...
I've posted what I think is a decent alternative solution to

In my solution, it doesn't require multiple WCF projects, instead working with either host headers or URL Rewrite rules to pick up the tenant ID from the HttpRequest header fields.

Hope it helps,
Left by Neil O'Connor on Dec 20, 2012 5:10 PM

Your comment:
 (will show your gravatar)

Copyright © Nilesh | Powered by: