Skip to content
This repository was archived by the owner on Nov 8, 2021. It is now read-only.

Commit 50511b6

Browse files
DevTrendsviceice
authored andcommitted
Initial Release
1 parent 394b8f7 commit 50511b6

15 files changed

+399
-0
lines changed

Unity.Wcf.sln

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 11.00
3+
# Visual Studio 2010
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Wcf", "Unity.Wcf\Unity.Wcf.csproj", "{8D78E387-BD71-4399-8A8E-6AE0DC8600F6}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|Any CPU = Debug|Any CPU
9+
Release|Any CPU = Release|Any CPU
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{8D78E387-BD71-4399-8A8E-6AE0DC8600F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13+
{8D78E387-BD71-4399-8A8E-6AE0DC8600F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
14+
{8D78E387-BD71-4399-8A8E-6AE0DC8600F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
15+
{8D78E387-BD71-4399-8A8E-6AE0DC8600F6}.Release|Any CPU.Build.0 = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
GlobalSection(ExtensibilityGlobals) = postSolution
21+
EnterpriseLibraryConfigurationToolBinariesPath = packages\Unity.2.1.505.0\lib\NET35
22+
EndGlobalSection
23+
EndGlobal

Unity.Wcf/Properties/AssemblyInfo.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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("Unity.Wcf")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("DevTrends")]
12+
[assembly: AssemblyProduct("Unity.Wcf")]
13+
[assembly: AssemblyCopyright("Copyright © DevTrends 2012")]
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("23d35d8e-4fcf-4883-a014-63e622391798")]
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")]

Unity.Wcf/Unity.Wcf.csproj

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{8D78E387-BD71-4399-8A8E-6AE0DC8600F6}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Unity.Wcf</RootNamespace>
12+
<AssemblyName>Unity.Wcf</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
35+
<HintPath>..\packages\CommonServiceLocator.1.0\lib\NET35\Microsoft.Practices.ServiceLocation.dll</HintPath>
36+
</Reference>
37+
<Reference Include="Microsoft.Practices.Unity, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
38+
<SpecificVersion>False</SpecificVersion>
39+
<HintPath>..\packages\Unity.2.1.505.0\lib\NET35\Microsoft.Practices.Unity.dll</HintPath>
40+
</Reference>
41+
<Reference Include="Microsoft.Practices.Unity.Configuration, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
42+
<SpecificVersion>False</SpecificVersion>
43+
<HintPath>..\packages\Unity.2.1.505.0\lib\NET35\Microsoft.Practices.Unity.Configuration.dll</HintPath>
44+
</Reference>
45+
<Reference Include="System" />
46+
<Reference Include="System.Core" />
47+
<Reference Include="System.ServiceModel" />
48+
<Reference Include="System.ServiceModel.Activation" />
49+
<Reference Include="System.Xml.Linq" />
50+
<Reference Include="System.Data.DataSetExtensions" />
51+
<Reference Include="Microsoft.CSharp" />
52+
<Reference Include="System.Data" />
53+
<Reference Include="System.Xml" />
54+
</ItemGroup>
55+
<ItemGroup>
56+
<Compile Include="Properties\AssemblyInfo.cs" />
57+
<Compile Include="UnityContractBehavior.cs" />
58+
<Compile Include="UnityInstanceContextExtension.cs" />
59+
<Compile Include="UnityInstanceContextInitializer.cs" />
60+
<Compile Include="UnityInstanceProvider.cs" />
61+
<Compile Include="UnityServiceHost.cs" />
62+
<Compile Include="UnityServiceHostFactory.cs" />
63+
</ItemGroup>
64+
<ItemGroup>
65+
<None Include="packages.config" />
66+
</ItemGroup>
67+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
68+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
69+
Other similar extension points exist, see Microsoft.Common.targets.
70+
<Target Name="BeforeBuild">
71+
</Target>
72+
<Target Name="AfterBuild">
73+
</Target>
74+
-->
75+
</Project>

Unity.Wcf/UnityContractBehavior.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.ServiceModel.Channels;
3+
using System.ServiceModel.Description;
4+
using System.ServiceModel.Dispatcher;
5+
6+
namespace Unity.Wcf
7+
{
8+
public class UnityContractBehavior : IContractBehavior
9+
{
10+
private readonly IInstanceProvider _instanceProvider;
11+
12+
public UnityContractBehavior(IInstanceProvider instanceProvider)
13+
{
14+
if (instanceProvider == null)
15+
{
16+
throw new ArgumentNullException("instanceProvider");
17+
}
18+
19+
_instanceProvider = instanceProvider;
20+
}
21+
22+
public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
23+
{
24+
}
25+
26+
public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
27+
{
28+
}
29+
30+
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
31+
{
32+
dispatchRuntime.InstanceProvider = _instanceProvider;
33+
dispatchRuntime.InstanceContextInitializers.Add(new UnityInstanceContextInitializer());
34+
}
35+
36+
public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
37+
{
38+
}
39+
}
40+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.ServiceModel;
3+
using Microsoft.Practices.Unity;
4+
5+
namespace Unity.Wcf
6+
{
7+
public class UnityInstanceContextExtension : IExtension<InstanceContext>
8+
{
9+
private IUnityContainer _childContainer;
10+
11+
public IUnityContainer GetChildContainer(IUnityContainer container)
12+
{
13+
if (container == null)
14+
{
15+
throw new ArgumentNullException("container");
16+
}
17+
18+
return _childContainer ?? (_childContainer = container.CreateChildContainer());
19+
}
20+
21+
public void DisposeOfChildContainer()
22+
{
23+
if (_childContainer != null)
24+
{
25+
_childContainer.Dispose();
26+
}
27+
}
28+
29+
public void Attach(InstanceContext owner)
30+
{
31+
}
32+
33+
public void Detach(InstanceContext owner)
34+
{
35+
}
36+
}
37+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.ServiceModel;
2+
using System.ServiceModel.Channels;
3+
using System.ServiceModel.Dispatcher;
4+
5+
namespace Unity.Wcf
6+
{
7+
public class UnityInstanceContextInitializer : IInstanceContextInitializer
8+
{
9+
public void Initialize(InstanceContext instanceContext, Message message)
10+
{
11+
instanceContext.Extensions.Add(new UnityInstanceContextExtension());
12+
}
13+
}
14+
}

Unity.Wcf/UnityInstanceProvider.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.ServiceModel;
3+
using System.ServiceModel.Channels;
4+
using System.ServiceModel.Dispatcher;
5+
using Microsoft.Practices.Unity;
6+
7+
namespace Unity.Wcf
8+
{
9+
public class UnityInstanceProvider : IInstanceProvider
10+
{
11+
private readonly IUnityContainer _container;
12+
private readonly Type _contractType;
13+
14+
public UnityInstanceProvider(IUnityContainer container, Type contractType)
15+
{
16+
if (container == null)
17+
{
18+
throw new ArgumentNullException("container");
19+
}
20+
21+
if (contractType == null)
22+
{
23+
throw new ArgumentNullException("contractType");
24+
}
25+
26+
_container = container;
27+
_contractType = contractType;
28+
}
29+
30+
public object GetInstance(InstanceContext instanceContext, Message message)
31+
{
32+
var childContainer =
33+
instanceContext.Extensions.Find<UnityInstanceContextExtension>().GetChildContainer(_container);
34+
35+
return childContainer.Resolve(_contractType);
36+
}
37+
38+
public object GetInstance(InstanceContext instanceContext)
39+
{
40+
return GetInstance(instanceContext, null);
41+
}
42+
43+
public void ReleaseInstance(InstanceContext instanceContext, object instance)
44+
{
45+
instanceContext.Extensions.Find<UnityInstanceContextExtension>().DisposeOfChildContainer();
46+
}
47+
}
48+
}

Unity.Wcf/UnityServiceHost.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.ServiceModel;
3+
using System.ServiceModel.Description;
4+
using Microsoft.Practices.Unity;
5+
6+
namespace Unity.Wcf
7+
{
8+
public class UnityServiceHost : ServiceHost
9+
{
10+
public UnityServiceHost(IUnityContainer container, Type serviceType, params Uri[] baseAddresses)
11+
: base(serviceType, baseAddresses)
12+
{
13+
if (container == null)
14+
{
15+
throw new ArgumentNullException("container");
16+
}
17+
18+
ApplyServiceBehaviors(container);
19+
20+
ApplyContractBehaviors(container);
21+
22+
foreach (var contractDescription in ImplementedContracts.Values)
23+
{
24+
var contractBehavior =
25+
new UnityContractBehavior(new UnityInstanceProvider(container, contractDescription.ContractType));
26+
27+
contractDescription.Behaviors.Add(contractBehavior);
28+
}
29+
}
30+
31+
private void ApplyContractBehaviors(IUnityContainer container)
32+
{
33+
var registeredContractBehaviors = container.ResolveAll<IContractBehavior>();
34+
35+
foreach (var contractBehavior in registeredContractBehaviors)
36+
{
37+
foreach (var contractDescription in ImplementedContracts.Values)
38+
{
39+
contractDescription.Behaviors.Add(contractBehavior);
40+
}
41+
}
42+
}
43+
44+
private void ApplyServiceBehaviors(IUnityContainer container)
45+
{
46+
var registeredServiceBehaviors = container.ResolveAll<IServiceBehavior>();
47+
48+
foreach (var serviceBehavior in registeredServiceBehaviors)
49+
{
50+
Description.Behaviors.Add(serviceBehavior);
51+
}
52+
}
53+
}
54+
}

Unity.Wcf/UnityServiceHostFactory.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.ServiceModel;
3+
using System.ServiceModel.Activation;
4+
using Microsoft.Practices.Unity;
5+
6+
namespace Unity.Wcf
7+
{
8+
public abstract class UnityServiceHostFactory : ServiceHostFactory
9+
{
10+
protected abstract void ConfigureContainer(IUnityContainer container);
11+
12+
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
13+
{
14+
var container = new UnityContainer();
15+
16+
ConfigureContainer(container);
17+
18+
return new UnityServiceHost(container, serviceType, baseAddresses);
19+
}
20+
}
21+
}

Unity.Wcf/packages.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="CommonServiceLocator" version="1.0" />
4+
<package id="Unity" version="2.1.505.0" />
5+
</packages>

0 commit comments

Comments
 (0)