PDA

View Full Version : WebServiceExporter Interfaces



LoreX75
12-02-2010, 07:01 AM
Hi.
I've just tried to export a class with the WebServiceExporter but, as per standard asmx services, I got an error as the returned type is an interface; is there any work around for this?
I put below an example:


public interface IWsTest
{
string SayHello();
ICiao SayCiao();
}

public class WsTest : IWsTest
{
public string SayHello()
{
return "hello world";
}

public ICiao SayCiao()
{
return new Ciao("Ciao");
}

}

public class Ciao : ICiao
{
private string _saluto;

public Ciao(string saluto)
{
Saluto = saluto;
}

public string Saluto
{
get { return _saluto; }
set { _saluto = value; }
}
}

public interface ICiao
{
string Saluto { set; get; }
}


and the spring config:


<object id="autoProva"
type="MyNamespace.WsTest, MyAssembly">
</object>
<object id="autoProvaExporter"
name="/autosend/WsTest.asmx"
type="Spring.Web.Services.WebServiceExporter, Spring.Web">
<property name="TargetName" value="autoProva"/>
<property name="Name" value="autoProva"/>
<property name="Namespace" value="http://MySite/WebServices"/>
<property name="Description" value="Test"/>
</object>



I get the followind exception:


[NotSupportedException: It's not possible to serialize the interfaceICiao.]
[...]


It's quite unfair to create a webservice interface which is coupled with real classes instead of interfaces, isn't it?

Thanks for any suggestion,
Lorenzo

madkinder
12-02-2010, 06:38 PM
It's quite unfair to create a webservice interface which is coupled with real classes instead of interfaces, isn't it?


Why would you like to return interfaces instead of real objects?

Just think about this: on the server side you return an instance of a real object Ciao, the object gets serialized and transferred somehow to the client. Now how is client side code (doesn't matter whether it is a proxy built by Spring automatically or any other means) supposed to deserialize that data? It just doesn't know the internal structure of the class.

LoreX75
12-03-2010, 12:46 PM
Hi madkinder.

I understand your point of view, but on the other hand I'm still not convinced.

Instead of creating an interface like the following:


IWebServiceInterface
{
IResponse = callMethod(IParam1 param1, IParam2 param2);
}
interface IParam1
{
IInnerProp1{set;get}
IInnerProp2{set;get;}
ICallResult Method1(...):
}

.....

WebServiceClass:IWebServiceInterface {...}


In order to export such an interface with the WebServiceExporter I should do:


IWebServiceInterface
{
Response = callMethod(Param1 param1, Param2 param2);
}
class Param1
{
InnerProp1{set;get}
InnerProp2{set;get;}
ICallResult Method1(...):
}


In my code I only use reference to Interfaces rather then classes, so with the WebServiceExporter I should create an interface which is useful only for that purpose, as it's coupled directly to classes instead of other interfaces.

I was thinking about a configuration like this:


<object id="objectToBeExported" type="WebServiceClass">
<intefacesMapping>
<interface name="IParam1" concreteClassToExpose="Param1"/>
</intefacesMapping>
</object>



In this latter sample, you actually tell Spring which class to expose to clients but interfaces are only coupled to other interfaces and not real calsses.

Hope I was clear.

Lorenzo