In our MVC 5 site, no session, we need to get/generate a unique ID per request. THhis will be used as an ID for logging all activity in the request.
Is there a way to assign/get a value to a Request to enable this?
In our MVC 5 site, no session, we need to get/generate a unique ID per request. THhis will be used as an ID for logging all activity in the request.
Is there a way to assign/get a value to a Request to enable this?
Here's some extension methods that I created for this purpose:
Update
@Luiso made a comment about interned strings not being garbage collected. This is a good point. In a very busy web app that doesn't recycle app pools frequently enough (or ever) this would be a serious issue.
Thankfully, recent versions of .NET have ephemeron support via the
ConditionalWeakTable<TK,TV>
class. Which gives a convenient and memory safe way of tackling this problem:My Overby.Extensions.Attachments package has an extension method that simplify things.
Using that extension method, you would just call
httpContext.GetReferenceId()
.Add it to the request item collection odetocode.com/articles/111.aspx
Will generate a unique id.
http://msdn.microsoft.com/en-us/library/system.guid.newguid(v=vs.110).aspx