3

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.

5 Answers 5

8

Use delayed initialization to construct your service.

private MyService service;
public MyService Service
{
    get
    {
         if (this.service == null)
         {
             this.service = new MyService(this.Server);
         }
         return this.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.

Sign up to request clarification or add additional context in comments.

2 Comments

Or override Controller.Initialize, which fires at the right time.
The best part of delayed initialization is that it doesn't ever happen if the property is never accessed.
0

I instantiate MyService in App.Controller's constructor.

There's your problem. You need to pass an instance of MyService which has already been constructed into your App.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).

2 Comments

ASP.NET MVC is very pluggable, and you can either write your own ControllerFactory or use one from a library. I like Autofac, so I tell ASP.NET MVC to use Autofac's ControllerFactory. Autofac autowires the arguments to my controllers' constructors, and the constructors are free of all "dirtiness."
If constructing MyService requires Controller.Server then it can't be constructed before the controller you pass it to
0

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.

Comments

0

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 constructor Server is null. But you can use the OnActionExecuting event:

protected override void OnActionExecuting(ActionExecutingContext filterContext) 
{
    base.OnActionExecuting(filterContext);

    CurrentServer = Server; // CurrentServer is some instance variable I need later.
} 

This works fine for me.

Comments

0

According to this site, if you have this Controller:

public class MyController : Controller
{
  private string folderPath;

  public MyController()
  {       
    // Throws an error because Server is null
    folderPath = Server.MapPath("~/uploads"); 

    // Throws an error because this.ControllerContext is null
    folderPath = this.ControllerContext.HttpContext.Server.MapPath("~/uploads"); 
  }
}

Then you want to initialize it this way:

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
  base.Initialize(requestContext);

  // now Server has been initialized
  folderPath = Server.MapPath("~/uploads"); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.