├── .gitattributes
├── .gitignore
├── .nuget
├── NuGet.Config
├── NuGet.exe
└── NuGet.targets
├── CODE-OF-CONDUCT.md
├── License.txt
├── README.md
├── WebFormsDependencyInjection.msbuild
├── WebFormsDependencyInjection.sln
├── build.cmd
├── src
├── UnityAdapter
│ ├── ContainerServiceProvider.cs
│ ├── HttpApplicationExtensions.cs
│ ├── Microsoft.AspNet.WebFormsDependencyInjection.Unity.csproj
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── UnityAdapter.cs
│ └── packages.config
└── packages
│ ├── Packages.csproj
│ └── UnityAdapter.nupkg
│ ├── Microsoft.AspNet.WebFormsDependencyInjection.Unity.nuproj
│ └── Microsoft.AspNet.WebFormsDependencyInjection.Unity.nuspec
├── test
└── UnityAdapter.Test
│ ├── 35MSSharedLib1024.snk
│ ├── ContainerServiceProviderTest.cs
│ ├── HttpApplicationExtensionsTest.cs
│ ├── Microsoft.AspNet.WebFormsDependencyInjection.Unity.Test.csproj
│ ├── Properties
│ └── AssemblyInfo.cs
│ ├── UnityAdapterTest.cs
│ └── packages.config
└── tools
├── 35MSSharedLib1024.snk
├── NuGet.targets
├── NuGetProj.targets
├── NuProj.Tasks.dll
├── NuProj.targets
├── UnityAdapter.settings.targets
├── WebFormsDependencyInjection.Extensions.settings.targets
├── WebFormsDependencyInjection.Extensions.targets
├── WebFormsDependencyInjection.settings.targets
├── WebFormsDependencyInjection.targets
└── version.targets
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.doc diff=astextplain
2 | *.DOC diff=astextplain
3 | *.docx diff=astextplain
4 | *.DOCX diff=astextplain
5 | *.dot diff=astextplain
6 | *.DOT diff=astextplain
7 | *.pdf diff=astextplain
8 | *.PDF diff=astextplain
9 | *.rtf diff=astextplain
10 | *.RTF diff=astextplain
11 |
12 | *.jpg binary
13 | *.png binary
14 | *.gif binary
15 |
16 | *.cs text=auto diff=csharp
17 | *.vb text=auto
18 | *.resx text=auto
19 | *.c text=auto
20 | *.cpp text=auto
21 | *.cxx text=auto
22 | *.h text=auto
23 | *.hxx text=auto
24 | *.py text=auto
25 | *.rb text=auto
26 | *.java text=auto
27 | *.html text=auto
28 | *.htm text=auto
29 | *.css text=auto
30 | *.scss text=auto
31 | *.sass text=auto
32 | *.less text=auto
33 | *.js text=auto
34 | *.lisp text=auto
35 | *.clj text=auto
36 | *.sql text=auto
37 | *.php text=auto
38 | *.lua text=auto
39 | *.m text=auto
40 | *.asm text=auto
41 | *.erl text=auto
42 | *.fs text=auto
43 | *.fsx text=auto
44 | *.hs text=auto
45 |
46 | *.csproj text=auto
47 | *.vbproj text=auto
48 | *.fsproj text=auto
49 | *.dbproj text=auto
50 | *.sln text=auto eol=crlf
51 |
52 | *.sh eol=lf
53 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | [Oo]bj/
2 | [Bb]in/
3 | TestResults/
4 | _ReSharper.*/
5 | /packages/
6 | artifacts/
7 | PublishProfiles/
8 | *.user
9 | *.suo
10 | *.cache
11 | *.sln.ide
12 | .vs
13 | .build/
14 | .testPublish/
15 | msbuild.*
16 | src/**/tools/Roslyn*/
--------------------------------------------------------------------------------
/.nuget/NuGet.Config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.nuget/NuGet.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aspnet/AspNetWebFormsDependencyInjection/85675f48512ec51cad296c85113cb6f2eda5f43b/.nuget/NuGet.exe
--------------------------------------------------------------------------------
/.nuget/NuGet.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildProjectDirectory)\..\
5 |
6 |
7 | false
8 |
9 |
10 | false
11 |
12 |
13 | true
14 |
15 |
16 | false
17 |
18 |
19 |
20 |
21 |
22 |
26 |
27 |
28 |
29 |
30 | $([System.IO.Path]::Combine($(SolutionDir), ".nuget"))
31 |
32 |
33 |
34 |
35 | $(SolutionDir).nuget
36 |
37 |
38 |
39 | $(MSBuildProjectDirectory)\packages.$(MSBuildProjectName.Replace(' ', '_')).config
40 | $(MSBuildProjectDirectory)\packages.$(MSBuildProjectName).config
41 |
42 |
43 |
44 | $(MSBuildProjectDirectory)\packages.config
45 | $(PackagesProjectConfig)
46 |
47 |
48 |
49 |
50 | $(NuGetToolsPath)\NuGet.exe
51 | @(PackageSource)
52 |
53 | "$(NuGetExePath)"
54 | mono --runtime=v4.0.30319 $(NuGetExePath)
55 |
56 | $(TargetDir.Trim('\\'))
57 |
58 | -RequireConsent
59 | -NonInteractive
60 |
61 | "$(SolutionDir) "
62 | "$(SolutionDir)"
63 |
64 |
65 | $(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)
66 | $(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols
67 |
68 |
69 |
70 | RestorePackages;
71 | $(BuildDependsOn);
72 |
73 |
74 |
75 |
76 | $(BuildDependsOn);
77 | BuildPackage;
78 |
79 |
80 |
81 |
82 |
83 |
84 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
99 |
100 |
103 |
104 |
105 |
106 |
108 |
109 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
141 |
142 |
143 |
144 |
145 |
--------------------------------------------------------------------------------
/CODE-OF-CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Code of Conduct
2 |
3 | This project has adopted the code of conduct defined by the Contributor Covenant
4 | to clarify expected behavior in our community.
5 |
6 | For more information, see the [.NET Foundation Code of Conduct](https://dotnetfoundation.org/code-of-conduct).
7 |
--------------------------------------------------------------------------------
/License.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) Microsoft Corporation
2 | All rights reserved.
3 |
4 | MIT License
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy of
7 | this software and associated documentation files (the ""Software""), to deal in
8 | the Software without restriction, including without limitation the rights to
9 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 | the Software, and to permit persons to whom the Software is furnished to do so,
11 | subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | > **Warning**
2 | > _This project demonstrates building a dependency injection adapter for the [Unity](https://github.com/unitycontainer/unity) IoC container. **It is a point of reference** for other adapter authors. Please note that this implementation is for demonstration purposes only and **is not being actively maintained.**_
3 |
4 | ## Introduction
5 | [Dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) design pattern is widely used in modern applications. It decouples objects to the extent that no client code has to be changed simply because an object it depends on needs to be changed to a different one. It brings you a lot of [benefits](http://tutorials.jenkov.com/dependency-injection/dependency-injection-benefits.html), like reduced dependency, more reusable code, more testable code, etc. However, it was very difficult to use dependency injection in WebForms application before. This is not an issue in .Net Framework 4.7.2 anymore. Dependency injection is natively supported in WebForms applications.
6 |
7 | ## How to use
8 | 1. Make sure your web project is targeting .NET Framework 4.7.2. You can download .NET Framework 4.7.2 developer pack from [here](https://www.microsoft.com/net/download/thank-you/net472-developer-pack). Check web.config and targetFramework in httpRuntime section should be 4.7.2.
9 | ```
10 |
11 |
12 |
13 |
14 | ```
15 | 2. Add the [`Unity.Container`](https://www.nuget.org/packages/Unity.Container/5.8.6) nuget package to your web project.
16 | 3. Add a reference to Microsoft.AspNet.WebFormsDependencyInjection.Unity.dll built by this project in your web project.
17 | 4. Open Global.asax and register the types in UnityContainer.
18 | ```
19 | protected void Application_Start(object sender, EventArgs e)
20 | {
21 | var container = this.AddUnity();
22 | container.RegisterType();
23 | }
24 | ```
25 |
26 | ## How to build
27 | 1. Open a [VS developer command prompt](https://docs.microsoft.com/en-us/dotnet/framework/tools/developer-command-prompt-for-vs)
28 | 2. Run build.cmd. This will build Nuget package and run all the unit tests.
29 | 3. All the build artifacts will be under AspNetWebFormsDependencyInjection\bin\Release\ folder.
30 |
--------------------------------------------------------------------------------
/WebFormsDependencyInjection.msbuild:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/WebFormsDependencyInjection.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27428.2037
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.WebFormsDependencyInjection.Unity", "src\UnityAdapter\Microsoft.AspNet.WebFormsDependencyInjection.Unity.csproj", "{3E53288E-F0D1-4A06-8FF9-7DC8FADFDC62}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{7A7363E1-8E17-4C97-9B94-6BAF1E422361}"
9 | EndProject
10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{20D2AF61-CF65-409C-95A4-DEA4C9B605F6}"
11 | EndProject
12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.WebFormsDependencyInjection.Unity.Test", "test\UnityAdapter.Test\Microsoft.AspNet.WebFormsDependencyInjection.Unity.Test.csproj", "{071D67ED-B0B6-4927-A75D-83DDDC4DD60B}"
13 | EndProject
14 | Global
15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
16 | Debug|Any CPU = Debug|Any CPU
17 | Release|Any CPU = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
20 | {3E53288E-F0D1-4A06-8FF9-7DC8FADFDC62}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {3E53288E-F0D1-4A06-8FF9-7DC8FADFDC62}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {3E53288E-F0D1-4A06-8FF9-7DC8FADFDC62}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {3E53288E-F0D1-4A06-8FF9-7DC8FADFDC62}.Release|Any CPU.Build.0 = Release|Any CPU
24 | {071D67ED-B0B6-4927-A75D-83DDDC4DD60B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25 | {071D67ED-B0B6-4927-A75D-83DDDC4DD60B}.Debug|Any CPU.Build.0 = Debug|Any CPU
26 | {071D67ED-B0B6-4927-A75D-83DDDC4DD60B}.Release|Any CPU.ActiveCfg = Release|Any CPU
27 | {071D67ED-B0B6-4927-A75D-83DDDC4DD60B}.Release|Any CPU.Build.0 = Release|Any CPU
28 | EndGlobalSection
29 | GlobalSection(SolutionProperties) = preSolution
30 | HideSolutionNode = FALSE
31 | EndGlobalSection
32 | GlobalSection(NestedProjects) = preSolution
33 | {3E53288E-F0D1-4A06-8FF9-7DC8FADFDC62} = {7A7363E1-8E17-4C97-9B94-6BAF1E422361}
34 | {071D67ED-B0B6-4927-A75D-83DDDC4DD60B} = {20D2AF61-CF65-409C-95A4-DEA4C9B605F6}
35 | EndGlobalSection
36 | GlobalSection(ExtensibilityGlobals) = postSolution
37 | SolutionGuid = {1B769C6E-7263-47BE-899F-95CA7A165C3F}
38 | EndGlobalSection
39 | EndGlobal
40 |
--------------------------------------------------------------------------------
/build.cmd:
--------------------------------------------------------------------------------
1 | @ECHO OFF
2 |
3 | setlocal
4 |
5 | set logOptions=/flp:Summary;Verbosity=diag;LogFile=msbuild.log /flp1:warningsonly;logfile=msbuild.wrn /flp2:errorsonly;logfile=msbuild.err
6 |
7 | echo Please build from VS 2015(or newer version) Developer Command Prompt
8 |
9 | :BUILD
10 | msbuild "%~dp0\WebFormsDependencyInjection.msbuild" %logOptions% /v:m /maxcpucount /nodeReuse:false %*
11 |
12 | endlocal
13 |
--------------------------------------------------------------------------------
/src/UnityAdapter/ContainerServiceProvider.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation. All rights reserved.
2 | // Licensed under the MIT license. See the License.txt file in the project root for full license information.
3 |
4 | namespace Microsoft.AspNet.WebFormsDependencyInjection.Unity
5 | {
6 | using global::Unity;
7 | using global::Unity.Exceptions;
8 | using System;
9 | using System.Collections.Concurrent;
10 | using System.Collections.Generic;
11 | using System.Reflection;
12 | using System.Web.Hosting;
13 |
14 | ///
15 | /// The Unity adapter for WebObjectActivator
16 | ///
17 | class ContainerServiceProvider : IServiceProvider, IRegisteredObject
18 | {
19 | private const int TypesCannontResolveCacheCap = 100000;
20 | private readonly IServiceProvider _next;
21 | private readonly ConcurrentDictionary _typesCannotResolve = new ConcurrentDictionary();
22 |
23 | public ContainerServiceProvider(IServiceProvider next)
24 | {
25 | _next = next;
26 | HostingEnvironment.RegisterObject(this);
27 | }
28 |
29 | ///
30 | /// Implementation of IServiceProvider. Asp.net will call this method to
31 | /// create the instances of Page/UserControl/HttpModule etc.
32 | ///
33 | ///
34 | ///
35 | public object GetService(Type serviceType)
36 | {
37 | //
38 | // Try unresolvable types
39 | if (_typesCannotResolve.ContainsKey(serviceType))
40 | {
41 | return DefaultCreateInstance(serviceType);
42 | }
43 |
44 | //
45 | // Try the container
46 | object result = null;
47 |
48 | try
49 | {
50 | result = Container.Resolve(serviceType);
51 | }
52 | catch (ResolutionFailedException)
53 | {
54 | // Ignore and continue
55 | }
56 |
57 | //
58 | // Try the next provider
59 | if (result == null)
60 | {
61 | result = _next?.GetService(serviceType);
62 | }
63 |
64 | //
65 | // Default activation
66 | if (result == null)
67 | {
68 | if ((result = DefaultCreateInstance(serviceType)) != null)
69 | {
70 | // Cache it
71 | if (_typesCannotResolve.Count < TypesCannontResolveCacheCap)
72 | {
73 | _typesCannotResolve.TryAdd(serviceType, true);
74 | }
75 | }
76 | }
77 |
78 | return result;
79 | }
80 |
81 | public IUnityContainer Container { get; internal set; } = new UnityContainer();
82 |
83 | public void Stop(bool immediate)
84 | {
85 | HostingEnvironment.UnregisterObject(this);
86 |
87 | Container.Dispose();
88 | }
89 |
90 | internal IServiceProvider NextServiceProvider
91 | {
92 | get { return _next; }
93 | }
94 |
95 | internal IDictionary TypeCannotResolveDictionary
96 | {
97 | get { return _typesCannotResolve; }
98 | }
99 |
100 | protected virtual object DefaultCreateInstance(Type type)
101 | {
102 | return Activator.CreateInstance(
103 | type,
104 | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.CreateInstance,
105 | null,
106 | null,
107 | null);
108 | }
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/src/UnityAdapter/HttpApplicationExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation. All rights reserved.
2 | // Licensed under the MIT license. See the License.txt file in the project root for full license information.
3 |
4 | namespace Microsoft.AspNet.WebFormsDependencyInjection.Unity
5 | {
6 | using System;
7 | using System.Web;
8 | using global::Unity;
9 |
10 | ///
11 | /// Extension methods of HttpApplication that help use Unity container
12 | ///
13 | public static class HttpApplicationExtensions
14 | {
15 | ///
16 | ///
17 | ///
18 | ///
19 | ///
20 | public static IUnityContainer AddUnity(this HttpApplication application)
21 | {
22 | if (application == null)
23 | {
24 | throw new ArgumentNullException(nameof(application));
25 | }
26 |
27 | return UnityAdapter.AddUnity();
28 | }
29 |
30 | ///
31 | ///
32 | ///
33 | ///
34 | public static IUnityContainer GetUnityContainer(this HttpApplication application)
35 | {
36 | return UnityAdapter.GetContainer();
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/UnityAdapter/Microsoft.AspNet.WebFormsDependencyInjection.Unity.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Debug
8 | AnyCPU
9 | {3E53288E-F0D1-4A06-8FF9-7DC8FADFDC62}
10 | Library
11 | Properties
12 | Microsoft.AspNet.WebFormsDependencyInjection.Unity
13 | Microsoft.AspNet.WebFormsDependencyInjection.Unity
14 | v4.7.2
15 | 512
16 | $(OutputPath)$(AssemblyName).xml
17 | ..\..\
18 |
19 |
20 | true
21 | full
22 | false
23 | DEBUG;TRACE
24 | prompt
25 | 4
26 | ..\obj\
27 |
28 |
29 | pdbonly
30 | true
31 | TRACE
32 | prompt
33 | 4
34 | ..\obj\
35 |
36 |
37 | true
38 |
39 |
40 | $(RepositoryRoot)tools\35MSSharedLib1024.snk
41 |
42 |
43 | true
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 | ..\..\packages\Unity.Abstractions.3.3.0\lib\net47\Unity.Abstractions.dll
57 |
58 |
59 | ..\..\packages\Unity.Container.5.8.6\lib\net47\Unity.Container.dll
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 | Microsoft
79 | MsSharedLib72
80 |
81 |
82 |
83 |
--------------------------------------------------------------------------------
/src/UnityAdapter/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Microsoft.AspNet.WebFormsDependencyInjection.Unity")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("Microsoft Corporation")]
12 | [assembly: AssemblyProduct("Microsoft.AspNet.WebFormsDependencyInjection.Unity")]
13 | [assembly: AssemblyCopyright("\x00a9 Microsoft Corporation. All rights reserved.")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("3e53288e-f0d1-4a06-8ff9-7dc8fadfdc62")]
24 |
25 | [assembly: InternalsVisibleTo("Microsoft.AspNet.WebFormsDependencyInjection.Unity.Test, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
--------------------------------------------------------------------------------
/src/UnityAdapter/UnityAdapter.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation. All rights reserved.
2 | // Licensed under the MIT license. See the License.txt file in the project root for full license information.
3 |
4 | namespace Microsoft.AspNet.WebFormsDependencyInjection.Unity
5 | {
6 | using System.Web;
7 | using global::Unity;
8 |
9 | ///
10 | /// Extension methods of HttpApplication that help use Unity container
11 | ///
12 | public static class UnityAdapter
13 | {
14 | private static object _lock = new object();
15 |
16 | ///
17 | /// Add a new Unity container in asp.net application. If there is WebObjectActivator already registered,
18 | /// it will be chained up. When the new WebObjectActivator can't resolve the type, the previous WebObjectActivator
19 | /// will be used. If the previous WebObjectActivator can't resolve it either, DefaultCreateInstance will be used
20 | /// which creates instance through none public default constructor based on reflection.
21 | ///
22 | ///
23 | public static IUnityContainer AddUnity()
24 | {
25 | lock (_lock)
26 | {
27 | HttpRuntime.WebObjectActivator = new ContainerServiceProvider(HttpRuntime.WebObjectActivator);
28 |
29 | return GetContainer();
30 | }
31 | }
32 |
33 | ///
34 | /// Get most recent added Unity container
35 | ///
36 | ///
37 | public static IUnityContainer GetContainer()
38 | {
39 | return (HttpRuntime.WebObjectActivator as ContainerServiceProvider)?.Container;
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/UnityAdapter/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/src/packages/Packages.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Release
6 | AnyCPU
7 | true
8 | 8.0.30703
9 | 2.0
10 | {7EC5863F-7FF1-41C7-A384-8FFF81531E7A}
11 | SAK
12 | SAK
13 | SAK
14 | SAK
15 |
16 |
17 | false
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/src/packages/UnityAdapter.nupkg/Microsoft.AspNet.WebFormsDependencyInjection.Unity.nuproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | $(MSBuildProjectName)
7 | $(MSBuildProjectName)
8 | Microsoft.AspNet.WebFormsDependencyInjection.Unity.nuspec
9 | true
10 |
11 |
12 |
13 | $(AssemblyPath)
14 | lib\Net472
15 |
16 |
17 | $(OutputPath)
18 | lib\Net472
19 |
20 |
21 | $(OutputPath)
22 | lib\Net472
23 |
24 |
25 |
26 |
27 |
28 |
29 | $(PackageOutputDir)
30 |
31 |
32 |
33 | NuGet
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/src/packages/UnityAdapter.nupkg/Microsoft.AspNet.WebFormsDependencyInjection.Unity.nuspec:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $NuGetPackageId$
5 | $NuGetPackageVersion$
6 | Microsoft
7 | Microsoft
8 | © Microsoft Corporation. All rights reserved.
9 | Microsoft ASP.NET WebForms Dependency Injection Unity Adapter
10 | In .Net 4.7.2, ASP.NET added dependency injection support in WebForms. This package is to make it easier to use Unity container in WebForms application.
11 | A Unity container adapter for HttpRuntime WebObjectActivator.
12 | en-US
13 | http://www.asp.net/
14 | http://go.microsoft.com/fwlink/?LinkID=288859
15 | http://www.microsoft.com/web/webpi/eula/net_library_eula_ENU.htm
16 | true
17 | ASP.NET WebForms Unity Container IoC UnityContainer
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/test/UnityAdapter.Test/35MSSharedLib1024.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aspnet/AspNetWebFormsDependencyInjection/85675f48512ec51cad296c85113cb6f2eda5f43b/test/UnityAdapter.Test/35MSSharedLib1024.snk
--------------------------------------------------------------------------------
/test/UnityAdapter.Test/ContainerServiceProviderTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation. All rights reserved.
2 | // Licensed under the MIT license. See the License.txt file in the project root for full license information.
3 |
4 | namespace Microsoft.AspNet.WebFormsDependencyInjection.Unity.Test
5 | {
6 | using Moq;
7 | using Microsoft.AspNet.WebFormsDependencyInjection.Unity;
8 | using System;
9 | using Xunit;
10 | using global::Unity.Exceptions;
11 | using global::Unity;
12 | using System.Web;
13 |
14 | public class ContainerServiceProviderTest
15 | {
16 | public ContainerServiceProviderTest()
17 | {
18 | HttpRuntime.WebObjectActivator = null;
19 | }
20 |
21 | [Fact]
22 | public void ContainerServiceProvider_Should_Preserve_Existing_ServiceProvider_And_Initialize_UnityContainer()
23 | {
24 | var existingSP = new Mock();
25 | var containerSP = new ContainerServiceProvider(existingSP.Object);
26 |
27 | Assert.Same(existingSP.Object, containerSP.NextServiceProvider);
28 | Assert.NotNull(containerSP.Container);
29 | }
30 |
31 | [Fact]
32 | public void GetService_Should_Resolve_Type_EvenIf_Unity_Cannot_Resolve()
33 | {
34 | var containerSP = new ContainerServiceProvider(null);
35 | var resolvedObj = containerSP.GetService(typeof(TypeToResolveBase));
36 |
37 | Assert.NotNull(resolvedObj);
38 | Assert.IsType(resolvedObj);
39 | }
40 |
41 | [Fact]
42 | public void GetService_Should_Use_Saved_ServiceProvider_If_UnityContainer_Cannot_Resolve()
43 | {
44 | var existingSP = new Mock();
45 | existingSP.Setup(sp => sp.GetService(typeof(TypeToResolveBase))).Returns(new TypeToResolve());
46 | var containerSP = new ContainerServiceProvider(existingSP.Object);
47 | var resolvedObj = containerSP.GetService(typeof(TypeToResolveBase));
48 |
49 | Assert.NotNull(resolvedObj);
50 | Assert.IsType(resolvedObj);
51 | }
52 |
53 | [Fact]
54 | public void GetService_Should_Not_Try_UnityContainer_Again_If_UnityContainer_Failed_To_Resolve_A_Type()
55 | {
56 | var container = new Mock();
57 | var isFirstCall = true;
58 | var secondCalled = false;
59 | var typeToResolve = typeof(TypeToResolveBase);
60 |
61 | container.Setup(sp => sp.Resolve(typeToResolve, "", null)).Callback(() =>
62 | {
63 | if(isFirstCall)
64 | {
65 | isFirstCall = false;
66 | throw new ResolutionFailedException(typeToResolve, "", "", null);
67 | }
68 | else
69 | {
70 | secondCalled = true;
71 | }
72 | });
73 | var containerSP = new ContainerServiceProvider(null);
74 | containerSP.Container = container.Object;
75 | var resolvedObj = containerSP.GetService(typeToResolve);
76 | Assert.NotNull(resolvedObj);
77 | Assert.IsType(typeToResolve, resolvedObj);
78 |
79 | resolvedObj = containerSP.GetService(typeToResolve);
80 | Assert.NotNull(resolvedObj);
81 | Assert.IsType(typeToResolve, resolvedObj);
82 | Assert.False(secondCalled);
83 | }
84 |
85 | [Fact]
86 | public void GetService_Should_Cache_Type_That_Cannot_Be_Resolved_By_UnityContainer()
87 | {
88 | var containerSP = new ContainerServiceProvider(null);
89 | var resolvedObj = containerSP.GetService(typeof(TypeToResolveBase));
90 |
91 | Assert.NotNull(resolvedObj);
92 | Assert.IsType(resolvedObj);
93 | Assert.True(containerSP.TypeCannotResolveDictionary.ContainsKey(typeof(TypeToResolveBase)));
94 | }
95 | }
96 |
97 | class TypeToResolveBase
98 | {
99 | protected TypeToResolveBase() { }
100 | }
101 |
102 | class TypeToResolve : TypeToResolveBase
103 | {
104 | public TypeToResolve() { }
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/test/UnityAdapter.Test/HttpApplicationExtensionsTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation. All rights reserved.
2 | // Licensed under the MIT license. See the License.txt file in the project root for full license information.
3 |
4 | namespace Microsoft.AspNet.WebFormsDependencyInjection.Unity.Test
5 | {
6 | using Moq;
7 | using System;
8 | using System.Web;
9 | using Xunit;
10 |
11 | public class HttpApplicationExtensionsTest
12 | {
13 | public HttpApplicationExtensionsTest()
14 | {
15 | HttpRuntime.WebObjectActivator = null;
16 | }
17 |
18 | [Fact]
19 | public void AddUnity_Should_Throw_ArgumentNullException_If_HttpApplication_Is_Null()
20 | {
21 | Assert.Throws(() =>
22 | {
23 | ((HttpApplication)null).AddUnity();
24 | });
25 | }
26 |
27 | [Fact]
28 | public void AddUnity_Should_Register_WebObjectActivator_With_ContainerServiceProvider()
29 | {
30 | var app = new HttpApplication();
31 | var container = app.AddUnity();
32 |
33 | Assert.NotNull(container);
34 | Assert.NotNull(HttpRuntime.WebObjectActivator);
35 | Assert.IsType(HttpRuntime.WebObjectActivator);
36 | }
37 |
38 | [Fact]
39 | public void GetUnityContainer_Should_Return_UnityContainer()
40 | {
41 | var app = new HttpApplication();
42 | app.AddUnity();
43 | var container = app.GetUnityContainer();
44 |
45 | Assert.NotNull(container);
46 | }
47 |
48 | [Fact]
49 | public void GetUnityContainer_Should_Return_Null_If_Registered_WebObjectActivator_Is_Not_ContainerServiceProvider()
50 | {
51 | var app = new HttpApplication();
52 | var existingSP = new Mock();
53 | HttpRuntime.WebObjectActivator = existingSP.Object;
54 |
55 | var container = app.GetUnityContainer();
56 | Assert.Null(container);
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/test/UnityAdapter.Test/Microsoft.AspNet.WebFormsDependencyInjection.Unity.Test.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Debug
9 | AnyCPU
10 | {071D67ED-B0B6-4927-A75D-83DDDC4DD60B}
11 | Library
12 | Properties
13 | Microsoft.AspNet.WebFormsDependencyInjection.Unity.Test
14 | Microsoft.AspNet.WebFormsDependencyInjection.Unity.Test
15 | v4.7.2
16 | 512
17 |
18 |
19 |
20 |
21 | true
22 | full
23 | false
24 | bin\Debug\
25 | DEBUG;TRACE
26 | prompt
27 | 4
28 |
29 |
30 | pdbonly
31 | true
32 | bin\Release\
33 | TRACE
34 | prompt
35 | 4
36 |
37 |
38 | true
39 |
40 |
41 | 35MSSharedLib1024.snk
42 |
43 |
44 | true
45 |
46 |
47 |
48 | ..\..\packages\Castle.Core.4.2.1\lib\net45\Castle.Core.dll
49 |
50 |
51 | ..\..\packages\Moq.4.8.2\lib\net45\Moq.dll
52 |
53 |
54 |
55 |
56 |
57 | ..\..\packages\System.Threading.Tasks.Extensions.4.3.0\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll
58 |
59 |
60 | ..\..\packages\System.ValueTuple.4.4.0\lib\net47\System.ValueTuple.dll
61 | True
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 | ..\..\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll
73 |
74 |
75 | ..\..\packages\xunit.assert.2.3.1\lib\netstandard1.1\xunit.assert.dll
76 |
77 |
78 | ..\..\packages\xunit.extensibility.core.2.3.1\lib\netstandard1.1\xunit.core.dll
79 |
80 |
81 | ..\..\packages\xunit.extensibility.execution.2.3.1\lib\net452\xunit.execution.desktop.dll
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 | {3e53288e-f0d1-4a06-8ff9-7dc8fadfdc62}
100 | Microsoft.AspNet.WebFormsDependencyInjection.Unity
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 | 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}.
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
--------------------------------------------------------------------------------
/test/UnityAdapter.Test/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("UnityAdapter.Test")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("UnityAdapter.Test")]
13 | [assembly: AssemblyCopyright("Copyright © 2018")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("071d67ed-b0b6-4927-a75d-83dddc4dd60b")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/test/UnityAdapter.Test/UnityAdapterTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation. All rights reserved.
2 | // Licensed under the MIT license. See the License.txt file in the project root for full license information.
3 |
4 | namespace Microsoft.AspNet.WebFormsDependencyInjection.Unity.Test
5 | {
6 | using Moq;
7 | using System;
8 | using System.Web;
9 | using Xunit;
10 |
11 | public class UnityAdapterTest
12 | {
13 | public UnityAdapterTest()
14 | {
15 | HttpRuntime.WebObjectActivator = null;
16 | }
17 |
18 | [Fact]
19 | public void AddUnity_Should_Register_WebObjectActivator_And_Return_UnityContainer()
20 | {
21 | var unityContainer = UnityAdapter.AddUnity();
22 |
23 | Assert.NotNull(HttpRuntime.WebObjectActivator);
24 | Assert.IsType(HttpRuntime.WebObjectActivator);
25 | Assert.NotNull(unityContainer);
26 | }
27 |
28 | [Fact]
29 | public void AddUnity_Should_Chain_Existing_WebObjectActivator()
30 | {
31 | var existingSP = new Mock();
32 | HttpRuntime.WebObjectActivator = existingSP.Object;
33 |
34 | var unityContainer = UnityAdapter.AddUnity();
35 |
36 | Assert.NotNull(HttpRuntime.WebObjectActivator);
37 | Assert.IsType(HttpRuntime.WebObjectActivator);
38 | Assert.Same(existingSP.Object, ((ContainerServiceProvider)HttpRuntime.WebObjectActivator).NextServiceProvider);
39 | Assert.NotNull(unityContainer);
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/test/UnityAdapter.Test/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/tools/35MSSharedLib1024.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aspnet/AspNetWebFormsDependencyInjection/85675f48512ec51cad296c85113cb6f2eda5f43b/tools/35MSSharedLib1024.snk
--------------------------------------------------------------------------------
/tools/NuGet.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildProjectDirectory)\..\
5 |
6 |
7 | false
8 |
9 |
10 | false
11 |
12 |
13 | true
14 |
15 |
16 | false
17 |
18 |
19 |
20 |
21 |
22 |
26 |
27 |
28 |
29 |
30 | $([System.IO.Path]::Combine($(SolutionDir), ".nuget"))
31 |
32 |
33 |
34 |
35 | $(SolutionDir).nuget
36 |
37 |
38 |
39 | $(MSBuildProjectDirectory)\packages.$(MSBuildProjectName.Replace(' ', '_')).config
40 | $(MSBuildProjectDirectory)\packages.$(MSBuildProjectName).config
41 |
42 |
43 |
44 | $(MSBuildProjectDirectory)\packages.config
45 | $(PackagesProjectConfig)
46 |
47 |
48 |
49 |
50 | $(NuGetToolsPath)\NuGet.exe
51 | @(PackageSource)
52 |
53 | "$(NuGetExePath)"
54 | mono --runtime=v4.0.30319 $(NuGetExePath)
55 |
56 | $(TargetDir.Trim('\\'))
57 |
58 | -RequireConsent
59 | -NonInteractive
60 |
61 | "$(SolutionDir) "
62 | "$(SolutionDir)"
63 |
64 |
65 | $(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)
66 | $(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols
67 |
68 |
69 |
70 | RestorePackages;
71 | $(BuildDependsOn);
72 |
73 |
74 |
75 |
76 | $(BuildDependsOn);
77 | BuildPackage;
78 |
79 |
80 |
81 |
82 |
83 |
84 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
99 |
100 |
103 |
104 |
105 |
106 |
108 |
109 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
141 |
142 |
143 |
144 |
145 |
--------------------------------------------------------------------------------
/tools/NuGetProj.targets:
--------------------------------------------------------------------------------
1 |
29 |
30 |
31 |
32 |
33 |
34 | Debug
35 | bin\$(Configuration)\
36 | obj\$(Configuration)\
37 | $(IntermediateOutputPath)$(NuGetPackageLanguage)\
38 |
39 |
40 |
41 |
42 |
43 |
44 | $(MSBuildProjectName)
45 | $(NuSpecId)
46 | $(NuSpecVersion)
47 | $(NuGetPackageVersion)
48 |
49 |
50 |
51 | 2
52 | true
53 | false
54 | $(MSBuildProjectDirectory)\$(NuGetPackageId).nuspec
55 | $([System.IO.Path]::GetFileName('$(NuSpecFile)'))
56 | true
57 | $(IntermediateOutputPath)$(NuGetPackageId).outputs
58 |
59 |
60 |
61 | $([MSBuild]::GetDirectoryNameOfFileAbove('$(MSBuildThisFileFullPath)', '.nuget\nuget.targets'))\.nuget\NuGet.exe
62 | $([System.IO.Path]::GetDirectoryName('$(NuGetExePath)'))
63 | $(NuGetOutputPath)
64 | $(OutputPath)NuGet
65 | $(PackageOutputDir.TrimEnd('\'))
66 | $(PackageOutputDir)
67 | $(IntermediateOutputPath)$(NuSpecFileName)
68 |
69 |
70 |
71 | $(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll
72 | $(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll
73 |
74 |
75 |
76 |
77 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath);$(MSBuildProjectFullPath)
78 |
79 |
80 |
84 |
85 |
86 |
87 | $(NuGetContentSource)
88 | $(NuGetContentDestination)
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
108 |
109 |
110 | $(SourceRootFullPath)
111 | src
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 | $(NuSpecId)
125 |
126 |
127 | $(NuSpecVersion)
128 |
129 |
130 | $(NuSpecTitle)
131 |
132 |
133 | $(NuSpecAuthors)
134 |
135 |
136 | $(NuSpecOwners)
137 |
138 |
139 | $(NuSpecDescription)
140 |
141 |
142 | $(NuSpecTags)
143 |
144 |
145 | $(NuSpecIconUrl)
146 |
147 |
148 | $(NuSpecProjectUrl)
149 |
150 |
151 | $(NuSpecLicenseUrl)
152 |
153 |
154 | $(NuSpecCopyright)
155 |
156 |
157 | $(NuSpecRequireLicenseAcceptance)
158 |
159 |
160 | $(NuSpecReleaseNotes)
161 |
162 |
163 | $(NuSpecSummary)
164 |
165 |
166 | $(NuSpecDevelopmentDependency)
167 |
168 |
169 | $(NuSpecLanguage)
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
181 |
182 | BeforeBuild;GetNuGetContentFromProject;ValidateNuGetParams;ReadNuGetCleanOutputs;GetNuGetProjectInputs;GetNuGetProjectOutputs;ValidateOutputs;NuGetPack;WriteNuGetProjectOutputs;AfterBuild
183 | BeforeClean;ReadNuGetCleanOutputs;CoreClean;AfterClean
184 | Clean;Build
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
210 |
211 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 | $([System.IO.Path]::GetFullPath($(SourceRootFullPath)))
222 |
223 |
224 |
225 |
226 |
227 |
228 | $([System.IO.Path]::Combine($(Destination), $([MSBuild]::MakeRelative($([System.IO.Path]::GetFullPath($(SourceRootFullPath))), %(Compile.RootDir)%(Directory)))))
229 |
230 |
231 |
232 |
233 |
237 |
238 |
239 |
241 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
264 |
265 |
266 |
267 |
268 |
269 |
270 | @(NuSpecProperties)
271 | -NoPackageAnalysis
272 | -NoPackageAnalysis -symbols
273 |
274 | $(BuildCommand.Replace('-symbols', ''))
275 | $(BuildCommand.Replace('/symbols', ''))
276 | $(BuildCommand) -Properties "$(NuSpecProperties)"
277 | $(BuildCommand) $(NuGetPackOptions)
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
304 |
305 |
306 | $(PackageOutputDir)\$(NuGetPackageId).$(NuGetPackageVersion).nupkg
307 | $(PackageOutputDir)\$(NuGetPackageId).$(NuGetPackageVersion).symbols.nupkg
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
343 |
344 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 | 1) {
384 | XNamespace pkgNs = "http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd";
385 | packageNode = new XElement(pkgNs + "package", new XElement("metadata", new XAttribute("schemaVersion", SchemaVersion)));
386 | } else {
387 | packageNode = new XElement("package", new XElement("metadata"));
388 | }
389 | }
390 | GeneratedNuSpec = Path.GetFullPath(Path.Combine(OutputDir, Path.GetFileName(NuSpecFile)));
391 | if (File.Exists(GeneratedNuSpec)) File.Delete(GeneratedNuSpec);
392 | XNamespace ns = packageNode.Name.Namespace;
393 |
394 | if(packageNode.Name.LocalName != "package") { Log.LogError("NuSpec file missing 'package' schema element. Found:'{0}'", packageNode.Name.LocalName); return false; };
395 | if (Files == null) Files = new TaskItem[] { };
396 | if (Dependencies == null) Dependencies = new TaskItem[] { };
397 | if (MetadataItems == null) MetadataItems = new TaskItem[] { };
398 | if (FrameworkAssemblies == null) FrameworkAssemblies = new TaskItem[] { };
399 |
400 | // replace/add simple metadata.
401 | XElement metadataNode = packageNode.FirstNode as XElement;
402 | if(metadataNode == null) { Log.LogError("NuSpec file missing 'metadata' schema element"); return false; };
403 | foreach (var metaItem in MetadataItems) {
404 | string name = metaItem.GetMetadata("Identity");
405 | string value = metaItem.GetMetadata("Value");
406 | XElement xnode = metadataNode.Descendants(ns + name).FirstOrDefault();
407 | if (xnode == null) { xnode = new XElement(name); metadataNode.Add(xnode); }
408 | xnode.Value = value;
409 | }
410 |
411 | // replaceable values for dependencies and frameworkassemblies - just replace the whole node.
412 | var removeQ1 = from dependencyNode in packageNode.Descendants(ns + "dependency").Attributes("id")
413 | from dependencyItem in Dependencies
414 | where dependencyItem.GetMetadata("Identity").ToLower().Equals(dependencyNode.Value.ToLower())
415 | select dependencyNode.Parent;
416 |
417 | var removeQ2 = from assemblyNode in packageNode.Descendants(ns + "frameworkAssembly").Attributes("assemblyName")
418 | from assemblyItem in FrameworkAssemblies
419 | where assemblyItem.GetMetadata("Identity").ToLower().Equals(assemblyNode.Value.ToLower())
420 | select assemblyNode.Parent;
421 |
422 | foreach (var node in removeQ1.ToArray()) node.Remove();
423 | foreach (var node in removeQ2.ToArray()) node.Remove();
424 |
425 | XElement filesNode = packageNode.Descendants(ns + "files").FirstOrDefault();
426 | if (filesNode == null) {
427 | filesNode = new XElement("files");
428 | packageNode.Add(filesNode);
429 | }
430 | filesNode.Add(from fi in Files select new XElement("file", new XAttribute("src", fi.GetMetadata("FullPath")), new XAttribute("target", fi.GetMetadata("Destination"))));
431 |
432 | XElement frameworkAssembliesNode = packageNode.Descendants(ns + "frameworkAssemblies").FirstOrDefault();
433 | if (frameworkAssembliesNode == null) {
434 | frameworkAssembliesNode = new XElement("frameworkAssemblies");
435 | metadataNode.Add(frameworkAssembliesNode);
436 | }
437 | frameworkAssembliesNode.Add(from assembly in FrameworkAssemblies select new XElement("frameworkAssembly",
438 | new XAttribute("assemblyName", assembly.GetMetadata("Identity")), new XAttribute("targetFramework", assembly.GetMetadata("TargetFramework"))));
439 |
440 | XElement dependenciesNode = packageNode.Descendants(ns + "dependencies").FirstOrDefault();
441 | if (dependenciesNode == null) {
442 | dependenciesNode = new XElement("dependencies");
443 | metadataNode.Add(dependenciesNode);
444 | }
445 | if(SchemaVersion > 1) {
446 | var depGroupsQ = from dp in Dependencies group dp by dp.GetMetadata("TargetFramework");
447 | foreach (var dpGroup in depGroupsQ) {
448 | XElement depGroupNode = new XElement("group");
449 | string targetFx = dpGroup.First().GetMetadata("TargetFramework");
450 | if(!string.IsNullOrEmpty(targetFx)) depGroupNode.Add(new XAttribute("targetFramework", dpGroup.First().GetMetadata("TargetFramework")));
451 | foreach(var depItem in dpGroup) {
452 | XElement dependencyNode = new XElement("dependency", new XAttribute("id", depItem.GetMetadata("Identity")), new XAttribute("version", depItem.GetMetadata("Version")));
453 | depGroupNode.Add(dependencyNode);
454 | }
455 | dependenciesNode.Add(depGroupNode);
456 | }
457 | } else {
458 | dependenciesNode.Add(from dp in Dependencies select new XElement("dependency", new XAttribute("id", dp.GetMetadata("Identity")), new XAttribute("version", dp.GetMetadata("Version"))));
459 | }
460 |
461 | if (!Directory.Exists(OutputDir)) Directory.CreateDirectory(OutputDir);
462 | packageNode.Save(GeneratedNuSpec);
463 | ]]>
464 |
465 |
466 |
467 |
468 |
472 |
473 |
474 |
475 |
476 |
477 |
478 |
479 |
480 |
494 |
495 |
496 |
497 |
498 |
499 |
500 |
501 |
502 |
--------------------------------------------------------------------------------
/tools/NuProj.Tasks.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aspnet/AspNetWebFormsDependencyInjection/85675f48512ec51cad296c85113cb6f2eda5f43b/tools/NuProj.Tasks.dll
--------------------------------------------------------------------------------
/tools/NuProj.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
11 | $(MSBuildProjectDirectory)\bin\
12 | $(MSBuildProjectDirectory)\obj\
13 | $(IntermediateOutputPath)$(Id).nuspec
14 |
15 | $(MSBuildThisFileDirectory)NuProj.Tasks.dll
16 | $(MSBuildThisFileDirectory)
17 | NuGet.exe
18 |
19 |
20 |
26 |
27 |
28 | $(OutDir)$(Id).$(Version).nupkg
29 |
30 |
31 |
35 |
36 |
37 | $(MSBuildAllProjects);$(MSBuildProjectFullPath)
38 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
39 |
40 |
41 |
44 |
45 |
46 |
47 |
48 |
51 |
52 |
53 |
55 |
57 |
58 |
60 |
62 |
63 |
64 |
65 |
68 |
69 |
73 |
93 |
94 |
95 |
98 |
99 |
105 |
107 |
111 |
112 |
113 |
116 |
117 |
118 |
119 | <_ToBeDeleted Include="$(NuSpecPath)" />
120 | <_ToBeDeleted Include="$(NuGetOuputPath)" />
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
--------------------------------------------------------------------------------
/tools/UnityAdapter.settings.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 | rtm
4 | 2018
5 | 1
6 | 0
7 | 0
8 |
9 |
10 |
11 | 5.8.6
12 |
13 |
--------------------------------------------------------------------------------
/tools/WebFormsDependencyInjection.Extensions.settings.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory),WebFormsDependencyInjection.sln))\
5 |
6 |
7 |
8 | Release
9 | $(RepositoryRootEx)bin\
10 | $(RepositoryRootEx)obj\
11 | $(BinPath)$(Configuration)\
12 | $(ObjPath)$(Configuration)\$(MSBuildProjectName)\
13 |
14 |
15 |
16 | $(CodeSignOutputPath)
17 | $(OutputPath)
18 |
19 |
20 |
21 | $(MSBuildThisFileDirectory)WebFormsDependencyInjection.Extensions.targets
22 |
23 |
24 |
--------------------------------------------------------------------------------
/tools/WebFormsDependencyInjection.Extensions.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | $(NuGetPackageVersion)-b$(VersionBuild)
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/tools/WebFormsDependencyInjection.settings.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 | $(MSBuildThisFileDirectory)WebFormsDependencyInjection.Extensions.settings.targets
4 | $([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), 'WebFormsDependencyInjection.sln'))\
5 |
6 |
7 |
9 |
11 |
12 |
13 |
14 |
15 |
16 | $([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), WebFormsDependencyInjection.sln))\
17 | $([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), WebFormsDependencyInjection.sln))\
18 |
19 |
20 |
21 | Release
22 | $(RepositoryRoot)bin\$(Configuration)\
23 | $(RepositoryRoot)obj\$(Configuration)\$(MSBuildProjectName)\
24 |
25 |
26 |
27 | $(OutputPath)
28 | $(AssemblyPath)Packages
29 | $(AssemblyPath)\SymbolPackages
30 | $(OutputPath)test\
31 |
32 |
33 |
34 | $(RepositoryRoot)packages\
35 | true
36 | $(RepositoryRoot)\src\$(MSBuildProjectName)\
37 |
38 |
39 |
40 | $(RepositoryRoot)tools\WebFormsDependencyInjection.targets
41 | $(CustomAfterMicrosoftCommonTargets)
42 |
43 |
44 |
--------------------------------------------------------------------------------
/tools/WebFormsDependencyInjection.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SetNuSpecProperties;$(BuildDependsOn)
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | $(AssemblyVersion)
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 | NuGetPackageVersion=$(NuGetPackageVersion);
24 | NuGetPackageId=$(NuGetPackageId);
25 | UnityContainerNuGetPackageVersion=$(UnityContainerNuGetPackageVersion);
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/tools/version.targets:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
12 |
13 |
14 | true
15 | false
16 |
17 |
18 |
19 | 2016
20 | INVALID_VersionMajor
21 | INVALID_VersionMinor
22 | $([MSBuild]::Add(1, $([MSBuild]::Subtract($([System.DateTime]::Now.Year), $(VersionStartYear)))))$([System.DateTime]::Now.ToString("MMdd"))
23 | 0
24 | 0
25 |
26 |
27 |
28 |
29 |
30 | $(VersionMajor).$(VersionMinor).0.$(VersionRevision)
31 | $(VersionMajor).$(VersionMinor).$(VersionBuild).$(VersionRevision)
32 | $(VersionMajor).$(VersionMinor).$(VersionRelease)-$(VersionBuild)
33 | $(AssemblyFileVersion)
34 | $(IntermediateOutputPath)$(MSBuildProjectName).version.cs
35 |
36 |
37 |
38 | $(VersionMajor).$(VersionMinor).$(VersionRelease)
39 | $(NuGetPackageVersion)-$(BuildQuality)
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 | $(DefineConstants);BUILD_GENERATED_VERSION
53 |
54 |
55 |
56 |
57 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
58 |
59 |
60 |
65 |
66 | GenerateVersionFile;$(CompileDependsOn)
67 | ValidateVersionValues;ShouldGenerateVersionFile;GenerateVersionFileCore
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
93 |
94 |
95 |
96 |
97 |
98 | @(VersionText)
99 | !$(VersionText.Contains('$(AssemblyFileVersion)'))
100 |
101 |
102 |
103 |
104 |
105 |
106 | $([System.Convert]::ToInt16('$(VersionMajor)'))
107 | $([System.Convert]::ToInt16('$(VersionMinor)'))
108 | $([System.Convert]::ToInt16('$(VersionBuild)'))
109 | $([System.Convert]::ToInt16('$(VersionRevision)'))
110 |
111 |
112 |
113 |
114 |
--------------------------------------------------------------------------------