Developer forum

Forum » Dynamicweb 10 » Dynamicweb.Context.Current.Items different from IHttpContextAccessor.HttpContext.Items

Dynamicweb.Context.Current.Items different from IHttpContextAccessor.HttpContext.Items

Kevin Steffer
Kevin Steffer
Reply

Is that by purpose or is that a bug, that the two Items objects are different?

Test:

@{
  IHttpContextAccessor? accessor = DependencyResolver.Current.GetService(typeof(IHttpContextAccessor)) as IHttpContextAccessor;
  if (accessor != null) {
    accessor.HttpContext.Items["Test"] = "Test";
  }
}
<p>Test: @Dynamicweb.Context.Current.Items["Test"]</p>

// Result: <p>Test: </p>

 


Replies

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer

Hi Kevin,

It's a little bit of both. Specifically, it's related to the legacy context from .NET Framework.

IHttpContextAccessor.HttpContext.Items is of type IDictionary<object, object?> but the Items property on IContext is of type IDictionary. Since no direct type conversion exists between those two, we create a new Dictionary<object, object?> from HttpContext.Items to use in IContext.Items. That's why the two objects are different.

Originally, we did not intend that the Items collection should be accessed directly using the HttpContext, so it was fine to create a wrapper around it. However, that's no longer the case. The whole context concept could do with a update, but we can start here, since it can be fixed in a non-breaking way. While I don't think we can avoid having a wrapper around the collection, we can make sure that entries are added back to the underlying collection.

I've added a work item to make that change. You can follow along here: https://doc.dynamicweb.dev/documentation/fundamentals/dw10release/releasenotes/workItemInfo.html?workitemid=26090

- Jeppe

Votes for this answer: 1
 
Kevin Steffer
Kevin Steffer
Reply

Awesome Jeppe!

 

You must be logged in to post in the forum