since 1997 a place for my stuff, and if it helps you then so much the better

 
...
...

Web Services with custom objects


I have no problem with simple web services that return primitives and usually that's all I've ever needed. But I just started working on a project that has to pass a custom enum and I'm stumped.

The enum comes from a dll that is installed on the web service server and referenced by the web service and the same version dll is referenced in the client program. When I make the call to return a value from the web service I get an error that "MyCompanyNamespace.testservice.MySpecialEnum can not be converted to MySpecialEnum". "testservice" is what I told Visual Studio to name my web reference but both sides are using the same dll so why is the enum being reported as that type instead of the "MyCompanyNamespace.MySpecialEnum" that it IS and that I can USE?

I've been all through the code including the autogenerated sections and can't see where the type name is being messed up. Is there any way to get this crap to work?

 

When you add a web reference in Visual Studio, the wizard does the WSDL hit and generates a proxy file and part of that file has copies of all of the types (enums, classses) required by the public methods. These copies are the types that end up being namespaced with the service reference name in your clients. All you have to do is edit the proxy to remove these copies and add an Import(s) to bind your shared dll's namespace(s) during compilation.

The good part is that the proxy generated by VS is left as code in an text file instead of automatically being compiled to a closed dll, and that file is easily edited. The annoyng part is that you may have to go looking for the file.

First off, if you haven't already, set your web reference and give it a name. Then enter some code to call one of the web service's functions. Right click on the function call and choose "Go to Definition", this *might* load the proxy file "Resource.vb" into visual Studio. If not, try this instead...

Set your web reference, if you haven't already, then browse to and open the folder that holds the source code to the project that will be a client of the service (the one that has the web reference). Open the "Web References" folder and look for the subfolder named the same as your web reference (so in your example, you'd look for the "testservice" folder). Inside of that is the "Reference.vb" field (or Reference.cs). Open that file in notepad.

Now it's just the editing. Scroll down till you see the definitions of the copies of your custom types, select them all and delete them. At the top of the file add in an Imports (c#: Using) to link the proxy to the namespace that your enums are in in the shared dll.

Recompile and give it a step-through.

One thing to remember. As you're working on your solution you might need to update the code in your web service and then of course you'll have to refresh the web service reference in VS for the client project. When you do that you could overwrite the Resource.vb file and you'll have to go back in and manually edit it again.

If you try to give MS the benefit fo the doubt you might find that the weirdness kind of makes sense from one angle. When you're adding a reference, you're saying that you're a consumer of some data that already exists in a specific structure 'out there somewhere' and maybe MS figured that most often you won't also be the developer of the service's dlls so you wouldn't usually have locally defined objects to synch up with. Ok, fine.

Hope that helps.

Robert Smith, Kirkland, WA

 

added to smithvoice august 2005


...
...

"In theory, theory and practice are the same. In practice, they are not." -Albert Einstein