From a3fd171712f42d8209c8e5007fa84bbf4b0689ae Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 8 Mar 2017 16:56:26 -0500 Subject: [PATCH 1/3] inject dependencies into the WebForms application --- .../Catalog.WebForms/Catalog.WebForms.csproj | 1 + .../Catalog.WebForms/Default.aspx.cs | 18 ++-- .../Catalog.WebForms/Global.asax.cs | 24 +----- .../Modules/AutoFacHttpModule.cs | 84 +++++++++++++++++++ .../Catalog.WebForms/Web.config | 4 + 5 files changed, 100 insertions(+), 31 deletions(-) create mode 100644 src/Web/Catalog.WebForms/Catalog.WebForms/Modules/AutoFacHttpModule.cs diff --git a/src/Web/Catalog.WebForms/Catalog.WebForms/Catalog.WebForms.csproj b/src/Web/Catalog.WebForms/Catalog.WebForms/Catalog.WebForms.csproj index 5661b2fdb..06bd309ca 100644 --- a/src/Web/Catalog.WebForms/Catalog.WebForms/Catalog.WebForms.csproj +++ b/src/Web/Catalog.WebForms/Catalog.WebForms/Catalog.WebForms.csproj @@ -221,6 +221,7 @@ + diff --git a/src/Web/Catalog.WebForms/Catalog.WebForms/Default.aspx.cs b/src/Web/Catalog.WebForms/Catalog.WebForms/Default.aspx.cs index bd222c5af..ee4efea3b 100644 --- a/src/Web/Catalog.WebForms/Catalog.WebForms/Default.aspx.cs +++ b/src/Web/Catalog.WebForms/Catalog.WebForms/Default.aspx.cs @@ -18,6 +18,13 @@ namespace Microsoft.eShopOnContainers.Catalog.WebForms private ICatalogService catalog; + protected _Default() { } + + public _Default(ICatalogService catalog) + { + this.catalog = catalog; + } + protected override void OnLoad(EventArgs e) { RegisterAsyncTask(new PageAsyncTask(LoadCatalogDataAsync)); @@ -27,14 +34,9 @@ namespace Microsoft.eShopOnContainers.Catalog.WebForms private async Task LoadCatalogDataAsync() { - var container = Application.Get("container") as IContainer; - using (scope = container?.BeginLifetimeScope()) - { - catalog = container?.Resolve(); - var collection = await catalog?.GetCatalogAsync(); - catalogList.DataSource = collection; - catalogList.DataBind(); - } + var collection = await catalog?.GetCatalogAsync(); + catalogList.DataSource = collection; + catalogList.DataBind(); } protected void Page_Load(object sender, EventArgs e) diff --git a/src/Web/Catalog.WebForms/Catalog.WebForms/Global.asax.cs b/src/Web/Catalog.WebForms/Catalog.WebForms/Global.asax.cs index 48e1d8dbc..f0e204417 100644 --- a/src/Web/Catalog.WebForms/Catalog.WebForms/Global.asax.cs +++ b/src/Web/Catalog.WebForms/Catalog.WebForms/Global.asax.cs @@ -1,14 +1,7 @@ -using Autofac; -using eShopOnContainers.Core.Services.Catalog; -using System; -using System.Collections.Generic; -using System.Linq; +using System; using System.Web; -using System.Web.Configuration; using System.Web.Optimization; using System.Web.Routing; -using System.Web.Security; -using System.Web.SessionState; namespace Microsoft.eShopOnContainers.Catalog.WebForms { @@ -21,21 +14,6 @@ namespace Microsoft.eShopOnContainers.Catalog.WebForms RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); - // Register Containers: - var settings= WebConfigurationManager.AppSettings; - var useFake = settings["usefake"]; - bool fake = useFake == "true"; - var builder = new ContainerBuilder(); - if (fake) - { - builder.RegisterType() - .As(); - } else { - builder.RegisterType() - .As(); - } - var container = builder.Build(); - Application.Add("container", container); } } } \ No newline at end of file diff --git a/src/Web/Catalog.WebForms/Catalog.WebForms/Modules/AutoFacHttpModule.cs b/src/Web/Catalog.WebForms/Catalog.WebForms/Modules/AutoFacHttpModule.cs new file mode 100644 index 000000000..d0d7a60e9 --- /dev/null +++ b/src/Web/Catalog.WebForms/Catalog.WebForms/Modules/AutoFacHttpModule.cs @@ -0,0 +1,84 @@ +using Autofac; +using eShopOnContainers.Core.Services.Catalog; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Configuration; +using System.Web.UI; + +namespace Microsoft.eShopOnContainers.Catalog.WebForms.Modules +{ + // Using DI with WebForms is not yet implemented. + // This implementation has been adapted from this post: + // https://blogs.msdn.microsoft.com/webdev/2016/10/19/modern-asp-net-web-forms-development-dependency-injection/ + + public class AutoFacHttpModule : IHttpModule + { + private static IContainer Container => lazyContainer.Value; + + private static Lazy lazyContainer = new Lazy(() => CreateContainer()); + + private static IContainer CreateContainer() + { + // Configure AutoFac: + // Register Containers: + var settings = WebConfigurationManager.AppSettings; + var useFake = settings["usefake"]; + bool fake = useFake == "true"; + var builder = new ContainerBuilder(); + if (fake) + { + builder.RegisterType() + .As(); + } + else + { + builder.RegisterType() + .As(); + } + var container = builder.Build(); + return container; + } + + public void Dispose() + { + Container.Dispose(); + } + + public void Init(HttpApplication context) + { + context.PreRequestHandlerExecute += (_, __) => InjectDependencies(); + } + + private void InjectDependencies() + { + if (HttpContext.Current.CurrentHandler is Page) + { + var page = HttpContext.Current.CurrentHandler as Page; + // Get the code-behind class that we may have written + var pageType = page.GetType().BaseType; + + // Determine if there is a constructor to inject, and grab it + var ctor = (from c in pageType.GetConstructors() + where c.GetParameters().Length > 0 + select c).FirstOrDefault(); + + if (ctor != null) + { + // Resolve the parameters for the constructor + var args = (from parm in ctor.GetParameters() + select Container.Resolve(parm.ParameterType)) + .ToArray(); + + // Execute the constructor method with the arguments resolved + ctor.Invoke(page, args); + } + + // Use the Autofac method to inject any properties that can be filled by Autofac + Container.InjectProperties(page); + + } + } + } +} \ No newline at end of file diff --git a/src/Web/Catalog.WebForms/Catalog.WebForms/Web.config b/src/Web/Catalog.WebForms/Catalog.WebForms/Web.config index 9da53af59..47ad4e633 100644 --- a/src/Web/Catalog.WebForms/Catalog.WebForms/Web.config +++ b/src/Web/Catalog.WebForms/Catalog.WebForms/Web.config @@ -20,6 +20,8 @@ + + @@ -39,6 +41,8 @@ + + From 74fbbecf5821b9c562513a464afc8579a6e39ed5 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 9 Mar 2017 16:38:02 -0500 Subject: [PATCH 2/3] Update Compiler package This is necessary for C# 7 feature support. See the pattern match is statement in AutoFacHttpModule --- .../Catalog.WebForms/Catalog.WebForms/Catalog.WebForms.csproj | 4 ++-- src/Web/Catalog.WebForms/Catalog.WebForms/Default.aspx.cs | 2 -- .../Catalog.WebForms/Modules/AutoFacHttpModule.cs | 3 +-- src/Web/Catalog.WebForms/Catalog.WebForms/Web.config | 4 ++-- src/Web/Catalog.WebForms/Catalog.WebForms/packages.config | 2 +- 5 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/Web/Catalog.WebForms/Catalog.WebForms/Catalog.WebForms.csproj b/src/Web/Catalog.WebForms/Catalog.WebForms/Catalog.WebForms.csproj index 06bd309ca..391983c31 100644 --- a/src/Web/Catalog.WebForms/Catalog.WebForms/Catalog.WebForms.csproj +++ b/src/Web/Catalog.WebForms/Catalog.WebForms/Catalog.WebForms.csproj @@ -1,7 +1,7 @@  + - Debug @@ -292,8 +292,8 @@ This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + - + @@ -21,7 +21,7 @@ - + diff --git a/src/Web/Catalog.WebForms/Catalog.WebForms/packages.config b/src/Web/Catalog.WebForms/Catalog.WebForms/packages.config index dd268d3f3..756f567ce 100644 --- a/src/Web/Catalog.WebForms/Catalog.WebForms/packages.config +++ b/src/Web/Catalog.WebForms/Catalog.WebForms/packages.config @@ -20,7 +20,7 @@ - + From 3ec61e0df64097b7c6524fbe58fee7a6173c84ed Mon Sep 17 00:00:00 2001 From: etomas Date: Sat, 18 Mar 2017 11:13:20 +0100 Subject: [PATCH 3/3] Tried patch proposed by Sandeep Bansal --- _docker/redis/Dockerfile.nanowin | 2 ++ src/Services/Basket/Basket.API/Dockerfile.nanowin | 1 + src/Services/Catalog/Catalog.API/Dockerfile.nanowin | 1 + src/Services/Identity/Identity.API/Dockerfile.nanowin | 1 + src/Services/Ordering/Ordering.API/Dockerfile.nanowin | 1 + src/Web/WebMVC/Dockerfile.nanowin | 1 + src/Web/WebSPA/Dockerfile.nanowin | 1 + 7 files changed, 8 insertions(+) diff --git a/_docker/redis/Dockerfile.nanowin b/_docker/redis/Dockerfile.nanowin index 6a36d1947..a08bcb0b6 100644 --- a/_docker/redis/Dockerfile.nanowin +++ b/_docker/redis/Dockerfile.nanowin @@ -22,6 +22,8 @@ RUN Get-Content redis.windows.conf | Where { $_ -notmatch 'bind 127.0.0.1' } | S EXPOSE 6379 +RUN set-itemproperty -path 'HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters' -Name ServerPriorityTimeLimit -Value 0 -Type DWord + # Define our command to be run when launching the container CMD .\\redis-server.exe .\\redis.unprotected.conf --port 6379 ; \ Write-Host Redis Started... ; \ diff --git a/src/Services/Basket/Basket.API/Dockerfile.nanowin b/src/Services/Basket/Basket.API/Dockerfile.nanowin index 41127e339..0541a26cc 100644 --- a/src/Services/Basket/Basket.API/Dockerfile.nanowin +++ b/src/Services/Basket/Basket.API/Dockerfile.nanowin @@ -1,6 +1,7 @@ FROM microsoft/dotnet:1.1-runtime-nanoserver ARG source WORKDIR /app +RUN set-itemproperty -path 'HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters' -Name ServerPriorityTimeLimit -Value 0 -Type DWord EXPOSE 80 COPY ${source:-obj/Docker/publish} . ENTRYPOINT ["dotnet", "Basket.API.dll"] diff --git a/src/Services/Catalog/Catalog.API/Dockerfile.nanowin b/src/Services/Catalog/Catalog.API/Dockerfile.nanowin index 5aad21ad1..c40d3a4d0 100644 --- a/src/Services/Catalog/Catalog.API/Dockerfile.nanowin +++ b/src/Services/Catalog/Catalog.API/Dockerfile.nanowin @@ -1,6 +1,7 @@ FROM microsoft/dotnet:1.1-runtime-nanoserver ARG source WORKDIR /app +RUN set-itemproperty -path 'HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters' -Name ServerPriorityTimeLimit -Value 0 -Type DWord EXPOSE 80 COPY ${source:-obj/Docker/publish} . ENTRYPOINT ["dotnet", "Catalog.API.dll"] diff --git a/src/Services/Identity/Identity.API/Dockerfile.nanowin b/src/Services/Identity/Identity.API/Dockerfile.nanowin index 01f5eb8c5..b950d8334 100644 --- a/src/Services/Identity/Identity.API/Dockerfile.nanowin +++ b/src/Services/Identity/Identity.API/Dockerfile.nanowin @@ -1,6 +1,7 @@ FROM microsoft/dotnet:1.1-runtime-nanoserver ARG source WORKDIR /app +RUN set-itemproperty -path 'HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters' -Name ServerPriorityTimeLimit -Value 0 -Type DWord EXPOSE 80 COPY ${source:-obj/Docker/publish} . ENTRYPOINT ["dotnet", "Identity.API.dll"] diff --git a/src/Services/Ordering/Ordering.API/Dockerfile.nanowin b/src/Services/Ordering/Ordering.API/Dockerfile.nanowin index 85b1f46b4..eac299fc7 100644 --- a/src/Services/Ordering/Ordering.API/Dockerfile.nanowin +++ b/src/Services/Ordering/Ordering.API/Dockerfile.nanowin @@ -1,6 +1,7 @@ FROM microsoft/dotnet:1.1-runtime-nanoserver ARG source WORKDIR /app +RUN set-itemproperty -path 'HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters' -Name ServerPriorityTimeLimit -Value 0 -Type DWord EXPOSE 80 COPY ${source:-obj/Docker/publish} . ENTRYPOINT ["dotnet", "Ordering.API.dll"] diff --git a/src/Web/WebMVC/Dockerfile.nanowin b/src/Web/WebMVC/Dockerfile.nanowin index fe622e8de..302526330 100644 --- a/src/Web/WebMVC/Dockerfile.nanowin +++ b/src/Web/WebMVC/Dockerfile.nanowin @@ -1,6 +1,7 @@ FROM microsoft/dotnet:1.1-runtime-nanoserver ARG source WORKDIR /app +RUN set-itemproperty -path 'HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters' -Name ServerPriorityTimeLimit -Value 0 -Type DWord EXPOSE 80 COPY ${source:-obj/Docker/publish} . ENTRYPOINT ["dotnet", "WebMVC.dll"] diff --git a/src/Web/WebSPA/Dockerfile.nanowin b/src/Web/WebSPA/Dockerfile.nanowin index 8c784ca6e..5f9a48294 100644 --- a/src/Web/WebSPA/Dockerfile.nanowin +++ b/src/Web/WebSPA/Dockerfile.nanowin @@ -1,6 +1,7 @@ FROM microsoft/dotnet:1.1-runtime-nanoserver ARG source WORKDIR /app +RUN set-itemproperty -path 'HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters' -Name ServerPriorityTimeLimit -Value 0 -Type DWord EXPOSE 80 COPY ${source:-obj/Docker/publish} . ENTRYPOINT ["dotnet", "WebSPA.dll"]