https://github.com/tugberkugurlu/dotnetdoodle.owin.dependencies
An IoC container adapter into OWIN pipeline
https://github.com/tugberkugurlu/dotnetdoodle.owin.dependencies
Last synced: 5 months ago
JSON representation
An IoC container adapter into OWIN pipeline
- Host: GitHub
- URL: https://github.com/tugberkugurlu/dotnetdoodle.owin.dependencies
- Owner: tugberkugurlu
- License: mit
- Created: 2013-09-05T12:04:25.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-04-15T08:42:16.000Z (about 11 years ago)
- Last Synced: 2024-12-03T04:43:29.233Z (6 months ago)
- Language: C#
- Size: 1.17 MB
- Stars: 23
- Watchers: 10
- Forks: 13
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
DotNetDoodle.Owin.Dependencies
=================
An IoC container adapter into OWIN pipeline## Install Through NuGet
Core package which includes the interfaces and common extensions:
PM> Install-Package DotNetDoodle.Owin.Dependencies -pre
Autofac IoC container implementation:
PM> Install-Package DotNetDoodle.Owin.Dependencies.Autofac -pre
ASP.NET Web API adapter:
PM> Install-Package DotNetDoodle.Owin.Dependencies.Adapters.WebApi -pre
## Sample Snippet
Install the `DotNetDoodle.Owin.Dependencies.Adapters.WebApi`, `DotNetDoodle.Owin.Dependencies.Autofac` and `Autofac.WebApi5` NuGet packages.
### Startup.cs
public class Startup
{
public void Configuration(IAppBuilder app)
{
IContainer container = RegisterServices();
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("DefaultHttpRoute", "api/{controller}");app.UseAutofacContainer(container)
.Use()
.UseWebApiWithContainer(config);
}public IContainer RegisterServices()
{
ContainerBuilder builder = new ContainerBuilder();builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterOwinApplicationContainer();builder.RegisterType()
.As()
.InstancePerLifetimeScope();return builder.Build();
}
}### RandomTextMiddleware.cs
public class RandomTextMiddleware : OwinMiddleware
{
public RandomTextMiddleware(OwinMiddleware next)
: base(next)
{
}public override async Task Invoke(IOwinContext context)
{
IServiceProvider requestContainer = context.Environment.GetRequestContainer();
IRepository repository = requestContainer.GetService(typeof(IRepository)) as IRepository;if (context.Request.Path == "/random")
{
await context.Response.WriteAsync(repository.GetRandomText());
}
else
{
context.Response.Headers.Add("X-Random-Sentence", new[] { repository.GetRandomText() });
await Next.Invoke(context);
}
}
}### TextsController.cs
public class TextsController : ApiController
{
private readonly IRepository _repo;public TextsController(IRepository repo)
{
_repo = repo;
}public IEnumerable Get()
{
return _repo.GetTexts();
}
}