Code Monkey home page Code Monkey logo

lightinject.microsoft.hosting's Introduction

This package enables LightInject to be used as the IoC container in a Asp.Net Core application.

Installing

dotnet add package LightInject.Microsoft.Hosting

Usage

public static IHostBuilder CreateHostBuilder(string[] args) =>
	Host.CreateDefaultBuilder(args)
  	.UseLightInject(services => services.RegisterFrom<CompositionRoot>())
    .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
              

Or from ASPNET 6 and onwards with minimal Program.cs setup:

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseLightInject(sr => sr.RegisterFrom<CompositionRoot>());

The CompositionRoot class is usually placed in the host project (the project containing Program.cs) and is used to register services that are specific to LightInject

public class HostCompositionRoot : ICompositionRoot
{
	public void Compose(IServiceRegistry registry)
	{
		registry.RegisterScoped<IFoo, Foo>();			
	}
}

LifeTime

In general we could say that there are usually just two different lifetimes used in web applications. Services are either Scoped or Singletons. Scoped services are services that lives for the lifetime of the web request. An example would IDbConnection which gets created when the scope starts (web request start) and disposed when the scope ends (web request end). We register these services using the RegisterScoped method. Scoped services are only created once per scope even if injected into several other services during the web request. Singleton services are shared across web request and must be 100% thread safe. An example here could be a cache that is used for all web requests.

lightinject.microsoft.hosting's People

Contributors

seesharper avatar andmos avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.