{"action":"opened","issue":{"url":"https://api.github.com/repos/simpleinjector/SimpleInjector/issues/351","repository_url":"https://api.github.com/repos/simpleinjector/SimpleInjector","labels_url":"https://api.github.com/repos/simpleinjector/SimpleInjector/issues/351/labels{/name}","comments_url":"https://api.github.com/repos/simpleinjector/SimpleInjector/issues/351/comments","events_url":"https://api.github.com/repos/simpleinjector/SimpleInjector/issues/351/events","html_url":"https://github.com/simpleinjector/SimpleInjector/issues/351","id":196170230,"number":351,"title":"How do I inject dependencies into a custom WebHostService in a .Net Core project?","user":{"login":"necoras","id":14185060,"avatar_url":"https://avatars.githubusercontent.com/u/14185060?v=3","gravatar_id":"","url":"https://api.github.com/users/necoras","html_url":"https://github.com/necoras","followers_url":"https://api.github.com/users/necoras/followers","following_url":"https://api.github.com/users/necoras/following{/other_user}","gists_url":"https://api.github.com/users/necoras/gists{/gist_id}","starred_url":"https://api.github.com/users/necoras/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/necoras/subscriptions","organizations_url":"https://api.github.com/users/necoras/orgs","repos_url":"https://api.github.com/users/necoras/repos","events_url":"https://api.github.com/users/necoras/events{/privacy}","received_events_url":"https://api.github.com/users/necoras/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-12-16T22:40:22Z","updated_at":"2016-12-16T22:40:22Z","closed_at":null,"body":"Before we get started, I'm reproducing my question here pretty much wholesale from my [Stack Overflow question](http://stackoverflow.com/questions/41191543/how-do-i-inject-dependencies-into-a-custom-webhostservice-in-a-net-core-project), so feel free to answer it there as well for additional internet points.\r\n\r\nI'm trying to create a service which will run as a Windows service as described [here](http://stackoverflow.com/questions/37346383/hosting-asp-net-core-as-windows-service). My problem is that the example Web Host Service constructor only takes an `IWebHost` parameter. My service needs a constructor more like this:\r\n\r\n    public static class HostExtensions\r\n    {\r\n        public static void RunAsMyService(this IWebHost host)\r\n        {\r\n            var webHostService = new MyService(host, loggerFactory, myClientFactory, schedulerProvider);\r\n            ServiceBase.Run(webHostService);\r\n        }\r\n    }\r\n\r\nMy `Startup.cs` file looks similar to this:\r\n\r\n    public Startup(IHostingEnvironment env)\r\n    {\r\n        var builder = new ConfigurationBuilder()\r\n            .AddJsonFile(\"appsettings.json\")\r\n            .AddEnvironmentVariables()\r\n            .AddInMemoryCollection();\r\n\r\n        this.Configuration = builder.Build();\r\n    }\r\n\r\n    public void ConfigureServices(IServiceCollection services)\r\n    {\r\n        services.AddOptions();\r\n        this.container.RegisterSingleton<IConfiguration>(this.Configuration);\r\n        services.AddSingleton<IControllerActivator>(new SimpleInjectorControllerActivator(container));\r\n    }\r\n\r\n    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)\r\n    {\r\n        app.UseSimpleInjectorAspNetRequestScoping(this.container);\r\n\r\n        this.container.Options.DefaultScopedLifestyle = new AspNetRequestLifestyle();\r\n\r\n        this.InitializeContainer(app, loggerFactory);\r\n        this.container.Verify();\r\n\r\n        if (env.IsDevelopment())\r\n        {\r\n            app.UseDeveloperExceptionPage();\r\n        }\r\n    }\r\n\r\n    private void InitializeContainer(IApplicationBuilder app, ILoggerFactory loggerFactory)\r\n    {\r\n        this.container.Register(() => loggerFactory, Lifestyle.Singleton);\r\n        this.container.Register<IMyClientFactory>(() => new MyClientFactory(), Lifestyle.Transient);\r\n        this.container.Register<ISchedulerProvider>(() => new SchedulerProvider(), Lifestyle.Transient);\r\n    }\r\n\r\nObviously I'm using SimpleInjector as a DI container. It's registered with the `IServiceCollection` as detailed in [the documentation](http://simpleinjector.readthedocs.io/en/latest/aspnetintegration.html).\r\n\r\nMy question is how do I access the framework's container (the IServicesCollection) in the HostExtensions class so that I can inject the necessary dependencies into `MyService`? For MVC controllers that's all just  handled under the covers, but I don't know of any documentation detailing how to access it where needed elsewhere.\r\n\r\nAm I trying to do something that Simple Injector doesn't support? Or is there just some obvious piece of plumbing that I'm missing?"}}
{"actor":{"display_login":"necoras"}}