Unity Cannot find Constructor of partial class!

Jun 24, 2013 at 11:45 AM
Edited Jun 24, 2013 at 11:47 AM
Hi there,
I have a problem whereby I have a WCF service reference and wish to inject it into MVC controllers in a client. This works fine when I don't want to specify parameters, but I have since added username and password credentials to the service so these have to be set like so:

MyServiceRef.ClientCredentials.UserName.UserName = username;
MyServiceRef.ClientCredentials.UserName.Password = password;

I decided to try use Unity to handle the username and password by creating a partial class of my service reference with constructor with 2 strings as parameters for username and pwd, and one for isTest (Bool).

public partial class ProductMasterServiceClientClient
       public ProductMasterServiceClientClient(string username, string password, bool test)
            ClientCredentials.UserName.UserName = username;
            ClientCredentials.UserName.Password = password;
and setup Unity like so:
container.RegisterType<IProductMasterServiceClient, ProductMasterServiceClientClient>(new InjectionConstructor(new InjectionProperty("username", "test"),new InjectionProperty("password", "password"), new InjectionProperty("test", true)));
But it doesnt work! I get the following error for some reason :(
The type com.luzern.co40.web.pm.wsProductMasterService.ProductMasterServiceClientClient does not have a constructor that takes the parameters (InjectionProperty, InjectionProperty, InjectionProperty).
Anyone any idea why its not working for me? :)
Jun 24, 2013 at 12:47 PM
Try passing the values that you want directly:
container.RegisterType<IProductMasterServiceClient, ProductMasterServiceClientClient>(new InjectionConstructor("test", "password", true));
This will always pass the same values into the constructor.

Another approach would be to use a ResolvedParameter to tell Unity to resolve the parameter from the container instead of using a hard coded value.

Or you could use an override when resolving:
// InjectionConstructor not necessary if calling constructor with most arguments since Unity will use that as default
container.RegisterType<IProductMasterServiceClient, ProductMasterServiceClientClient>(new InjectionConstructor(typeof(string), typeof(string), typeof(bool)));

    new ParameterOverride("username", "test"),
    new ParameterOverride("password", "password"),
    new ParameterOverride("test", true),
See Dependency Injection with Unity for a more complete discussion.

Randy Levy
Enterprise Library support engineer
Support How-to