Question

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
}
Was it helpful?

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.

OTHER TIPS

Instead of having A.com.test.services.Task and 'B.com.test.services.Task you could use A.com.test.services.Task in the B library. So you should add a reference to A in the B. Then where ever you used the B.com.test.services.Task in your B library you replace it with A.com.test.services.Task. That way your object can be past back and forth between the libraries because the type is the same.

Or in B you can convert A.com.test.services.Task to B.com.test.services.Task if the properties match like the following:

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

public string BuildDetail(B.com.test.services.Task task, bool TaskExecutionState, bool TaskComment) 
{
    //create a new Task of the A library
    A.com.test.services.Task tempTask = new A.com.test.services.Task();

    //fill the task with the properties of B
    tempTask.propOne = task.propOne;
    tempTask.propTwo = task.propTwo;

    //call the function which takes an A task and return that.
    return BuildDetail(tempTask, TaskExecutionState, TaskComment);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top