Unity.Wcf is a library that allows simple Integration of Microsoft's Unity IoC container with WCF.
Перейти к файлу
Eugene Sadovoi 6c8293eaf2
Merge branch 'master' into feature/tests
2019-03-14 18:30:18 -04:00
src Add service host for simple REST services. 2019-03-14 10:16:10 +01:00
tests Add behavior test. 2019-03-14 10:17:05 +01:00
.editorconfig Add code style. 2019-03-14 10:16:10 +01:00
.gitignore Ignore opencover results. 2019-03-14 10:16:09 +01:00
CodeMaid.config Add code style. 2019-03-14 10:16:10 +01:00
LICENSE Create LICENSE 2017-06-20 12:12:03 +02:00
README.md Add codecov badge. 2019-03-14 10:16:10 +01:00
appveyor.yml Do not build on readme or license changes. 2019-03-14 10:16:10 +01:00
package.props Fixing dependency version 2019-01-30 00:10:17 -05:00
package.sln Add code style. 2019-03-14 10:16:10 +01:00

README.md

Build status License NuGet NuGet

Unity.Wcf

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.

How to use Unity.Wcf

WAS hosted services

For WAS-based hosting, first create a WcfServiceFactory class. You can choose any name. The WcfServiceFactory has to extend the UnityServiceHostFactory and implement the ConfigureContainer method.

Open the WcfServiceFactory class. Add all necessary component registrations. If you are registering IDisposable components that need to be created and destroyed on a per client basis (i.e. an EntityFramework DataContext), please ensure that you use the HierarchicalLifetimeManager:

public class WcfServiceFactory : UnityServiceHostFactory {
    protected override void ConfigureContainer(IUnityContainer container) {
        // configure container
        container
            .RegisterType<IService1, Service1>()
            .RegisterType<IRespository<Blah>, BlahRepository>()
            .RegisterType<IBlahContext, BlahContext>(new HierarchicalLifetimeManager());
    }
}

Right click on your svc file in the solution explorer and select View Markup. Next replace CodeBehind="Service1.svc.cs" with Factory="WcfService1.WcfServiceFactory", where WcfService1 is the namespace of your project. If you are using fileless activation and do not have an SVC file, change your web.config instead.

<serviceHostingEnvironment>
  <serviceActivations>
    <add factory="WcfService1.WcfServiceFactory" relativeAddress="./Service1.svc" service="WcfService1.Service1"/>
  </serviceActivations>
<serviceHostingEnvironment>

Windows Service hosting

If you are hosting your WCF service within a Windows Service using a ServiceHost, replace the ServiceHost instance with the custom Unity.Wcf.UnityServiceHost. You will find that the UnityServiceHost takes in a Unity container as its first parameter but is otherwise identical to the default ServiceHost.

You are free to configure Unity any way you like as long as the configured container is passed into the UnityServiceHost correctly. As with WAS hosting, if you want Unity.WCF to dispose of IDisposable components, you must register those components using the HierarchicalLifetimeManager lifestyle.