How to pass IOptions to an ASP.NET 5 middle-ware service? -


i have started playing around asp.net 5 vnext , struggling passing options config.json middle-ware service used webapi controller.

here snippet middle-ware service:

public class myservice : imyservice  {         public myservice(ioptions<myoptions> settings)         {             var o = settings.options;         } } 

here webapi controller using middle-ware service:

public class mycontroller : controller     {         private imyservice _myservice;          public testcontroller(imyservice service)         {             _myservice = service;         } } 

in startup.cs reading options:

services.addoptions(); services.configure<myoptions>(configuration); 

what struggling how register instance imyservice passed constructor of controller (how can hold of ioptions)?

services.addinstance<imyservice>(new myservice(xxxxx)); 

as suggested below did try use both

services.addtransient<myservice>(); 

and

services.addsingleton<myservice>(); 

but in both cases seeing following error:

an unhandled exception occurred while processing request.

invalidoperationexception: unable resolve service type 'myapp.services.imyservice' while attempting activate 'myapp.controllers.testcontroller'. microsoft.framework.dependencyinjection.activatorutilities.getservice(iserviceprovider sp, type type, type requiredby, boolean isdefaultparameterrequired)

thanks help!

don't register instance. instead add scoped/transient/singleton depending on requirements , let dependency injection magic;

services.configure<myoptions>(configuration.getsection("myoptions")); services.addscoped<imyservice, myservice>(); 

Comments