Search code examples
c#web-servicesweb-reference

Passing web reference objects between dll's


I have a project with two dll's (I'll name them A & B).
In the first dll I want to pass a object of a web reference to my other dll.
The two dll's use properties from the object so I have added a web reference to the service in both my dll's.

But when I want to pass this object I get an error:

The best overloaded method match for 'method in dll B (B.com.test.services.Task)' has some invalid arguments.

So my dll A expects it to be a object of type A.com.test.services.Task while it gets an object of type B.com.test.services.Task.

How do I fix this?

Some code:

Dll A:

using A.com.test.services
public string BuildDetail(Task task, bool TaskExecutionState, bool TaskComment) 
{
    DetailScreen detail = new DetailScreen(task);   //error is here.
    return detail.Layout;
}

Dll B:

using B.com.test.services 
public DetailScreen(Task task)
{
    //some code
}

Solution

  • The problem is that creating a web reference means that proxy classes are created for each types of the referenced service.

    Suppose you have your dll, A.dll and you create a web reference to a service with the type Task. Inside A, a proxy type, let's call it A.Task will be created. The type Task and A.Task are two different types, Task exists on the server, A.Task exists on the client

    Then, you have another DLL, B.dll and once again you add a web reference. This time, yet another proxy type will be created, let's call it B.Task. And although A.Task and B.Task probably look alike, they are two different types (most probably they exist in two different namespaces).

    There are two possible approaches. First - create your own mapping classes with methods which take A.Task and make B.Task out of it (and the other way around possibly).

    But the other approach involves creating a "common language" - a shared DLL (let's call it Task.dll) where you put your Task class. You reference the DLL everywhere, in your webservice, in A.dll and in B.dll. Everytime you create a web reference, you make sure that the option "Reuse types from referenced assemblies" is checked (at reference properties page).

    This way, there will be no proxy types created for the type Task - the same class will be used at the server side and at the client side. There will be then no need to convert anything and you will be able to pass references around.