简体   繁体   中英

Unity Cannot find Constructor of partial class

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? :)

You question is not WCF related, it is a Unity related. I added some samples that might help you to create a class with parameters.

Register Instance samples

This will register an existing class instance to the container, anyone that will resolve for ILogger, will get the same instance.

container.RegisterInstance(typeof(ILogger), logger);   

Register Type samples:

Register a type and class, on each Resolve and new instance will be created.

container.RegisterType<ISession, Session>();  

In order to register a singleton class, any Resolve will receive the same single EventAggregator class use a life time manager:

container.RegisterType<ILogger, Logger>(new     ContainerControlledLifetimeManager());

• This is a specific Lifetime manager that creates a single instance of the registered type in the container; in general there are other options for Lifetime managers

Examples on register and resolve:

When you create a class and want to DI the container itself to the class upon construction, you have 2 ways to do it:

  1. Inject the container in constructor and save it as a private member: We register (this example its singleton and constructor takes 2 parameters): _container.RegisterType(new ContainerControlledLifetimeManager());

The class constructor is:

 public SessionProvider(IUnityContainer container)
    {
        _container = container; //private IUnityContainer member
    }

When we resolve, pass the parameter to constructor as DependecyOverride:

 _sessionProvider = _container.Resolve<ISessionProvider>(new DependencyOverride<IUnityContainer>(_container));

Additional way to use the container and DI:
If we don't want to pass parameter to constructor, we can DI them using the [Dependency] attribute.
This will create a new Session class on each resolve

container.RegisterType<ISession, Session>();

Instead of having a Session(IUnityContainer container) constructor, we set attribute in class:

[Dependency]
public IUnityContainer Container { get; set; }

This way, every time the container creates new object, it will set the Container property upon construction without passing it as parameter in the Resolve<> So we don't need to declare a constructor, the container will instantiate a class and set the container property upon instantiation. The problem is that it must be public.

Try this format:

IUnityContainer container = new UnityContainer().RegisterType<ProductMasterServiceClientClient>(
    new InjectionConstructor(username, password, test));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM