c# - web api self host custom ioc inject data to controllers -


i have property in web api self hosted app inject controllers, loaded via reflection using custom ioc framework, here startup code:

public customclass stuffinstance { get; set; }   // method required katana: public void configuration(iappbuilder app) {     configureoauth(app);     var webapiconfiguration = configurewebapi();      // use extension method provided webapi.owin library:     app.usewebapi(webapiconfiguration); } 

my controllers scaffolded , like:

// put: api/eventtypedescriptions/5 [responsetype(typeof(void))] public ihttpactionresult putstuff(int id, int something) {     //do stuff     //here use stuffinstance singleton     return statuscode(httpstatuscode.nocontent); }   

how can inject stuffinstance controllers? information relevant making ioc framework btw

since mentioned autofac potential candidate, recommend follow tutorial on webapi integration. you'll need define interface on customclass can inject it.

you'll need inject instance you've created (since want treat singleton) registering instance component.

public interface icustomclass {} public class customclass : icustomclass {}  public customclass _stuffinstance = new customclass();  public class startup {     public void configuration(iappbuilder app)     {         configureoauth(app);          var builder = new containerbuilder();         var config = new httpconfiguration();          builder.registerapicontrollers(assembly.getexecutingassembly());          builder.registerinstance(_stuffinstance).as<icustomclass>();          var container = builder.build();         config.dependencyresolver = new autofacwebapidependencyresolver(container);          app.useautofacmiddleware(container);         app.useautofacwebapi(config);         app.usewebapi(config);     } } 

then, in each controller's constructor, inject instance that's been bound appropriate interface.

public class customcontroller : apicontroller {     private readonly icustomclass _customclass;      public customcontroller(icustomclass customclass)     {         _customclass = customclass;     }     } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -