Search code examples
c#asp.net-web-api2owinappdomain

Multiple appdomains in Web API 2


I'm trying to move a Web project from an IIS hosting to a self hosting structure. I'm using Owin and Web API 2.

It appears that this code:

using (WebApp.Start<Startup>(url))
{
  bla...
}

creates a AppDomain (with ID == 1).

Edit: This API is a new interface for accessing data for an already existing software ("only" a few millions lines of code), which means that I can't really do what I want. A "user" is actually more of an "account", which means that I technically can have hundreds of clients connected as the same "user". End of edit

The problem I'm having is the following: when a client logs in, I create an AppDomain to load its static data. As the same account can have several sessions at the same time (or example the same user connected on his smartphone + computer), I have to make a difference between Session and AppDomain.

So, all my Controllers are called within the AppDomain of the user calling the API, all Services too.

BUT, the Web API 2 seems to add a serialization layer after each Controller. And this serialization layer occurs outside the scope of all the AppDomains I have created manually: it is in the AppDomain with ID == 1.

As the serialization sometimes needs access to the staticdata of the user, I end up having quite frequently serialization errors.

I'm looking for a way to either skip the additional serialization induced by Web API 2 or to compel this serialization to occur within a specific AppDomain.

I haven't found any hint for either ideas in the documentation of the .Net packages I'm using nor in the already asked questions.

Any idea (even a workaround) would be greatly appreciated =)

Thanks for any contribution ;)


Solution

  • The problem really came from a static class (thus defined at an AppDomain level) containing A LOT of information, and used in a few getters of properties throughout the code (I know it's a bad practice, but unfortunately I can't change that right now).

    The solution I finally come up with is to kind of serialize everything manually in the created AppDomains, thus solving the problem.