Reusing types with Visual Studio Service Reference.

I am sure this is something many folks would have come across by now. But what the heck, I thought of adding some more crap on the internet. So, this is what I want to do, I have a shared library called MySharedLib.dll. I have added a reference to it from my Console Application project. Now I want to add a WCF Service reference to my application, but the service internally uses MySharedLib.dll too. So you say whats the problem?? For newbs, if you add a reference to such a service without doing “something” in Visual Studio, the proxy generated would have defined the same type in its namespace. This type would be incompatible with the type which is in MySharedLib.dll. Enough blabbering, here is some code

public ConsoleApplication3.ServiceReference1.Assignment[] GetAssignments(ConsoleApplication3.ServiceReference1.User authenticatedUser) {
return base.Channel.GetAssignments(authenticatedUser);}

public ConsoleApplication3.ServiceReference1.AppFolder[] GetAppFolders(ConsoleApplication3.ServiceReference1.Assignment userAssignment) {
return base.Channel.GetAppFolders(userAssignment);

The above classes like Assignment, User, AppFolder are not the ones from MySharedLib.dll. They are generated for the proxy by Visual Studio. If you try to assign this type to a variable of type in MySharedLib.dll they would obviously be incompatible. If you want Visual Studio to reuse the types in a shared library in its proxy do the following –:

In your project to which you need to add a service reference, add a reference to the shared dll you need to reuse.
On Add Service Reference dialog, click Advanced,

Capture1


And this,

Capture2



Happy Programming!

Abhang Rane


1 comment :

Reader's Comments