Session variable equivalent in OWIN self-host
Asked Answered
Z

2

8

I have a sample web API hosted in an OWIN process (self hosted, not in IIS). I get a JWT token in my controller and I want to be able to retreive it in another part of the application, a class that implements NserviceBus IMutateOutgoingTransportMessages. In my other web application POC (hosted in IIS), I used a simple session variable and it works just fine. But I'd like to know what would be the best way to do it in my new OWIN self hosted environment ? Static property in static class ?

Zincography answered 18/12, 2014 at 15:16 Comment(1)
I'm currently facing the same need (session in Owin self hosted process to store some information).Balladeer
A
1

This question is really broad and difficult to answer without detailed knowledge of your specific needs. Here's my interpretation of your issue:

  • You're already signing each request, perhaps storing the token in the browser sessionStorage (or even localStorage), but this does not suffice
  • You need to retrieve the token outside of or not in relation to any request cycle (if not, this is probably where you should be looking for answers)
  • Your application does not need to be stateless

Just one static property for one token in a static class would of course start breaking as soon as more than one request hits the application at the same time. Implementing a class that maintains a list of tokens may be a solution, although I can't tell what key you should use to identify each token. Interface details would vary depending on things like if you need to retrieve the token more than once.

Thread safety issues would apply to all handling and implementation of such a class. Using Immutable Collections and functional programming practices as an inspiration may help.

If lingering tokens poses a problem (and they probably would from a security perspective, if nothing else), you need to figure out how to make sure that tokens do not outstay their welcome, even if the cycle is for some reason not completed.

Seeing how you used Session as a solution in your POC, I'm assuming you want some similar behavior, and that one user should not be allowed to carry two tokens at the same time. You could store the tokens i a database, or even in the local file system, making maintenance and validity a separate issue all together.

There are implementations of cache-like functionality already available for OWIN self-hosted applications, and maybe one of those would serve as a shortcut to implementing everything yourself.

If this token business in fact is the only reason for introducing state in your application, then the best solution IMHO would be to rethink your architecture so that the application can remain stateless.

Alyose answered 27/5, 2015 at 7:54 Comment(1)
As a matter of fact, it was just a test and we dropped the self hosted option. Thanks for your pointing me at the cache like fonctionnality in OWIN, I'll take a look at it. We are still evaluating the right way to do this and you are absolutely right about the application needs for remain stateless if possible.Zincography
B
0

I'm facing a similar dilemma on a server i'm currently developing for a customer. My problem is that the server must make calls (and retain a live connection) with a legacy, multithreaded DLL, (aka the SDK).

I struggled to get this working on IIS with a regular Web API project. Failed badly since IIS recycles threads when it determines that a thread is going rogue... witch is what the SDK thread looks like in that perspective. Also, the SDK must be able to callback on the caller (client - single page app) and for this I'm using SignalR.

I then tried a multi-part system (single page + web api on IIS + WCF service for the SDK integration). But it is a real nightmare to manage because of the 2 way async communication that must occur between all apps. Again: failure.

So I reverted to a single self hosted OWIN + WebAPI service in a console app (for now). My problem is that some of the calls are lengthy and are processed in a worker thread. I managed to pass the SignalR client id in each ajax calls via headers. I can extract the id when in web api controller. But when the task goes async, I need to get the id (via an Unity injected service) from the class that manages the async task. This is where my problem is similar to yours. In IIS hosted apps, we have HttpContext. It is contextualized on each client calls, and follows any thread changes in the pipeline... But not in self hosted OWIN WCF apps...

I'm looking into Thread Local Storage, CallContext... and other means of keeping track of the original caller info during the lifecycle of the async call. I have read about OWIN pipeline, I can capture the info in a OWIN middleware... but how to safely keep that info for use in injected services? I'm still searching for an answer...

I was wondering if you have found a solution to this rather interesting problem ?

I prefer adding to your thread rather than start another parallel thread / SO question.

Birdbath answered 14/1, 2017 at 15:57 Comment(1)
Unfortunately no. We decided to go with the IIS hosted OWIN instead of the self-hosted. But I would suggest you to take a look at the System.Runtime.CachingZincography

© 2022 - 2024 — McMap. All rights reserved.