mercredi 3 avril 2019

Setup HttpApplicationState for WebForms project from Unit Test project

I am trying to call a method in a class in a WebForms ASP.net project. I need to add a value into the Application State, and I have tried:

HttpContext.Current = new HttpContext(new HttpRequest(null, "http://tempuri.org", null), new HttpResponse(null));

var cfg = new MyConfiguration();
HttpContext.Current.Application.Lock();

// Neither of these adds the item to the collection.
HttpContext.Current.Application.Add("MyConfiguration", cfg);
HttpContext.Current.Application["MyConfiguration"] = cfg;

// Looking in the debugger, there are 0 keys in the Application collection.

HttpContext.Current.Application.UnLock();
cfg = (MyConfiguration)HttpContext.Current.Application["MyConfiguration"];
Assert.IsNotNull(cfg);

Why won't the items add to the ApplicationState collection?

Thanks.

Aucun commentaire:

Enregistrer un commentaire