1

I have an application with a GenericHandler and would like to inject dependencies using Unity. No matter what I try I get the error:

[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean 

I have tried to follow the example at http://geekswithblogs.net/Rhames/archive/2012/09/11/loosely-coupled-.net-cache-provider-using-dependency-injection.aspx.

My constructor for the handler is as follows:

    public class GetPerson : IHttpHandler
    {
        private IPersonRepository repo;
        public GetPerson(IPersonRepository repo)
        {
            this.repo = repo;
        }

IPersonRepository is implemented by CachedPersonRepository. CachedPersonRepository wraps the PersonRepository (which is used for DataAccess if an item cannot be found in the cache). Both CachedPersonRepository and PersonRepository are IPersonRepository:

public class CachedPersonRepository : IPersonRepository
{
    private ICacheProvider<Person> cacheProvider;
    private IPersonRepository personRepository;

    public CachedPersonRepository(IPersonRepository personRepository, ICacheProvider<Person> cacheProvider)
    {

This IPersonRepository personRepository is parameterless.

ICacheProvider<Person> is implemented by MemcachedCacheProvider<T>:
public class MemcachedCacheProvider<T> : ICacheProvider<T>
{
    public T Get(string key, Func<T> retrieveData, DateTime? absoluteExpiry, TimeSpan relativeExpiry)
    {

I have tried unsuccessfully to initialise the Unity Container in my Global.asax file Application_Start. DI is new to me and I would very much appreciate any advice on where I'm going wrong.

2
  • Could you post the Unity configuration method (which you call in Application_Start)? This may just be a configuration error. Commented Mar 9, 2014 at 10:03
  • Thanks Damon, I've managed to solve the problem and will post the solution below. Commented Mar 9, 2014 at 22:39

1 Answer 1

2

There were actually two issues here.

Firstly, CachedPersonRepository uses the Decorator pattern which I didn't properly understand before. Once I understood this I was able to register and resolve the PersonRepository appropriately using this configuration:

public static void Configure(IUnityContainer container)
    {
        container.RegisterType<ICacheProvider<Person>, MemcachedCacheProvider<Person>>();

        container.RegisterType<IPersonRepository, PersonRepository>("PersonRepository", new ContainerControlledLifetimeManager());

        container.RegisterType<IPersonRepository, CachedPersonRepository>(
            new InjectionConstructor(
                    new ResolvedParameter<IPersonRepository>("PersonRepository"),
                    new ResolvedParameter<ICacheProvider<Person>>()));

        container.Resolve<IPersonRepository>();
    }

Having fixed this I still saw the same "No parameterless constructor defined for this object" error.

The reason for this, is that I was working with an IHttpHandler and it is not possible to inject dependencies in the constructor.

I got around this by using Property injection:

A Repository property with the Dependency Attribute has been added to the GetPerson handler:

public class GetPerson : HandlerBase
    {
        [Dependency]
        public IPersonRepository Repository { get; set; }

A new http module was needed to check for requests from handlers which implemented my HandlerBase:

public class UnityHttpModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;
    }

    public void Dispose() { }

    private void OnPreRequestHandlerExecute(object sender, EventArgs e)
    {
        IHttpHandler currentHandler = HttpContext.Current.Handler as HandlerBase;

        if (currentHandler != null)
        {
            HttpContext.Current.Application.GetContainer().BuildUp(
                currentHandler.GetType(), currentHandler);
        }
    }
}

Resources:

http://download.microsoft.com/download/4/D/B/4DBC771D-9E24-4211-ADC5-65812115E52D/DependencyInjectionWithUnity.pdf (Chapter 4, pages 60-63)

http://msdn.microsoft.com/en-us/library/ff664534(v=pandp.50).aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.