lincolnk
lincolnk

Reputation: 11238

How do I make my client app recognize an object from my webservice as a type it already knows?

So I have a webservice that returns a custom type

public List<MyNS.Product> GetProducts(string filter)
{
    ...
}

MyNS.Product is defined in another assembly. My client app references the same assembly. When I add a service reference to my client app, the return type is MyServiceReference.Product.

var client = new MyServiceReference.dataSoapClient();
var products = client.GetProducts("derp");
// products type is MyServiceReference.Product[]

How do I get my client app to recognize that returned object of type MyServiceReference.Product is actually the MyNS.Product type it already knows about? (the list to array part isn't a big deal, just the T type)

The webservice is the regular asmx as part of a web site. I'm doing "add service reference" to add the service to my client project within visual studio.

edit: here are the options from the "advanced" button in the add service referance dialog. I changed the collection type from Array to List. I also tried specifically selecting my library with the Product type but that didn't help either.

add service reference options

Upvotes: 3

Views: 3760

Answers (5)

kindaska
kindaska

Reputation: 156

Well, i know it is an old post but (unfortunately) i've the same problem, so i thought that i could share my solution.

PROLOGUE

I'm using asmx web service (.net version 2.0) and i've a web method that return a custom type (with the [serializable] attribute) that is referenced in an assembly that both the projects (actually web service and client) share.

The solution i've found, is to let visual studio create the client for you (in the client project), and then modify the Reference.cs file created (to see it you have to turn on "show all files").

N.B. also the client project's .NET version has to be 2.0

BUT BEWARE!!

being it an autogenerated file, if you update the web service reference all the changes WILL BE LOST! so pay a LOT of attention.

As you can see in this file(reference.cs), the custom type is recreated with all the properties and, if the class inerith from another one (as in my case), also the base class is recreated. The obviously problem is that the recreated types are NOT the same of the shared assembly.

The solution i've found is to rename the generated classes (for ex: put an "_" before the names [YOUR_BASECLASS_NAME => _YOUR_BASECLASS_NAME]) and then in the "using" part of the Reference.cs put something like this :

using YOUR_BASECLASS_NAME = SHAREDLIBRARY.BASECLASS
using YOUR_CLASS_NAME = SHAREDLIBRARY.CLASS

do this for all the the classes you use.

Compile..et voilà! i know it is not a very good solution, but it's the smartest i've found.

Upvotes: 0

Grzegorz W
Grzegorz W

Reputation: 3517

Shared types is a feature introduced in WCF but not in old asmx web services. For old ASMX try SchemaImporterExtension: link

Edit: OK. So I tested and this can be done by manually creating service contract, request and response classes and manually creating channel. You need to set [XmlSerializerFormat] on your contract and make sure your object has [SerializableAttribute]

Upvotes: 0

Bob The Janitor
Bob The Janitor

Reputation: 20802

Since you are using soap (.asmx creates a SOAP web service) the client should be able to pull the typing from the soap's wsdl page that your asmx is creating, you just need to make sure the object your passing is xml serializable.

As a General rule I will create a dto(data transfer object) to store the data passed to the client to make sure it's serializable and to decouple it from the rest of my application to prevent problems caused by updating the existing object.

This also works with non .NET clients as well, for example you can consume a SOAP call from .NET in java the same way

Upvotes: 0

Val Bakhtin
Val Bakhtin

Reputation: 1464

When you are adding service reference, tell VS to reuse objects from your business library. And as long as your service and client using same version of business library, visual studio will pick right types instead of service proxies.

Upvotes: 3

Steven Doggart
Steven Doggart

Reputation: 43743

There is no way to do this without either modifying the automatically generated web service reference code (which I would not advise), or wrapping your web reference calls with a wrapper class that takes and returns objects with their expected types.

When you add a web reference, the IDE automatically generates a class that matches the web service interface based on the definition returned via SOAP. Therefore, if you use a complex type like Product, it's going to automatically generate it's own Product class that has the same public properties.

Upvotes: 2

Related Questions