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

Commit

Permalink
Initial Release
Browse files Browse the repository at this point in the history
  • Loading branch information
DevTrends authored and viceice committed Jun 20, 2017
1 parent 394b8f7 commit 50511b6
Show file tree
Hide file tree
Showing 15 changed files with 399 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Unity.Wcf.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Wcf", "Unity.Wcf\Unity.Wcf.csproj", "{8D78E387-BD71-4399-8A8E-6AE0DC8600F6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8D78E387-BD71-4399-8A8E-6AE0DC8600F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8D78E387-BD71-4399-8A8E-6AE0DC8600F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D78E387-BD71-4399-8A8E-6AE0DC8600F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8D78E387-BD71-4399-8A8E-6AE0DC8600F6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
EnterpriseLibraryConfigurationToolBinariesPath = packages\Unity.2.1.505.0\lib\NET35
EndGlobalSection
EndGlobal
36 changes: 36 additions & 0 deletions Unity.Wcf/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Unity.Wcf")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("DevTrends")]
[assembly: AssemblyProduct("Unity.Wcf")]
[assembly: AssemblyCopyright("Copyright © DevTrends 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("23d35d8e-4fcf-4883-a014-63e622391798")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
75 changes: 75 additions & 0 deletions Unity.Wcf/Unity.Wcf.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{8D78E387-BD71-4399-8A8E-6AE0DC8600F6}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Unity.Wcf</RootNamespace>
<AssemblyName>Unity.Wcf</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\CommonServiceLocator.1.0\lib\NET35\Microsoft.Practices.ServiceLocation.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Practices.Unity, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Unity.2.1.505.0\lib\NET35\Microsoft.Practices.Unity.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Practices.Unity.Configuration, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Unity.2.1.505.0\lib\NET35\Microsoft.Practices.Unity.Configuration.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.ServiceModel.Activation" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UnityContractBehavior.cs" />
<Compile Include="UnityInstanceContextExtension.cs" />
<Compile Include="UnityInstanceContextInitializer.cs" />
<Compile Include="UnityInstanceProvider.cs" />
<Compile Include="UnityServiceHost.cs" />
<Compile Include="UnityServiceHostFactory.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
40 changes: 40 additions & 0 deletions Unity.Wcf/UnityContractBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace Unity.Wcf
{
public class UnityContractBehavior : IContractBehavior
{
private readonly IInstanceProvider _instanceProvider;

public UnityContractBehavior(IInstanceProvider instanceProvider)
{
if (instanceProvider == null)
{
throw new ArgumentNullException("instanceProvider");
}

_instanceProvider = instanceProvider;
}

public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}

public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}

public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
{
dispatchRuntime.InstanceProvider = _instanceProvider;
dispatchRuntime.InstanceContextInitializers.Add(new UnityInstanceContextInitializer());
}

public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
{
}
}
}
37 changes: 37 additions & 0 deletions Unity.Wcf/UnityInstanceContextExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.ServiceModel;
using Microsoft.Practices.Unity;

namespace Unity.Wcf
{
public class UnityInstanceContextExtension : IExtension<InstanceContext>
{
private IUnityContainer _childContainer;

public IUnityContainer GetChildContainer(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}

return _childContainer ?? (_childContainer = container.CreateChildContainer());
}

public void DisposeOfChildContainer()
{
if (_childContainer != null)
{
_childContainer.Dispose();
}
}

public void Attach(InstanceContext owner)
{
}

public void Detach(InstanceContext owner)
{
}
}
}
14 changes: 14 additions & 0 deletions Unity.Wcf/UnityInstanceContextInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;

namespace Unity.Wcf
{
public class UnityInstanceContextInitializer : IInstanceContextInitializer
{
public void Initialize(InstanceContext instanceContext, Message message)
{
instanceContext.Extensions.Add(new UnityInstanceContextExtension());
}
}
}
48 changes: 48 additions & 0 deletions Unity.Wcf/UnityInstanceProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using Microsoft.Practices.Unity;

namespace Unity.Wcf
{
public class UnityInstanceProvider : IInstanceProvider
{
private readonly IUnityContainer _container;
private readonly Type _contractType;

public UnityInstanceProvider(IUnityContainer container, Type contractType)
{
if (container == null)
{
throw new ArgumentNullException("container");
}

if (contractType == null)
{
throw new ArgumentNullException("contractType");
}

_container = container;
_contractType = contractType;
}

public object GetInstance(InstanceContext instanceContext, Message message)
{
var childContainer =
instanceContext.Extensions.Find<UnityInstanceContextExtension>().GetChildContainer(_container);

return childContainer.Resolve(_contractType);
}

public object GetInstance(InstanceContext instanceContext)
{
return GetInstance(instanceContext, null);
}

public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
instanceContext.Extensions.Find<UnityInstanceContextExtension>().DisposeOfChildContainer();
}
}
}
54 changes: 54 additions & 0 deletions Unity.Wcf/UnityServiceHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using Microsoft.Practices.Unity;

namespace Unity.Wcf
{
public class UnityServiceHost : ServiceHost
{
public UnityServiceHost(IUnityContainer container, Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{
if (container == null)
{
throw new ArgumentNullException("container");
}

ApplyServiceBehaviors(container);

ApplyContractBehaviors(container);

foreach (var contractDescription in ImplementedContracts.Values)
{
var contractBehavior =
new UnityContractBehavior(new UnityInstanceProvider(container, contractDescription.ContractType));

contractDescription.Behaviors.Add(contractBehavior);
}
}

private void ApplyContractBehaviors(IUnityContainer container)
{
var registeredContractBehaviors = container.ResolveAll<IContractBehavior>();

foreach (var contractBehavior in registeredContractBehaviors)
{
foreach (var contractDescription in ImplementedContracts.Values)
{
contractDescription.Behaviors.Add(contractBehavior);
}
}
}

private void ApplyServiceBehaviors(IUnityContainer container)
{
var registeredServiceBehaviors = container.ResolveAll<IServiceBehavior>();

foreach (var serviceBehavior in registeredServiceBehaviors)
{
Description.Behaviors.Add(serviceBehavior);
}
}
}
}
21 changes: 21 additions & 0 deletions Unity.Wcf/UnityServiceHostFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Microsoft.Practices.Unity;

namespace Unity.Wcf
{
public abstract class UnityServiceHostFactory : ServiceHostFactory
{
protected abstract void ConfigureContainer(IUnityContainer container);

protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
var container = new UnityContainer();

ConfigureContainer(container);

return new UnityServiceHost(container, serviceType, baseAddresses);
}
}
}
5 changes: 5 additions & 0 deletions Unity.Wcf/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CommonServiceLocator" version="1.0" />
<package id="Unity" version="2.1.505.0" />
</packages>
4 changes: 4 additions & 0 deletions nuget/Build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rem ..\..\tools\nuget pack
nuget pack

pause
20 changes: 20 additions & 0 deletions nuget/Unity.Wcf.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Unity.Wcf</id>
<version>1.0.0</version>
<authors>Paul Hiles</authors>
<owners>DevTrends</owners>
<licenseUrl>http://unitywcf.codeplex.com/license</licenseUrl>
<projectUrl>http://unitywcf.codeplex.com/</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Unity.Wcf is a library that allows simple Integration of Microsoft's Unity IoC container with WCF. This project includes a bespoke InstanceProvider that creates a child container per client connection and disposes of all registered IDisposable instances once the connection is terminated.</description>
<tags>wcf unity ioc di</tags>
<dependencies>
<dependency id="Unity" version="2.1.505.0" />
</dependencies>
<frameworkAssemblies>
<frameworkAssembly assemblyName="System.ServiceModel.Activation" />
</frameworkAssemblies>
</metadata>
</package>
Loading

0 comments on commit 50511b6

Please sign in to comment.