Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to return interfaces from .net webservices or can one only return concrete classes?

I need to return a complex type from a webservice and had constructed the class MyComplexType which implements the interface IMyComplexType. Now in my webservice I have a method that should return an object that implements IMyComplexType like so:

[WebMethod]
public IMyComplexType GetMyComplexType()
{
   IMyComplexType myCt = new MyComplexType();
   ...
   return myCt; 
}

Everything compiles ok but when I run the webservice it throws an exception as follows:

System.NotSupportedException: Cannot serialize interface MyWebService.IMyComplexType.

Is this by design or am I missing something. Can I decorate my class or interface somehow to allow this? Do I have to eliminate all interfaces and only work with concrete and perhaps abstract base-classes to be able to do this with .net webservices?

Edit:

this is the interface I am trying to serialize:

public interface ICustomer
{
     long Id {get; set;}
     string Name {get; set;}
     string EmailAddress {get; set;}
}
like image 795
Fadeproof Avatar asked Jan 28 '09 14:01

Fadeproof


People also ask

Can an interface inherit from a concrete class?

A concrete class can implement multiple interfaces, but can only inherit from one parent class.

Can interface methods have return type in C#?

Answers. By definition of what an interface is it is impossible to return an interface because interfaces cannot be allocated; there cannot be anything to return.

Do I need an interface for every class?

If you want to treat different, specific classes in some way that is the same for every one of them, you should introduce an interface that covers their common ground. Such an interface is often called an "abstraction", because it abstracts away the details that don't matter to the client of that interface.

CAN interface have return type in Java?

Note: You also can use interface names as return types. In this case, the object returned must implement the specified interface.


1 Answers

Short answer is no, you can't return an interface through a WebService.

Long answer is that you can kind of get the behaviour you want, but it's a bit hacky. The solution is to use binary serialization. So you serialize your type to bytes, have the web method return a series of bytes and then on the other side you would deserialize the bytes back to your complex type.

For example

[WebMethod]
public byte[] GetMyComplexType()
{

   IMyComplexType myCt = new MyComplexType();
   ...

   MemoryStream stream = new MemoryStream();
   IFormatter fmt = new BinaryFormatter();
   fmt.Serialize(stream, myCt);
   stream.Flush();
   byte[] bytes = stream.ToArray();
   stream.Close();

   return bytes;
}

You'll need to convert everything back at the other end.

byte[] bytes = service.GetMyComplexType();
MemoryStream stream = new MemoryStream(bytes);
BinaryFormatter fmt = new BinaryFomatter();
MyComplexType myCt = fmt.DeSerialize(stream);

You can probably clean the deserialization up by creating a generic method. I should add, that you'll need to make sure you can reference the complex type on the client.

Alternatively, if they are your interfaces then you could turn them into Abstract Classes. However, this won't work if you want the class to implement multiple interfaces (or you already inherit from another class) as you can only inherit one class. If you need some polymorphic behaviour on the client, you'll need to use the XmlInclude attribute.

like image 101
Martin Clarke Avatar answered Sep 28 '22 06:09

Martin Clarke