I have my own inherited App.Controller
from Mvc.Controller
which then all of my controllers inherit from. I wrote a provider utilizing an interface and implemented it as MyService
and the constructor takes the Server
property of Mvc.Controller
which is of HttpServerUtilityBase
.
However, I instantiate MyService
in App.Controller
's constructor. The problem is that the Server
property of the Controller is null
when constructing MyService
. I have used public Controller () : base() { }
to get the base to be constructed. However, Server
remains null
.
I would like to avoid Web.HttpContext.Current.Server
if possible.
Has any one have a work around for this problem?
Edit: Well, I have implemented tvanfosson's suggestion, and when my app constructs MyService
in the property get
method, Server
is still null.
Edit 2: Nevermind, I was a goof. I had another Controller
using Server
aswell and did not change that. Case closed.
There's your problem. You need to pass an instance of
MyService
which has already been constructed into yourApp.Controller
's constructor. Take a look at the Inversion of Control / Dependency Injection patterns, and take a look at some of the libraries which make these patterns easy (see this list).Use delayed initialization to construct your service.
Then, your service isn't actually instantiated until it is used in the controller action and by that time the Server property has been set.
Why do you need the Server reference? Are you doing stuff like url/html encoding? If so, you could use HttpUtility instead and get rid of the context reference entirely.
This is a very old question, but the subject is still relevant. So, one hint in 2017 that was possibly not available in 2009:
It is true that in the
Controller
constructorServer
is null. But you can use theOnActionExecuting
event:This works fine for me.
According to this site, if you have this Controller:
Then you want to initialize it this way: