Skip to content

Commit

Permalink
Add ability to use Autofac parameters for WCF service resolution.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Popov committed Apr 30, 2016
1 parent 5feaae5 commit a894214
Showing 1 changed file with 121 additions and 94 deletions.
215 changes: 121 additions & 94 deletions src/Autofac.Integration.Wcf/ServiceHostExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,94 +1,121 @@
// This software is part of the Autofac IoC container
// Copyright © 2011 Autofac Contributors
// http://autofac.org
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Globalization;
using System.Linq;
using System.ServiceModel;
using Autofac.Core;

namespace Autofac.Integration.Wcf
{
/// <summary>
/// Adds dependency injection related methods to service hosts.
/// </summary>
public static class ServiceHostExtensions
{
/// <summary>
/// Adds the custom service behavior required for dependency injection.
/// </summary>
/// <typeparam name="T">The web service contract type.</typeparam>
/// <param name="serviceHost">The service host.</param>
/// <param name="container">The container.</param>
public static void AddDependencyInjectionBehavior<T>(this ServiceHostBase serviceHost, ILifetimeScope container)
{
if (container == null) throw new ArgumentNullException("container");

AddDependencyInjectionBehavior(serviceHost, typeof(T), container);
}

/// <summary>
/// Adds the custom service behavior required for dependency injection.
/// </summary>
/// <param name="serviceHost">The service host.</param>
/// <param name="contractType">The web service contract type.</param>
/// <param name="container">The container.</param>
public static void AddDependencyInjectionBehavior(this ServiceHostBase serviceHost, Type contractType, ILifetimeScope container)
{
if (serviceHost == null)
{
throw new ArgumentNullException("serviceHost");
}
if (contractType == null)
{
throw new ArgumentNullException("contractType");
}
if (container == null)
{
throw new ArgumentNullException("container");
}

var serviceBehavior = serviceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>();
if (serviceBehavior != null && serviceBehavior.InstanceContextMode == InstanceContextMode.Single)
return;

IComponentRegistration registration;
if (!container.ComponentRegistry.TryGetRegistration(new TypedService(contractType), out registration))
{
var message = string.Format(CultureInfo.CurrentCulture, ServiceHostExtensionsResources.ContractTypeNotRegistered, contractType.FullName);
throw new ArgumentException(message, "contractType");
}
var data = new ServiceImplementationData
{
ConstructorString = contractType.AssemblyQualifiedName,
ServiceTypeToHost = contractType,
ImplementationResolver = l => l.ResolveComponent(registration, Enumerable.Empty<Parameter>())
};

var behavior = new AutofacDependencyInjectionServiceBehavior(container, data);
serviceHost.Description.Behaviors.Add(behavior);
}
}
}
// This software is part of the Autofac IoC container
// Copyright © 2011 Autofac Contributors
// http://autofac.org
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Globalization;
using System.Linq;
using System.ServiceModel;
using Autofac.Core;
using System.Collections.Generic;

namespace Autofac.Integration.Wcf
{
/// <summary>
/// Adds dependency injection related methods to service hosts.
/// </summary>
public static class ServiceHostExtensions
{
/// <summary>
/// Adds the custom service behavior required for dependency injection.
/// </summary>
/// <typeparam name="T">The web service contract type.</typeparam>
/// <param name="serviceHost">The service host.</param>
/// <param name="container">The container.</param>
public static void AddDependencyInjectionBehavior<T>(this ServiceHostBase serviceHost, ILifetimeScope container)
{
AddDependencyInjectionBehavior(serviceHost, typeof(T), container);
}

/// <summary>
/// Adds the custom service behavior required for dependency injection.
/// </summary>
/// <param name="serviceHost">The service host.</param>
/// <param name="contractType">The web service contract type.</param>
/// <param name="container">The container.</param>
public static void AddDependencyInjectionBehavior(this ServiceHostBase serviceHost, Type contractType, ILifetimeScope container)
{
AddDependencyInjectionBehavior(serviceHost, contractType, container, Enumerable.Empty<Parameter>());
}

/// <summary>
/// Adds the custom service behavior required for dependency injection.
/// </summary>
/// <typeparam name="T">The web service contract type.</typeparam>
/// <param name="serviceHost">The service host.</param>
/// <param name="container">The container.</param>
/// <param name="parameters">Parameters for the instance.</param>
public static void AddDependencyInjectionBehavior<T>(this ServiceHostBase serviceHost, ILifetimeScope container, IEnumerable<Parameter> parameters)
{
AddDependencyInjectionBehavior(serviceHost, typeof(T), container, parameters);
}

/// <summary>
/// Adds the custom service behavior required for dependency injection.
/// </summary>
/// <param name="serviceHost">The service host.</param>
/// <param name="contractType">The web service contract type.</param>
/// <param name="container">The container.</param>
/// <param name="parameters">Parameters for the instance.</param>
public static void AddDependencyInjectionBehavior(this ServiceHostBase serviceHost, Type contractType, ILifetimeScope container, IEnumerable<Parameter> parameters)
{
if (serviceHost == null)
{
throw new ArgumentNullException("serviceHost");
}
if (contractType == null)
{
throw new ArgumentNullException("contractType");
}
if (container == null)
{
throw new ArgumentNullException("container");
}
if (parameters == null)
{
throw new ArgumentNullException("parameters");
}

var serviceBehavior = serviceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>();
if (serviceBehavior != null && serviceBehavior.InstanceContextMode == InstanceContextMode.Single)
return;

IComponentRegistration registration;
if (!container.ComponentRegistry.TryGetRegistration(new TypedService(contractType), out registration))
{
var message = string.Format(CultureInfo.CurrentCulture, ServiceHostExtensionsResources.ContractTypeNotRegistered, contractType.FullName);
throw new ArgumentException(message, "contractType");
}
var data = new ServiceImplementationData
{
ConstructorString = contractType.AssemblyQualifiedName,
ServiceTypeToHost = contractType,
ImplementationResolver = l => l.ResolveComponent(registration, parameters)
};

var behavior = new AutofacDependencyInjectionServiceBehavior(container, data);
serviceHost.Description.Behaviors.Add(behavior);
}
}
}

0 comments on commit a894214

Please sign in to comment.