Wednesday, November 4, 2015

MVC 6 and Dependency Injection on object with Parameters:

Sometimes things aren't obvious like this, which I spent a good portion of the day on.

In Startup.cs in method ConfigureServices(), 


   
            services.AddMvc();
            

            // Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
            // You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
            // services.AddWebApiConventions();

            // Register application services.
            services.AddTransient();
            services.AddTransient();
#if DNX451
           services.AddSingleton(provider => {
                var endPt = Configuration["Data:DocumentDbConnection:EndpointUrl"];
                var authKey = Configuration["Data:DocumentDbConnection:RWAuthKey"];
                var dbName = Configuration["Data:JobApp:DatabaseName"];
                var collName = Configuration["Data:JobApp:CollectionName"];
                return new JobAppDb(authKey, endPt, dbName, collName);
            }); 
#endif

No comments:

Post a Comment