Resolving wcf service instances with an IoC container

In wcf, services are created and disposed by the wcf runtime through an IInstanceProvider implementation. The default implementation calls the parameterless constructor of the service. The easiest way to provide our own IInstanceProvider is to add a behavior to our service host by using an IServiceBehavior.

We can achieve our goal as simple as this:

public sealed class IoCInstanceProvider : IInstanceProvider
{
    private readonly Type serviceType;

    public IoCInstanceProvider(Type serviceType)
    {
        this.serviceType = serviceType;
    }

    public object GetInstance(InstanceContext instanceContext)
    {
        return GetInstance(instanceContext, null);
    }
    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        return IoC.Current.Container.Resolve(this.serviceType);
    }
    public void ReleaseInstance(System.ServiceModel.InstanceContext instanceContext, object instance)
    {
        if (instance is IDisposable)
            (instance as IDisposable).Dispose();
    }
}

The IoC class is just a singleton I use that exposes an Inversion of Control container like Unity.

Continue reading

Managing the Entity Framework ObjectContext instance lifetime in WCF and sharing it among repositories

As I pointed out in a previous post about using the Repository pattern in EF and WCF, it’s a bad practice when service operations manage the ObjectContext instance lifetime and have dependencies on it.

In this post I’m going to show you how to share the same ObjectContext among repositories as long as the service is activated (either PerCall or PerSession) and have the service totally agnostic of it.

First lets introduce two concepts:

public interface IDbContext : IDisposable
{
    object Context { get; }
}

public interface IDbContextManager
{
    IDbContext GetDbContext();
}

The IDbContext interface can be used as a wrapper for the ObjectContext or even NHibernate’s ISession. The IDbContextManager will be used by the repositories to get an IDbContext instance.

To simplify things we can extend our existing ObjectContext class using a partial class that implements the IDbContext interface. In this case, since I’m using the same example as the previous post it should look like this:

public partial class MFEntitiesContainer : IDbContext
{
    public object Context
    {
        get { return this; }
    }
}


Lets see how the refactored EntityRepository class looks like:

public class EntityRepository<TEntity> : IRepository<TEntity>
    where TEntity : EntityObject, new()
{
    public EntityRepository(IDbContextManager manager)
    {
        this.SetContext(manager.GetDbContext());
    }

    private void SetContext(IDbContext ctx)
    {
        this.ObjectContext = ctx.Context as ObjectContext;
        this.FetchEntitySetName();
    }

    //.. same implementation as the previous post
}

So now we can use our repositories without having to worry how to give them an ObjectContext anymore. We simply have to supply an implementation of IDbContextManager. In this case, I’m going to implement a DbContextManager that will return the same IDbContext instance while the wcf service is activated in order to reuse it among repositories.

Continue reading