How to use Ninject to insert a static property?

I am using ASP.NET MVC 2 to implement a web service, and I have a custom class JsonResult:

public abstract class JsonResult : ActionResult
{
    public static ISerializer Serializer { get; set; }

    public override void ExecuteResult(ControllerContext context)
    {
        var json = Serializer.Serialize(this);
        context.HttpContext.Response.Write(json);
    }
}

JsonResultis an abstract base class for all results that need to be serialized into JSON data. It uses to perform serialization ISerializer.

I am using Ninject as an IoC container. However, I'm not sure how I should add dependency ISerializer. I originally did this:

var kernel = new StandardKernel().Bind<ISerializer>().To<JsonNetSerializer>();
JsonResult.Serializer = kernel.Get<ISerializer>();

But something about it just doesn't seem right. So how can I enter a property correctly Serializer? I want to enter it only once when the application starts.

+3
1

, MVC , - , static, JsonNetSerializer JsonResult? , , ISerializer ( ) static ( ).

+4

Source: https://habr.com/ru/post/1775935/


All Articles