Invalid type owner for DynamicMethod

May 14, 2008 at 1:31 AM


How can I implemenet Unity in Constructor dependency injection using WCSF View-Presenter Pattern ?

Below is my web.config learning code:
    <section name="unity"
                   Microsoft.Practices.Unity.Configuration" />

          <type type="UnitySamples.ILogger,Shell" mapTo="UnitySamples.ConsoleLogger,Shell">
            <lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,
                            Microsoft.Practices.Unity" />

If I do the following in global.asax.cs:

  IUnityContainer container = new UnityContainer();
  UnityConfigurationSection section;
  section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

  ILogger logger = container.Resolve<ILogger>();

it works fine and "logger" variable is substituted with 'ConsoleLogger".

But when I try to inject it into a controller constructor (created using WCSF Business module recipe):

  public BusinessModuleController(ILogger logger)

It will throws "Invalid type owner for DynamicMethod." error.

However, it will works fine otherwise if I add the following globalService code in BusinessModuleModuleInitializer:

  protected virtual void AddGlobalServices(IServiceCollection globalServices)
    // TODO: add a service that will be visible to any module
    globalServices.AddNew<ConsoleLogger, ILogger>();

But this is not using Unity value as configure from the web.config.

I must be missing something and has spend a day on it, cannot find any useful hint on the net, any help ?


May 14, 2008 at 5:21 AM
Just because you've put an instance of UnityContainer in your web app doesn't mean that WCSF will use it for anything. WCSF uses the Composite Web Application Block (CWAB) for it's DI container. As a result, any pages done with WCSF will go through the CWAB container, not the Unity one. globalServices.AddNew<> for example, adds the service to the CWAB container, not the Unity one.

Michael Puleio is writing a blog series about replacing CWAB with Unity in the WCSF. Check those out.

May 14, 2008 at 4:46 PM
Oh, thank you. I keep thinking (after reading the internet) that I can switch to use Unity on WCSF's DI functionality, when it isn't yet. Michael Puleio blog is great for his details explanation on his work too.

So will not able to use it yet in my current project, but do look forward for its release version soon.
May 18, 2008 at 6:49 PM
What I am doing with WCSF on my blog is a proof of concept/thought experiment.  There are no plans currently to make it an official release, and it is unsupported.
If you really want it to be an official release, go to and in the Issue Tracker there, find the work item that is something like "Add Unity Support", and vote for it. :)
If enough people ask for it, it may happen.
Jun 5, 2008 at 4:41 PM
Big thank you to your work and Unity is great. I just cast my ever first codeplex vote in 'Support for the new Unity Dependency Injection Container'. 

Still thinking of how to switch my existing project to fully change to OB2 and Unity from CWAB.  Try taking a break from my tight project work, to see any latest news on Unity. Google the internet for some WCSF bundled Unity Recipe and back to this post again.