-
-
Save stormwild/6e0d8a6ed8e584daaa390322a9df832e to your computer and use it in GitHub Desktop.
Microsoft.Extensions.DependencyInjection with OWIN Self-Hosted WebAPI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
using Microsoft.Owin.Hosting; | |
// ... | |
public class ServiceHost | |
{ | |
private IDisposable server = null; | |
const string baseAddress = "https://*:443"; | |
public void Start() | |
{ | |
server = WebApp.Start<WebApiStartup>(baseAddress); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WebApiStartup | |
{ | |
public void Configuration(IAppBuilder appBuilder) | |
{ | |
appBuilder.Map("/api", api => | |
{ | |
var config = new HttpConfiguration(); | |
config.MapHttpAttributeRoutes(); | |
// Add IoC | |
var serviceProvider = IocStartup.BuildServiceProvider(); | |
config.DependencyResolver = new DefaultDependencyResolver(serviceProvider); | |
// ... | |
api.UseCors(CorsOptions.AllowAll); | |
api.UseWebApi(config); | |
}); | |
// ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
using System.Web.Http.Dependencies; | |
using Microsoft.Extensions.DependencyInjection; | |
// ... | |
public class DefaultDependencyResolver : IDependencyResolver | |
{ | |
private readonly IServiceProvider provider; | |
public DefaultDependencyResolver(IServiceProvider provider) | |
{ | |
this.provider = provider; | |
} | |
public object GetService(Type serviceType) | |
{ | |
return provider.GetService(serviceType); | |
} | |
public IEnumerable<object> GetServices(Type serviceType) | |
{ | |
return provider.GetServices(serviceType); | |
} | |
public IDependencyScope BeginScope() | |
{ | |
return this; | |
} | |
public void Dispose() | |
{ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class IocStartup | |
{ | |
public static IServiceProvider BuildServiceProvider() | |
{ | |
var services = new ServiceCollection(); | |
// Register all dependent services | |
// | |
// IocSomeAssembly.Register(services); | |
// | |
// services.AddTransient<ISomething, Something>() | |
// For WebApi controllers, you may want to have a bit of reflection | |
var controllerTypes = Assembly.GetExecutingAssembly().GetExportedTypes() | |
.Where(t => !t.IsAbstract && !t.IsGenericTypeDefinition) | |
.Where(t => typeof(ApiController).IsAssignableFrom(t) | |
|| t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)); | |
foreach (var type in controllerTypes) | |
{ | |
services.AddTransient(type); | |
} | |
// It is only that you need to get service provider in the end | |
var serviceProvider = services.BuildServiceProvider(); | |
return serviceProvider; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment