{"id":19099757,"url":"https://github.com/PosInformatique/PosInformatique.AspNet.WebForms.DependencyInjection","last_synced_at":"2025-04-18T17:31:57.240Z","repository":{"id":35128023,"uuid":"210191144","full_name":"PosInformatique/PosInformatique.AspNet.WebForms.DependencyInjection","owner":"PosInformatique","description":"PosInformatique.AspNet.WebForms.DependencyInjection is a library to add the IoC container support of Microsoft.Extensions.DependencyInjection for ASP .NET Web Forms","archived":false,"fork":false,"pushed_at":"2022-12-07T23:29:51.000Z","size":698,"stargazers_count":7,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-03T06:42:43.430Z","etag":null,"topics":["aspnet","dependencyinjection","ioc","netstandard","webforms"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PosInformatique.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-22T17:59:41.000Z","updated_at":"2023-08-22T16:37:13.000Z","dependencies_parsed_at":"2023-01-15T14:23:18.392Z","dependency_job_id":null,"html_url":"https://github.com/PosInformatique/PosInformatique.AspNet.WebForms.DependencyInjection","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PosInformatique%2FPosInformatique.AspNet.WebForms.DependencyInjection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PosInformatique%2FPosInformatique.AspNet.WebForms.DependencyInjection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PosInformatique%2FPosInformatique.AspNet.WebForms.DependencyInjection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PosInformatique%2FPosInformatique.AspNet.WebForms.DependencyInjection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PosInformatique","download_url":"https://codeload.github.com/PosInformatique/PosInformatique.AspNet.WebForms.DependencyInjection/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223783108,"owners_count":17201903,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aspnet","dependencyinjection","ioc","netstandard","webforms"],"created_at":"2024-11-09T03:52:15.238Z","updated_at":"2024-11-09T03:52:18.993Z","avatar_url":"https://github.com/PosInformatique.png","language":"JavaScript","funding_links":[],"categories":["aspnet"],"sub_categories":[],"readme":"# PosInformatique.AspNet.WebForms.DependencyInjection\nPosInformatique.AspNet.WebForms.DependencyInjection is a library to add the IoC container support of Microsoft.Extensions.DependencyInjection for ASP .NET Web Forms\n\n## Installing from NuGet\nThe\n[PosInformatique.AspNet.WebForms.DependencyInjection](https://www.nuget.org/packages/PosInformatique.AspNet.WebForms.DependencyInjection/)\nis available directly on the\n[NuGet](https://www.nuget.org/packages/PosInformatique.AspNet.WebForms.DependencyInjection/) official website.\nTo download and install the library to your Visual Studio project using the following NuGet command line \n```\nInstall-Package PosInformatique.AspNet.WebForms.DependencyInjection\n```\n\n## Setting up\nAfter adding the\n[PosInformatique.AspNet.WebForms.DependencyInjection](https://www.nuget.org/packages/PosInformatique.AspNet.WebForms.DependencyInjection/)\npackage on your ASP .NET\nWebForms project call the `AddServiceCollection` the following lines in the ``Application_Start`` of your `HttpApplication` class in the\n`Global.asax.cs` code behind:\n```csharp\npublic class Global : HttpApplication\n{\n    protected void Application_Start(Object sender, EventArgs e)\n    {\n        ServicesConfig.RegisterServices(this.AddServiceCollection());\n\n        // Code that runs on application startup\n        RouteConfig.RegisterRoutes(RouteTable.Routes);\n        BundleConfig.RegisterBundles(BundleTable.Bundles);\n    }\n}\n```\n\nIn the `App_Start` folder add a new static class called `ServicesConfig` which allows to register\nthe services using the `Microsoft.Extensions.DependencyInjection.ServiceCollection`:\n```csharp\nnamespace PosInformatique.AspNet.WebForms.DependencyInjection.IntegrationTests\n{\n    using System;\n    using System.Collections.Generic;\n    using System.Collections.ObjectModel;\n    using System.Linq;\n    using Microsoft.Extensions.DependencyInjection;\n\n    public static class ServicesConfig\n    {\n        public static void RegisterServices(IServiceCollection serviceCollection)\n        {\n            serviceCollection.AddSingleton\u003cIDogRepository, DogRepository\u003e();\n            serviceCollection.AddTransient\u003cIDogManager, DogManager\u003e();\n        }\n    }\n}\n```\nYou can register services with a *Transient* or *Singleton* scope. Unlike to ASP .NET Core,\n[PosInformatique.AspNet.WebForms.DependencyInjection](https://www.nuget.org/packages/PosInformatique.AspNet.WebForms.DependencyInjection/)\ndoes not support *Scoped* scope services\nwhich exists during the HTTP request lifetime.\n\n## Setting up using an existing IServiceProvider/IServiceCollection\nIf in your ASP .NET application you host the ASP .NET Core infrastructure (using the\n[PosInformatique.AspNetCore.Server.AspNet](https://github.com/PosInformatique/PosInformatique.AspNetCore.Server.AspNet)\nlibrary for example), you can reuse the ``IServiceProvider`` and ``IServiceCollection``\ninternally builded by the ASP .NET Core infrastructure to share the same services\n(and same singleton instances !) between your ASP .NET application and ASP .NET Core hosted application.\n\nThe following example, show how to reuse the internal ``IServiceProvider`` and ``IServiceCollection``\nof ASP .NET Core application hosted in ASP .NET using the\n[PosInformatique.AspNetCore.Server.AspNet](https://github.com/PosInformatique/PosInformatique.AspNetCore.Server.AspNet)\nlibrary:\n\n```csharp\npublic class Global : System.Web.HttpApplication\n{\n    protected void Application_Start(object sender, EventArgs e)\n    {\n        var host = WebHost.CreateDefaultBuilder()\n            .UseAspNet(options =\u003e\n            {\n                options.Routes.Add(\"api\");\n                options.Routes.Add(\"swagger\");\n            })\n            .ConfigureServices(services =\u003e\n            {\n                // Add the default ASP .NET non-core services\n                // in the IServiceCollection of ASP .NET core.\n                services.AddDefaultAspNetServices(this);\n            })\n            .UseStartup\u003cStartup\u003e()\n            .Start();\n\n        // Reuse the built IServiceProvider of ASP .NET Core WebHost inside\n        // the ASP .NET non-core infrastructure to use IoC feature.\n        this.UseServiceProvider(host);\n    }\n}\n```\n\nThe ``AddDefaultAspNetServices()`` method allows to add ASP .NET non-core infrastructure\nservice like the ``HttpRequest`` or ``HttpContext`` into the ``IServiceCollection`` which\nwill be available in the ``IServiceProvider`` built by the ASP .NET core infrastructure.\n\nThe ``UseServiceProvider()`` method allows to setup the ASP .NET non-core infrastructure\nto use the implementation of ``IServiceProvider`` which is builted by the ``IWebHost``\nof the ASP .NET Core infrastructure started.\n\nWith this approach the ASP .NET non-core and Core components will share the **same** service.\nFor example, if you register a ``IDogManager`` service as singleton in the services of ASP .NET Core,\nthe ``IDogManager`` service instance will be available and **the same instance**:\n- In the constructors of the ASP .NET Web Forms pages, user controls,...\n- In the constructors of the ASP .NET Core controllers\n- And so on...\n\n## Add the support of [ActivatorUtilitiesConstructor] attribute\n\nBy default, the\n[Microsoft.Extensions.DependencyInjection](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection/)\ndependency injection container will use any\nconstructor which match the parameters of the control, or service to instantiate.\nIn the version **2.1** of\n[Microsoft.Extensions.DependencyInjection](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection/)\nlibrary, Microsoft has introduced a new\n[ActivatorUtilitiesConstructorAttribute](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.activatorutilitiesconstructorattribute)\nattribute which allows to force the `IServiceProvider` of the\n[Microsoft.Extensions.DependencyInjection](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection/)\nlibrary\nto call explicitely the constructor decorated.\n\nThe version **1.2.0** of the\n[PosInformatique.AspNet.WebForms.DependencyInjection](https://www.nuget.org/packages/PosInformatique.AspNet.WebForms.DependencyInjection/)\nlibrary add the support of the\n[ActivatorUtilitiesConstructorAttribute](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.activatorutilitiesconstructorattribute)\nattribute even technically this attribute is not added by the ASP .NET Compiler on the generated ASPX and ASCX views.\n\nFor example, imagine that you have the following user control:\n````charp\npublic partial class UserControlWithDependency : System.Web.UI.UserControl\n{\n    private readonly IDogManager dogManager;\n\n    [ActivatorUtilitiesConstructor]   // Will be call by the IServiceProvider of the Microsoft.Extensions.DependencyInjection library.\n    public UserControlWithDependency(IDogManager dogManager)\n    {\n        this.dogManager = dogManager;\n    }\n\n    public UserControlWithDependency()\n    {\n    }\n}\n````\n\nWhen the application is loaded the ASP .NET Compiler generate the following code which will not add the\n[ActivatorUtilitiesConstructorAttribute](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.activatorutilitiesconstructorattribute)\nattribute on the constructors associated:\n````charp\npublic class usercontrolwithdependency_ascx : UserControlWithDependency\n{\n\t...\n\n\t[DebuggerNonUserCode]\n\tpublic usercontrolwithdependency_ascx()\n\t{\n\t\t__Init();\n\t}\n\n\t[DebuggerNonUserCode]\n\tpublic usercontrolwithdependency_ascx(IDogManager dogManager)\n\t\t: base(dogManager)\n\t{\n\t\t__Init();\n\t}\n\n    ...\n}\n````\n\nThe previous generated code can not be used correctly by the\n[Microsoft.Extensions.DependencyInjection](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection/)\nlibrary to call the right constructor based on the\n[ActivatorUtilitiesConstructorAttribute](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.activatorutilitiesconstructorattribute).\n\nIn the version **1.2.0**, the\n[PosInformatique.AspNet.WebForms.DependencyInjection](https://www.nuget.org/packages/PosInformatique.AspNet.WebForms.DependencyInjection/)\nadd the full support of the\n[ActivatorUtilitiesConstructorAttribute](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.activatorutilitiesconstructorattribute)\nby *virtually* add the attribute by reflection in the class generated by the ASP .NET Compiler.\n\n## Issue in the ASPX/ASCX designer\n\nWhen changing the constructor of the user controls and control to inject dependencies using the\n[HttpRuntime.WebObjectActivator](https://docs.microsoft.com/fr-fr/dotnet/api/system.web.httpruntime.webobjectactivator),\nthe following message can appear when adding these controls to a page or other user controls:\n\n\u003e *Element 'xxxxx' is not a known element. This can occur if there is a compilation error in the Web site, or the web.config\nfile is missing*.\n\n![Visual Studio Designer Issue](Documentation/VisualStudioDesignerIssue.png)\n\nIn this case, lot of *fakes* errors/warnings can be raised by Visual Studio (but the application can compile successfully).\n\n![Visual Studio Errors And Warnings](Documentation/VisualStudioErrorsAndWarnings.png)\n\nAlso, the IntelliSense for the ASPX code to edit the properties of the added controls does not work...\n\nThis is a\n[known issue](https://developercommunity.visualstudio.com/content/problem/668468/user-control-fake-error-with-dependency-injection.html)\nof Visual Studio and Microsoft seam to take *Under Consideration*...\n\nThere is workaround, which is provided with the\n[PosInformatique.AspNet.WebForms.DependencyInjection](https://www.nuget.org/packages/PosInformatique.AspNet.WebForms.DependencyInjection/)\npackage by adding\na constructor without parameter in the control:\n\n````csharp\npublic partial class UserControlWithDependency : System.Web.UI.UserControl\n{\n    private readonly IDogManager dogManager;\n\n    [ActivatorUtilitiesConstructor]   // It is import to set this attribute to be sure this constructor will be called by the Microsoft.Extensions.DependencyInjection.\n    public UserControlWithDependency(IDogManager dogManager)\n    {\n        this.dogManager = dogManager;\n    }\n\n    // Avoid the errors and the warnings in the Visual Studio ASPX code designer\n    public UserControlWithDependency()\n    {\n    }\n\n    protected void Page_Load(object sender, EventArgs e)\n    {\n        this.doggoList.DataSource = this.dogManager.GetDogs();\n        this.doggoList.DataBind();\n    }\n}\n````\n\n\u003e **Remarks**: You have also to add the [ActivatorUtilitiesConstructorAttribute](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.activatorutilitiesconstructorattribute)\non the constructor which requires the services registered in the\n[Microsoft.Extensions.DependencyInjection](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection/)\ncontainer.\nWithout this attribute, the constructor without parameter can be used during the execution of the application\nwith the default behavior of the\n[Microsoft.Extensions.DependencyInjection](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection/).\n\n## Contributions\nDo not hesitate to clone my code and submit some changes...\nIt is a open source project, so everyone is welcome to improve this library...\nBy the way, I am french... So maybe you will remarks that my english is not really fluent...\nSo do not hesitate to fix my resources strings or my documentation... Merci !\n\n## Thanks\nI want to thank the [DiliTrust](https://www.dilitrust.com/) company to test and gave me their\nfeedback of this library for their ASP .NET WebForms applications.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPosInformatique%2FPosInformatique.AspNet.WebForms.DependencyInjection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FPosInformatique%2FPosInformatique.AspNet.WebForms.DependencyInjection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPosInformatique%2FPosInformatique.AspNet.WebForms.DependencyInjection/lists"}