Reuse existing classes from another Service Reference
Asked Answered
C

1

11

The situation that I'm working with is suboptimal, so bear with me..

We are to consume external (non .NET) SOAP-webservices.
For some reason, each method is implemented in a separate web service with it's own WSDL.
A lot of the objects returned by the web services have the exact same design, name and xml namespace.

"Add Service Reference" is used because:
- the internal dev team are used to it
- we'd like to keep the automatic "Update Service Reference"

The 2nd reference is added with the "Reuse types in all referenced assemblies" option on.
All of the classes are however generated again under the new reference.

Is where a way to reuse the classes from the 1st reference?

Colosseum answered 19/2, 2013 at 13:54 Comment(3)
Do you mean reuse classes like data classes? Probably not, since each client proxy is in its own namespace. If you're sharing common data objects, what you really need to do is to either hand-code the client proxy class or make your own client proxy generator. .NET really makes it fairly easy to do that sort of thing. You could even leverage something like T4 to help generate the code.Perfidious
I do mean the data classes and the problem at hand are idd the namespaces per proxy. How would I go about an own client proxy generator?Colosseum
possible duplicate of svcutil exlude/reuse refrenced assemblies. Have you tried svcutil.exe /r?Unsex
B
21

You can add multiple WSDLs if you open your Service Reference's Reference.svcmap (to find it, check "Show All Files" and expand your service reference) and edit it manually, rather than setting the WSDL URI through the GUI. E.g.

  <MetadataSources>
    <MetadataSource Address="http://localhost/example1.wsdl" Protocol="http" SourceId="1" />
    <MetadataSource Address="http://localhost/example2.wsdl" Protocol="http" SourceId="2" />
  </MetadataSources>

When you have multiple WSDLs in one Service Reference, classes will be reused as you describe, as long as they're identical in the different WSDLs. If there are classes with the same name but different definitions, one of them will have a '1' suffix appended to its name.

Boucher answered 19/2, 2013 at 14:20 Comment(6)
Thanks, exactly what I needed! Classes with the same name but different definition are handled like so: ClassName, ClassName1. Would you happen to know a way to control the suffix ('eg' in stead of '1') ?Colosseum
You might try writing your own client proxy generator. When you find a class ending in a '1', change it to the suffix you prefer. See primordialcode.com/blog/post/… for a start in that.Boucher
I have only 1 WSDl in 1 service reference in svcmap but it is still generating some attributes with 1 suffix. what could be the reason? any idea?Booklet
@batmaci Sometimes old files hanging around can cause this. If it bothers you, you can delete all the items besides Reference.svcmap from that folder, make sure they're all deleted from the project, delete all the references to them in the Reference.svcmap file (you should see a section for them) and then update the service reference. It should regenerate the files cleanly.Boucher
@TimS. I finally figured out why it was persisting for me. I was having included xsd files and proxy classes generated from them and my those included proxy classes were having different namespace than parent proxy classes/xsds. So I have just equalized namespaces and It worked fine. no more multiple properties with 1 suffix.Booklet
@TimS. Thank you so much. I stumbled upon this and while it didn't fix any bugs, it made my code much much cleaner.Hodman

© 2022 - 2024 — McMap. All rights reserved.