1
votes

Question Back ground:

I have a project consisting of a DLL, a web service and a test project. Currently I'm simply trying to set the web service constructor string parameter through the use of Unity.

The issue & error:

The following shows the constructor structure of the web service class 'VersionControlFacade':

 private string _url;

 public VersionControlService()
 {
 }

 public VersionControlService(string url)
 {
    _url = url;
 }

If I simply pass the 'url' parameter as shown in the following code it is fine:

 var newVersionControlFacade = new VersionControlFacade(@"http://somelink.com");

When I try and pass it using my Unity IoC container as shown:

 var container = new UnityContainer();
 container.RegisterType<IVersionControlFacade, ProxyHandler>();
 container.RegisterType<Proxy>();
 container.RegisterType<VersionControlService>(new InjectionConstructor(@"http://somelink.com"));
 return container.Resolve<IVersionControlFacade>();

The following error is being throw stating that the web service does not contain a constructor that takes a string parameter even though it clearly does.:

Result Message:

Test method Test_Unity threw exception: System.InvalidOperationException: The type VersionControl.WebService.VersionControlService does not have a constructor that takes the parameters (String).

Can anyone steer me in the right direction here? Do I need to re-add the web reference for it to see this new constructor?

Try reading the web reference, Also to debug try to call the class directly, at least you will see if the constructor is being seen. - Lawrence Thurman
Here is a sample with injection constructor and it works fine for me - dotnetfiddle.net/Vb1uEK - Sergey Litvinov
@LawrenceThurman Not sure I understand by 'reading the web reference could you elaborate please. I have called the class directly and it works, it just seems to be trying to set it through Unity. - user1352057
@SergeyLitvinov Thanks for your example. I have used unity the way you have shown and the way I have shown many times, but I've never attempted to set a web service constructor using Unity and this is where the issue is coming in. - user1352057
sorry - re-add the web service. - Lawrence Thurman