1

I am trying to implement multitenancy in a WCF application but facing some issues.

So I already have Autofac configured in the app and Autofac also supports multitenancy with WCF

Now my scenario is little different. I have a mobile app that's consuming this WCF service. And the service is deployed on Azure and we've front door configured so I can use X-Forwarded-Host header to extract the tenant information so that's not a problem.

Now the main issue is I have a TenantService that I need to setup for each request and then I can use the same TenantService wherever I want within the request scope.

I also implemented multitenancy in one of the MVC app and WebApi. But there we've an option to implement OwinMiddleware where we can setup the TenantService and that remains tenant aware within the request scope.

In Wcf I tried adding a MessageInspector and IServiceBehavior but the Bahavior created only once and it acts like a singleton so if I inject TenantService in my Behavior and pass that to the MessageInspector then it always have the same copy of the TenantService which I don't want.

I want to use something similar to the owin middleware but in WCF. What are the best options?

Ask
  • 3,076
  • 6
  • 30
  • 63
  • What version of .NET? The .NET Framework or .NET core? How do you pass tenant information? – Wiktor Zychla Oct 21 '22 at 12:56
  • .net framework 4.8 – Ask Oct 21 '22 at 13:10
  • https://www.wiktorzychla.com/2014/08/dynamic-wcf-routing-and-easy-upgrading.html – Wiktor Zychla Oct 21 '22 at 13:11
  • @WiktorZychla, thanks for sharing the article. I am still unsure if it can benefit in my scenario where I need to setup TenantService – Ask Oct 21 '22 at 13:43
  • Not sure about this, too. Never used Autofac's extensions for multitenancy and if it's not pluggable (so that you can make it respect the tenat name from the RouteData) you can have a hard time combining the two. – Wiktor Zychla Oct 21 '22 at 14:20
  • Can you explain the usage of ServiceHostFactory that you created for dealing with the multitenancy? I am not very familiar with the WCF as I usually work on other .net stuff. What if we don't have that? – Ask Oct 21 '22 at 14:25
  • It's just a factory of instances. It's the part of WCF infrastructure. A piece of the whole puzzle there. – Wiktor Zychla Oct 21 '22 at 15:51
  • Did you [look at the example](https://github.com/autofac/Examples/tree/master/src/MultitenantExample.WcfService)? – Travis Illig Oct 23 '22 at 00:48
  • @TravisIllig, yes. That's the same I mentioned in my question while my use case is different. – Ask Oct 24 '22 at 06:08
  • You can see these two cases, one is the application of [PerCall](https://stackoverflow.com/questions/1431180/wcf-service-which-creates-a-new-thread-for-every-new-request), the other is the [implementation of the contract](https://stackoverflow.com/questions/10657078/wcf-implementing-a-servicehostfactory-to-use-into-iis-context-svc-file). Hope it helps. – Jiayao Oct 24 '22 at 08:23

0 Answers0