1
0
Форкнуть 0
This commit is contained in:
Matt Fei 2018-05-22 16:10:57 -07:00
Родитель 200ad78ca3
Коммит 9605b1e455
11 изменённых файлов: 281 добавлений и 23 удалений

Просмотреть файл

@ -1,4 +1,5 @@

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See the License.txt file in the project root for full license information.
namespace Microsoft.AspNet.WebFormsDependencyInjection.Unity
{
@ -6,6 +7,7 @@ namespace Microsoft.AspNet.WebFormsDependencyInjection.Unity
using global::Unity.Exceptions;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection;
using System.Web.Hosting;
@ -76,7 +78,7 @@ namespace Microsoft.AspNet.WebFormsDependencyInjection.Unity
return result;
}
public IUnityContainer Container { get; } = new UnityContainer();
public IUnityContainer Container { get; internal set; } = new UnityContainer();
public void Stop(bool immediate)
{
@ -85,6 +87,16 @@ namespace Microsoft.AspNet.WebFormsDependencyInjection.Unity
Container.Dispose();
}
internal IServiceProvider NextServiceProvider
{
get { return _next; }
}
internal IDictionary<Type, bool> TypeCannotResolveDictionary
{
get { return _typesCannotResolve; }
}
protected virtual object DefaultCreateInstance(Type type)
{
return Activator.CreateInstance(

Просмотреть файл

@ -1,9 +1,12 @@
using System;
using System.Web;
using Unity;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See the License.txt file in the project root for full license information.
namespace Microsoft.AspNet.WebFormsDependencyInjection.Unity
{
using System;
using System.Web;
using global::Unity;
/// <summary>
/// Extension methods of HttpApplication that help use Unity container
/// </summary>

Просмотреть файл

@ -10,7 +10,7 @@ using System.Runtime.InteropServices;
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft Corporation")]
[assembly: AssemblyProduct("Microsoft.AspNet.WebFormsDependencyInjection.Unity")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyCopyright("\x00a9 Microsoft Corporation. All rights reserved.")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@ -22,3 +22,4 @@ using System.Runtime.InteropServices;
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("3e53288e-f0d1-4a06-8ff9-7dc8fadfdc62")]
[assembly: InternalsVisibleTo("Microsoft.AspNet.WebFormsDependencyInjection.Unity.Test, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]

Просмотреть файл

@ -1,8 +1,11 @@
using System.Web;
using Unity;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See the License.txt file in the project root for full license information.
namespace Microsoft.AspNet.WebFormsDependencyInjection.Unity
{
using System.Web;
using global::Unity;
/// <summary>
/// Extension methods of HttpApplication that help use Unity container
/// </summary>

Двоичные данные
test/UnityAdapter.Test/35MSSharedLib1024.snk Normal file

Двоичный файл не отображается.

Просмотреть файл

@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnityAdapter.Test
{
public class Class1
{
}
}

Просмотреть файл

@ -0,0 +1,106 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See the License.txt file in the project root for full license information.
namespace Microsoft.AspNet.WebFormsDependencyInjection.Unity.Test
{
using Moq;
using Microsoft.AspNet.WebFormsDependencyInjection.Unity;
using System;
using Xunit;
using global::Unity.Exceptions;
using global::Unity;
using System.Web;
public class ContainerServiceProviderTest
{
public ContainerServiceProviderTest()
{
HttpRuntime.WebObjectActivator = null;
}
[Fact]
public void ContainerServiceProvider_Should_Preserve_Existing_ServiceProvider_And_Initialize_UnityContainer()
{
var existingSP = new Mock<IServiceProvider>();
var containerSP = new ContainerServiceProvider(existingSP.Object);
Assert.Same(existingSP.Object, containerSP.NextServiceProvider);
Assert.NotNull(containerSP.Container);
}
[Fact]
public void GetService_Should_Resolve_Type_EvenIf_Unity_Cannot_Resolve()
{
var containerSP = new ContainerServiceProvider(null);
var resolvedObj = containerSP.GetService(typeof(TypeToResolveBase));
Assert.NotNull(resolvedObj);
Assert.IsType<TypeToResolveBase>(resolvedObj);
}
[Fact]
public void GetService_Should_Use_Saved_ServiceProvider_If_UnityContainer_Cannot_Resolve()
{
var existingSP = new Mock<IServiceProvider>();
existingSP.Setup(sp => sp.GetService(typeof(TypeToResolveBase))).Returns(new TypeToResolve());
var containerSP = new ContainerServiceProvider(existingSP.Object);
var resolvedObj = containerSP.GetService(typeof(TypeToResolveBase));
Assert.NotNull(resolvedObj);
Assert.IsType<TypeToResolve>(resolvedObj);
}
[Fact]
public void GetService_Should_Not_Try_UnityContainer_Again_If_UnityContainer_Failed_To_Resolve_A_Type()
{
var container = new Mock<IUnityContainer>();
var isFirstCall = true;
var secondCalled = false;
var typeToResolve = typeof(TypeToResolveBase);
container.Setup(sp => sp.Resolve(typeToResolve, "", null)).Callback(() =>
{
if(isFirstCall)
{
isFirstCall = false;
throw new ResolutionFailedException(typeToResolve, "", "", null);
}
else
{
secondCalled = true;
}
});
var containerSP = new ContainerServiceProvider(null);
containerSP.Container = container.Object;
var resolvedObj = containerSP.GetService(typeToResolve);
Assert.NotNull(resolvedObj);
Assert.IsType(typeToResolve, resolvedObj);
resolvedObj = containerSP.GetService(typeToResolve);
Assert.NotNull(resolvedObj);
Assert.IsType(typeToResolve, resolvedObj);
Assert.False(secondCalled);
}
[Fact]
public void GetService_Should_Cache_Type_That_Cannot_Be_Resolved_By_UnityContainer()
{
var containerSP = new ContainerServiceProvider(null);
var resolvedObj = containerSP.GetService(typeof(TypeToResolveBase));
Assert.NotNull(resolvedObj);
Assert.IsType<TypeToResolveBase>(resolvedObj);
Assert.True(containerSP.TypeCannotResolveDictionary.ContainsKey(typeof(TypeToResolveBase)));
}
}
class TypeToResolveBase
{
protected TypeToResolveBase() { }
}
class TypeToResolve : TypeToResolveBase
{
public TypeToResolve() { }
}
}

Просмотреть файл

@ -0,0 +1,59 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See the License.txt file in the project root for full license information.
namespace Microsoft.AspNet.WebFormsDependencyInjection.Unity.Test
{
using Moq;
using System;
using System.Web;
using Xunit;
public class HttpApplicationExtensionsTest
{
public HttpApplicationExtensionsTest()
{
HttpRuntime.WebObjectActivator = null;
}
[Fact]
public void AddUnity_Should_Throw_ArgumentNullException_If_HttpApplication_Is_Null()
{
Assert.Throws<ArgumentNullException>(() =>
{
((HttpApplication)null).AddUnity();
});
}
[Fact]
public void AddUnity_Should_Register_WebObjectActivator_With_ContainerServiceProvider()
{
var app = new HttpApplication();
var container = app.AddUnity();
Assert.NotNull(container);
Assert.NotNull(HttpRuntime.WebObjectActivator);
Assert.IsType<ContainerServiceProvider>(HttpRuntime.WebObjectActivator);
}
[Fact]
public void GetUnityContainer_Should_Return_UnityContainer()
{
var app = new HttpApplication();
app.AddUnity();
var container = app.GetUnityContainer();
Assert.NotNull(container);
}
[Fact]
public void GetUnityContainer_Should_Return_Null_If_Registered_WebObjectActivator_Is_Not_ContainerServiceProvider()
{
var app = new HttpApplication();
var existingSP = new Mock<IServiceProvider>();
HttpRuntime.WebObjectActivator = existingSP.Object;
var container = app.GetUnityContainer();
Assert.Null(container);
}
}
}

Просмотреть файл

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\packages\xunit.runner.visualstudio.2.3.1\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\..\packages\xunit.runner.visualstudio.2.3.1\build\net20\xunit.runner.visualstudio.props')" />
<Import Project="..\..\packages\xunit.core.2.3.1\build\xunit.core.props" Condition="Exists('..\..\packages\xunit.core.2.3.1\build\xunit.core.props')" />
<Import Project="..\..\packages\xunit.runner.msbuild.2.3.1\build\net452\xunit.runner.msbuild.props" Condition="Exists('..\..\packages\xunit.runner.msbuild.2.3.1\build\net452\xunit.runner.msbuild.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
@ -9,8 +10,8 @@
<ProjectGuid>{071D67ED-B0B6-4927-A75D-83DDDC4DD60B}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>UnityAdapter.Test</RootNamespace>
<AssemblyName>UnityAdapter.Test</AssemblyName>
<RootNamespace>Microsoft.AspNet.WebFormsDependencyInjection.Unity.Test</RootNamespace>
<AssemblyName>Microsoft.AspNet.WebFormsDependencyInjection.Unity.Test</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>
@ -33,15 +34,40 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>35MSSharedLib1024.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup>
<DelaySign>true</DelaySign>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\..\packages\Castle.Core.4.2.1\lib\net45\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="Moq, Version=4.8.0.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\..\packages\Moq.4.8.2\lib\net45\Moq.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Threading.Tasks.Extensions, Version=4.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Threading.Tasks.Extensions.4.3.0\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.ValueTuple">
<HintPath>..\..\packages\System.ValueTuple.4.4.0\lib\net47\System.ValueTuple.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="Unity.Abstractions, Version=3.3.0.0, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f" />
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll</HintPath>
</Reference>
@ -56,15 +82,27 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="ContainerServiceProviderTest.cs" />
<Compile Include="HttpApplicationExtensionsTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UnityAdapterTest.cs" />
</ItemGroup>
<ItemGroup>
<None Include="35MSSharedLib1024.snk" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\..\packages\xunit.analyzers.0.7.0\analyzers\dotnet\cs\xunit.analyzers.dll" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\UnityAdapter\Microsoft.AspNet.WebFormsDependencyInjection.Unity.csproj">
<Project>{3e53288e-f0d1-4a06-8ff9-7dc8fadfdc62}</Project>
<Name>Microsoft.AspNet.WebFormsDependencyInjection.Unity</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
@ -74,6 +112,7 @@
<Error Condition="!Exists('..\..\packages\xunit.runner.msbuild.2.3.1\build\net452\xunit.runner.msbuild.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\xunit.runner.msbuild.2.3.1\build\net452\xunit.runner.msbuild.targets'))" />
<Error Condition="!Exists('..\..\packages\xunit.core.2.3.1\build\xunit.core.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\xunit.core.2.3.1\build\xunit.core.props'))" />
<Error Condition="!Exists('..\..\packages\xunit.core.2.3.1\build\xunit.core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\xunit.core.2.3.1\build\xunit.core.targets'))" />
<Error Condition="!Exists('..\..\packages\xunit.runner.visualstudio.2.3.1\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\xunit.runner.visualstudio.2.3.1\build\net20\xunit.runner.visualstudio.props'))" />
</Target>
<Import Project="..\..\packages\xunit.runner.msbuild.2.3.1\build\net452\xunit.runner.msbuild.targets" Condition="Exists('..\..\packages\xunit.runner.msbuild.2.3.1\build\net452\xunit.runner.msbuild.targets')" />
<Import Project="..\..\packages\xunit.core.2.3.1\build\xunit.core.targets" Condition="Exists('..\..\packages\xunit.core.2.3.1\build\xunit.core.targets')" />

Просмотреть файл

@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See the License.txt file in the project root for full license information.
namespace Microsoft.AspNet.WebFormsDependencyInjection.Unity.Test
{
using Moq;
using System;
using System.Web;
using Xunit;
public class UnityAdapterTest
{
public UnityAdapterTest()
{
HttpRuntime.WebObjectActivator = null;
}
[Fact]
public void AddUnity_Should_Register_WebObjectActivator_And_Return_UnityContainer()
{
var unityContainer = UnityAdapter.AddUnity();
Assert.NotNull(HttpRuntime.WebObjectActivator);
Assert.IsType<ContainerServiceProvider>(HttpRuntime.WebObjectActivator);
Assert.NotNull(unityContainer);
}
[Fact]
public void AddUnity_Should_Chain_Existing_WebObjectActivator()
{
var existingSP = new Mock<IServiceProvider>();
HttpRuntime.WebObjectActivator = existingSP.Object;
var unityContainer = UnityAdapter.AddUnity();
Assert.NotNull(HttpRuntime.WebObjectActivator);
Assert.IsType<ContainerServiceProvider>(HttpRuntime.WebObjectActivator);
Assert.Same(existingSP.Object, ((ContainerServiceProvider)HttpRuntime.WebObjectActivator).NextServiceProvider);
Assert.NotNull(unityContainer);
}
}
}

Просмотреть файл

@ -1,5 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="4.2.1" targetFramework="net472" />
<package id="Moq" version="4.8.2" targetFramework="net472" />
<package id="System.Threading.Tasks.Extensions" version="4.3.0" targetFramework="net472" />
<package id="System.ValueTuple" version="4.4.0" targetFramework="net472" />
<package id="xunit" version="2.3.1" targetFramework="net472" />
<package id="xunit.abstractions" version="2.0.1" targetFramework="net472" />
<package id="xunit.analyzers" version="0.7.0" targetFramework="net472" />
@ -8,4 +12,5 @@
<package id="xunit.extensibility.core" version="2.3.1" targetFramework="net472" />
<package id="xunit.extensibility.execution" version="2.3.1" targetFramework="net472" />
<package id="xunit.runner.msbuild" version="2.3.1" targetFramework="net472" developmentDependency="true" />
<package id="xunit.runner.visualstudio" version="2.3.1" targetFramework="net472" developmentDependency="true" />
</packages>