|
1 | 1 | # PosInformatique.AspNet.WebForms.DependencyInjection |
2 | 2 | PosInformatique.AspNet.WebForms.DependencyInjection is a library to add the IoC container support of Microsoft.Extensions.DependencyInjection for ASP .NET Web Forms |
| 3 | + |
| 4 | +## Installing from NuGet |
| 5 | +The **PosInformatique.AspNet.WebForms.DependencyInjection** is available directly on the |
| 6 | +[NuGet](https://www.nuget.org/packages/PosInformatique.AspNet.WebForms.DependencyInjection/) official website. |
| 7 | +To download and install the library to your Visual Studio project using the following NuGet command line |
| 8 | +``` |
| 9 | +Install-Package PosInformatique.AspNet.WebForms.DependencyInjection |
| 10 | +``` |
| 11 | + |
| 12 | +## Setting up |
| 13 | +After adding the **PosInformatique.AspNet.WebForms.DependencyInjection** package on your ASP .NET |
| 14 | +WebForms project call the `AddServiceCollection` the following lines in the constructor of your `HttpApplication` class in the |
| 15 | +`Global.asax.cs` code behind: |
| 16 | +```csharp |
| 17 | +public class Global : HttpApplication |
| 18 | +{ |
| 19 | + public Global() |
| 20 | + { |
| 21 | + ServicesConfig.RegisterServices(this.AddServiceCollection()); |
| 22 | + } |
| 23 | + |
| 24 | + void Application_Start(object sender, EventArgs e) |
| 25 | + { |
| 26 | + // Code that runs on application startup |
| 27 | + RouteConfig.RegisterRoutes(RouteTable.Routes); |
| 28 | + BundleConfig.RegisterBundles(BundleTable.Bundles); |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +In the `App_Start` folder add a new static class called `ServicesConfig` which allows to register |
| 34 | +the services using the `Microsoft.Extensions.DependencyInjection.ServiceCollection`: |
| 35 | +```csharp |
| 36 | +namespace PosInformatique.AspNet.WebForms.DependencyInjection.IntegrationTests |
| 37 | +{ |
| 38 | + using System; |
| 39 | + using System.Collections.Generic; |
| 40 | + using System.Collections.ObjectModel; |
| 41 | + using System.Linq; |
| 42 | + using Microsoft.Extensions.DependencyInjection; |
| 43 | + |
| 44 | + public static class ServicesConfig |
| 45 | + { |
| 46 | + public static void RegisterServices(ServiceCollection serviceCollection) |
| 47 | + { |
| 48 | + serviceCollection.AddSingleton<IDogRepository, DogRepository>(); |
| 49 | + serviceCollection.AddTransient<IDogManager, DogManager>(); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | +You can register services with a *Transient* or *Singleton* scope. Unlike to ASP .NET Core, |
| 55 | +**PosInformatique.AspNet.WebForms.DependencyInjection** does not support *Scoped* scope services |
| 56 | +which exists during the HTTP request lifetime. |
| 57 | + |
| 58 | +## Contributions |
| 59 | +Do not hesitate to clone my code and submit some changes... |
| 60 | +It is a open source project, so everyone is welcome to improve this library... |
| 61 | +By the way, I am french... So maybe you will remarks that my english is not really fluent... |
| 62 | +So do not hesitate to fix my resources strings or my documentation... Merci ! |
| 63 | + |
| 64 | +## Thanks |
| 65 | +I want to thank the [DiliTrust](https://www.dilitrust.com/) company to test and gave me their |
| 66 | +feedback of this library for their ASP .NET WebForms applications. |
0 commit comments