Invalid type owner for DynamicMethod

Mar 26, 2008 at 1:45 PM
<type type="Adf.Process.ITask, Adf.Process" mapTo="Synopsis.Process.MainTask, Synopsis.Process">
<lifetime type="Singleton"></lifetime>

Error: System.ArgumentException: Invalid type owner for DynamicMethod.

Now I already debugged this problem and I suspect it has something to do with a constructor in one of the dependencies that takes a string as input parameter.

Overview of the classes and constructors involved
public MainTask() : base(ApplicationTasks.Main, Empty)

public MainTask(Tasks name, ITask origin) : base(name, origin)
I suspect that Unity uses the constructor "public MainTask(Tasks name, ITask origin)".

Tasks has the following 2 constructors:
public Tasks()

public Tasks(string name)
Name = name;

Once again I suspect that Unity takes this constructor "public Tasks(string name)"
As far as I can follow in my debug session I think Unity tries to resolve the dependency on "string". It fails while doing that.

Is there somehting I am doing wrong here, or is it a bug?

Mar 26, 2008 at 2:44 PM
Edited Mar 26, 2008 at 2:46 PM
In your config, you need to add a nested element to the <type> node:

By default (if no InjectionConstructorAttribute specified), Unity uses the longest ctor. If want to either parameterize this ctor through config or use another one, you can through the <typeConfig> element.

For example, if your MainTask took only a name of type string:

<type type="Adf.Process.ITask, Adf.Process" mapTo="Synopsis.Process.MainTask, Synopsis.Process">
  <lifetime type="Singleton"/>
  <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
      <param name="name" parameterType="System.String, mscorlib">
        <value value="myName"/>

Or remove the <param> if you want to use the default ctor.