Sunday, February 15, 2009

Reuse types in a Web Service

For some time I have trying, with no success, to get the Reuse types in referenced assemblies option to work after adding a web service reference to a project.

Finally I have found the answer: That option is not available for so called ‘legacy’ ASP.NET web services, only for WCF web services. So I just re-created my web service as a WCF service. My web service is a pretty simple one providing access to a .NET Remoting server hosted in a windows service. As such the code is pretty simple, and it was a matter of less than an hour to get it all going.

Just one small gotcha that I guess should be pretty obvious. The assembly with the types that you want to re-use must already be referenced in the project that you are adding the service reference to. I.e. it does not go and look for them. I don’t suppose it would have any way of doing that.

The only modification that I had to do was to the web service. It seems to work. I am a little surprised that I did not have to add the DataContractAttribute to the classes to use them… They are already marked Serializable and the members are XmlElements… perhaps that is why it works. I have not really tested it yet, just compiled the service and the client. I am thinking now that perhaps the data is not being passed at all. Will see tomorrow – when I am supposed to be working (rather than now, the weekend).


[Edit] I does work just fine. I wonder then what is the difference between using DataContract and just making them Serializable. I also wonder what is the benefit of using the ClientFactory rather than just using the Client object directly.


[Edit] There are, it seems, times when you do need to add the DataContractAttribute to your data classes. I just found that one class that I was using as a parameter caused an exception upon serialization:

There was an error while trying to serialize parameter http://tempuri.org/:Commands. The InnerException message was 'Type 'BSPG.Prism.Script.Table' with data contract name 'Table:http://schemas.datacontract.org/2004/07/BSPG.Prism.Script' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.

This class contains members whose type is also not a standard class. The class that was working only had members of standard types. Fortunately, it seems to work if you just decorate the top level class with the DataContractAttribute – no need to go and change each other class in there (this would have been quite a pain for me, as they are all generated classes). That said, it might turn out that it only works because after the first level they are all standard types… That is not necessarily true for all possible data though – this is a class hierarchy created by XSDObjectGen from a quite deeply nested DOM… We will see.

No comments:

Post a Comment