Skip to content

Instantly share code, notes, and snippets.

@MatthewBarker
Created February 25, 2015 11:55
Show Gist options
  • Save MatthewBarker/b47b9df9edcf0a5fa384 to your computer and use it in GitHub Desktop.
Save MatthewBarker/b47b9df9edcf0a5fa384 to your computer and use it in GitHub Desktop.
AutomapServiceBehavior: This avoids multi threading issues with Automapper in a class library used by WCF. A static MapperConfig class is called by the service behaviour.
namespace ServiceLayer
{
using System;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using LAI.Data.Controllers.Mappers;
/// <summary>
/// Registers AutoMapper for the calls to data controller methods.
/// </summary>
public sealed class AutomapServiceBehavior : Attribute, IServiceBehavior
{
/// <summary>
/// Provides the ability to pass custom data to binding elements to support the contract implementation.
/// </summary>
/// <param name="serviceDescription">The service description of the service.</param>
/// <param name="serviceHostBase">The host of the service.</param>
/// <param name="endpoints">The service endpoints.</param>
/// <param name="bindingParameters">Custom objects to which binding elements have access.</param>
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
MapperConfig.RegisterMap();
}
/// <summary>
/// Provides the ability to change run-time property values or insert custom extension objects such as error handlers, message or parameter interceptors, security extensions, and other custom extension objects.
/// </summary>
/// <param name="serviceDescription">The service description.</param>
/// <param name="serviceHostBase">The host that is currently being built.</param>
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
}
/// <summary>
/// Provides the ability to inspect the service host and the service description to confirm that the service can run successfully.
/// </summary>
/// <param name="serviceDescription">The service description.</param>
/// <param name="serviceHostBase">The service host that is currently being constructed.</param>
public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
}
}
}
namespace DataAccessLayer
{
using AutoMapper;
/// <summary>
/// Represents the mapper configuration.
/// </summary>
public static class MapperConfig
{
/// <summary>
/// Registers the map.
/// </summary>
public static void RegisterMap()
{
Mapper.Initialize(map =>
{
map.AddProfile<ProfileOne>();
map.AddProfile<ProfileTwo>();
});
Mapper.AssertConfigurationIsValid();
}
}
}
namespace ServiceLayer
{
using System.ServiceModel;
/// <summary>
/// Represents a service.
/// </summary>
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession), AutomapServiceBehavior]
public partial class Service : IService
{
/// <summary>
/// Do stuff.
/// </summary>
public void DoStuff()
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment