Two web services + same objects = It's time to discošŸ•ŗ [How to create a proxy class between wsdls]


csharp english exe wsdl

TLDR;

Create discomap files with the following command:

disco.exe "file:///[Local path to file]\file.wsdl"

Use the wsdl.exe to create proxy classes with the following command:

wsdl.exe /shareTypes [path to discomap file for ws 1] [path to discomap file for ws 2]... /o: [path to proxy class] /n: [namespace of proxy class]

ā­ā­ā­

THE JOURNEY TO OBTAIN THE KNOWLEDGE TO CREATE THE TLDR;

Letā€™s say that you have two or more web services that you need to use in your app. You download them into your project, implement the first one and when youā€™re going to implement the second one, you realize that it has the same objects as the first, and that they need to be filled up exactly the same way but, because theyā€™re inside their own wsdl, you would need to do it separately. Effectively making your code *wet*.

As you can imagine, I was not having any of this when I found myself facing this exact problem. So I headed into googlelandia and set up a trip to find a solution for this. Ah yes the first thing youā€™ll read is to use the wsdl.exe command ā€œ/sharedtypeā€. Thatā€™s it. Thatā€™s the solution, so why did it take me a day to make it work?

Because Iā€™m my teamā€™s Jon Snow thatā€™s why. So, I decided to create this guide for all of the programming Jon Snows out there. No need to thank me.

Yet.

We don't pet doggies here

we donā€™t pet doggies here

First things first. You need to know what a discomap is. Itā€™s an extension, an xml file that allows your web service to be discoverable and eeehā€¦ Iā€™m going to honest, I didnā€™t and donā€™t care much about what its true life purpose is but, because Iā€™m dumb and didnā€™t know the file:/// extension to create local URI paths, I learned how to create them since I only had the WSDL file.

So, boot up you visual studioā€™s command prompt and paste whatever code you think youā€™ll need based on that one lonely SO answer:

wsdl.exe /sharedtypes <path to first webservice wsdl file> <path to second webservice wsdl file>

Oh yeah, since Iā€™m, like, ME. I headed to the Package Manager Console first šŸ˜‚

Tip #1: THE FOLLOWING IS NOT A COMMAND PROMPT:

not a command prompt

You can find the actual command prompt here:

tools > command line > developer command prompt

And it should look like this:

the developer command prompt

Now CD that into your web service folder, maybe itā€™s called ā€œWeb Referencesā€. Right outside there. Nothing badā€™s going to happen I SWEAR.

So run that code.

Bam bam bam, it throws a million errors at ā€˜cha. Because you CDā€™d wrong.

Prompt: "What do you mean WEB(SPACE)REFERENCE?"

Since youā€™re stressed out, know nothing about CMD except for the command ā€œipconfigā€ and ā€œpingā€ and, need to finish like pront-o, I suggest you CD by folder like this:

cd /Folder1

cd /Folder2

...

It works. Long live practicality.

Now run your code!!

.

.

.

IT. DOESNT. WORK. AGH. šŸ˜”

Since you donā€™t know about the file:/// to create a URI for a local file, itā€™ll tell you that it needs a disco. FORREAL. Itā€™s telling you that it needs a discomap file, but you only have your WSDL file. What to do? you use the Web Services Discovery Tool (Disco.exe), thatā€™s what. 

In the same command prompt, run this code:

disco.exe "[Local path to file]\file.wsdl"

Ah! an error message. Remember that Iā€™ve been saying that I didnā€™t know how to make local paths into URIs? At this point I had to finally learn that ā€œ file:///ā€ exists. Once I did learn it though, I had to think long and hard if I should try using it for the wsdl.exe with it but, I decided to learn how to  create discomap files for wsdls. Valuable.

The new code will be like this:

disco.exe "file:///[Local path to file]\file.wsdl"

Cool, the discomap files should create itself in the same folder as the WSDL. Now we can use the to create our proxy class in the wsdl.exe tool.

But before creating it, Iā€™m going to give you two more tips.

Tip #2: The /o: command letā€™s you set a filename or a directory name. I for one, had my own proxy class already created so I set the path to it. If you donā€™t set it, itā€™ll create a proxy class probably named ā€œresults.csā€ in the folder you CDā€™d in.

Tip #3: The /n: command lets you specify the proxy classā€™s namespace. Unless you want it set as global (note: this can cause issues with other web services not part of the proxy) use this.

The code now looks like this:

wsdl.exe /shareTypes [path to discomap file for ws 1] [path to discomap file for ws 2]... /o: [path to proxy class] /n: [namespace of proxy class]

If you get any schema issues ignore them. I promise you the proxy will work. I wasted like an hour in trying to fix it and itā€™s simply not worth it, especially if itā€™s not your own wsdl. A small note, sometimes itā€™ll create the same object but with sequential numbers like 1, 2, 3ā€¦ I got this because I went crazy and created a proxy for more than 4 web services. I donā€™t really know how to fix it but just a small warning.

Anyway, hopefully after going through this small journey with me youā€™ll understand how I was able to create the proxy. It took me a day because thereā€™s not a lot of decent explanations on the subject/problem so what Iā€™m explaining here is a summary of everything I went through to get it to work.

Now go on and enjoy the proxy class šŸ˜‰

Jon petting Ghost, the good boy

We have now acquired the ability to pet doggies