I use Automapper in my WCF Services to map from database entities to data contracts. For each service I create a static AutomapBootstrap class with a method to InitializeMap. Then for each service, I decorate the service with an AutomapServiceBehavior attribute.
I do not know if this will work for your scenario because WCF Data Services is a little different from vanilla WCF SOAP services and services using WCF WebBindings.
However, its worth a look.
This is the Service Behavior
[CoverageExclude(Reason.Framework)]
public sealed class AutomapServiceBehavior : Attribute, IServiceBehavior
{
public AutomapServiceBehavior()
{
}
#region IServiceBehavior Members
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,
Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
AutomapBootstrap.InitializeMap();
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
}
public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
}
#endregion
}
This is my mapper
public class AutomapBootstrap
{
public static void InitializeMap()
{
Mapper.CreateMap<CreateBookmarkRequest, TagsToSaveRequest>()
.ForMember(dest => dest.TagsToSave, opt => opt.MapFrom(src => src.BookmarkTags))
.ForMember(dest => dest.SystemObjectId, opt => opt.UseValue((int)SystemObjectType.Bookmark))
.ForMember(dest => dest.SystemObjectRecordId, opt => opt.Ignore());
}
}
this is how I wire up my service to automap
[AutomapServiceBehavior]
[ServiceBehavior(InstanceContextMode = InstanceContex
answered 2010-12-05T05:28:34.080