3

I have a C# project which has many web references to a third party product. All of these web service calls use a 'user context' class. So each web service accepts the exact same XML snippet.

Currently I have to keep many of these 'user context' objects around as I hit all the different web service calls. The generated 'user context' class is the exact same but just put in different .NET namespaces (the XML namespace is the same).

I would like to be able to have only one of these instances passed around. How could I do that?

I am using Visual Studio 2010 and using the generated class by adding a WebReference. I'm not opposed to using some other framework or mechanism. I had even thought of rolling my own code, but there are many WebServices (each with many web methods) that I will need to use.

Update: I originally added these by adding a Web Reference, I will now be adding them as a Service Reference. The web services themselves are exposed through InterSystems Caché and I haven't looked into how well they conform to WS-I.

Chad Gorshing
  • 2,998
  • 32
  • 30
  • +1 I have also encountered this and ended up using Automapper to copy the namespace-specific class versions into each other as required -- it worked, but I was sure there was a better way ... – Dr Herbie Jul 01 '10 at 15:08

2 Answers2

2

Not sure about VS 2010, but in 2008 there was an option "reuse types in referenced assemblies" in the service reference adding wizard which does just what you need. In order to have the same type in all web references create an assembly, put those type definition there, reference it from your project and then add web references you need.

Konstantin Oznobihin
  • 5,234
  • 24
  • 31
  • I hadn't looked at those options before, so I tried them out and read about them. I do not think this is what will work for me. From what I've read this typically pertains to types already defined in a shared class library (which I do not have). These types are part of the web services themselves and not any shared library that I have written or have access to. Please enlighten me if I am wrong in the functioning of this option. – Chad Gorshing Jul 01 '10 at 18:05
  • You are quite right about this option, but you miss one thing: you can create necessary shared class library by yourself. It's very easy actually: 1. Create class library project 2. Add web reference to any of your services with types you'd like to share setting access level to public 3. Reference this class library in your project. And that's all. All web references you'll add to your project from now on will use types from your class library. – Konstantin Oznobihin Jul 01 '10 at 19:24
  • I will accept your answer, but for me it still did not quite work out. I created a simple WCF service and was able to limit down the amount of duplicate code being created, which is what you suggested. However, for the web service I'm trying to code against this does not work. There must be something else that keeping this from working. – Chad Gorshing Nov 10 '10 at 12:03
0

I resolved a problem like this by using only one class and converting the object with XML serialization/deserialization. But still it's not the perfect solution.

remi bourgarel
  • 9,231
  • 4
  • 40
  • 73