Last active
April 16, 2024 15:58
-
-
Save andriybuday/a96bfc7845ba1514561a4e6f146a629d to your computer and use it in GitHub Desktop.
Microsoft.Extensions.DependencyInjection with OWIN Self-Hosted WebAPI
This file contains 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 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 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 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; | |
} | |
} |
Without addition probosed by @UnknownAngel the server will leak memory.
Since this pattern maybe used together with MVC, here the small modification for making similar code working with MVC
kant2002/MvcDIMemoryLeak@b6984d2
Without addition probosed by @UnknownAngel the server will leak memory.
could you please point out which part would cause the memory leakage. What is proposed by @UnknownAngel? I see b6984d2 but im unable to figure out what it exactly solves!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you for your example.
if you add
`
class DependencyScope : IDependencyScope
{
private readonly IServiceScope _scope;
}
`
and return in DefaultDependencyResolver
public IDependencyScope BeginScope() { return new DependencyScope(provider); }
you have also full support for scopes