I have an asp.net project that’s compiled on a server. On that same server I have a windows service that’s running methods referenced from the asp.net .dll.
Everything works fine except for when the httpcontext.current object needs to be accessed.
I’m fairly certain that the cause of the .current being null is that it’s referencing the shared library without an actual instance of the site running. In a previous version of the service I was able to find a workaround in asp.net by not using the context, however that is now a last resort as much of the architecture of the site has changed.
Is there anyway to create an instance of the site and simulate a current httpcontext for the purposes of the service?
The ASP.NET framework instantiates and manages the HTTP context and outside of ASP.NET, this will always be null. You’d have to dig into the framework to figure out how to create it, but honestly, it wouldn’t be worth it…
However, could you reconfigure your HttpContext references to point to HTTpcontextbase? If you did, HttpContextBase is a base class and can be inherited by anyone. Very easily, you could create a custom one to achieve what you want. You can even create your own static to wrap HttpContext.Current, refer to this static in the app, and to this static in the service (but have the service initially instantiate it).
HTH.