Unable to Pass CSLA object to WCF service

Unable to Pass CSLA object to WCF service

Old forum URL: forums.lhotka.net/forums/t/4199.aspx


Vinodonly posted on Saturday, January 19, 2008

I have created a wcf service in which I need to pass my CSLA BO (BusinessListBase) to my service.

This is how I'm defining the DataContract and I have also defined my BO as knowntype

 

[KnownType(typeof(FabAccMainItems))]

[DataContract(Name="RepObject")]

public class RepObject

{

[DataMember(Name = "RepName", IsRequired = true, Order = 0)]

string _RepName = "";

[DataMember(Name = "RepParams", IsRequired = true, Order = 1)]

SortedList<string, object> _RepParams = new SortedList<string,object>();

}

Prob is that as soon as the parameter is passed by adding my BO in SortedList _RepParams from calling code and calling the required service, following error is thrown.

 

"Insert is an invalid Operation" and following is the stack trace (truncated), I'm surprised why it is going in ReadOnlyBindingList...

   at Csla.Core.ReadOnlyBindingList`1.InsertItem(Int32 index, C item) in C:\Dev\Projects\cslacs\cslacs\Csla\Core\ReadOnlyBindingList.cs:line 87
   at System.Collections.ObjectModel.Collection`1.Add(T item)
   at ReadArrayOfBrokenRuleFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString , XmlDictionaryString , CollectionDataContract )

RockfordLhotka replied on Sunday, January 20, 2008

It isn't clear whether you are trying to serialize an actual CSLA business object, or whether you are doing the right thing and defining a DTO?

You can't directly pass CSLA objects through WCF, just like you can't pass them directly through an asmx Web service.

CSLA uses serialization to do many things, including n-level undo support and data portal support. It requires that serialization create a complete clone of the original object graph.

Unfortunately (and I argued against this), WCF only allows one data contract per object. I'd have preferred at least two (one for external contract use, and one for internal use).

What you need to do is described in the Using CSLA .NET 3.0 ebook, and is along the same lines as Chapter 11 of Expert 2005 Business Objects.

  1. Create a formal contract for your service in the form of either an XSD contract (from which you code-gen a DTO) or a DTO (from which VS will code-gen the XSD). I tend to create the DTO, as that's easiest.
  2. Write code in your service implementation to copy data between the DTO and your business object.

A WCF service is just another type of interface to your application. It is just like a web interface, except that it exposes/consumes XML instead of HTML. But all the same concepts apply - you don't send your real business objects directly to the consumer - you copy their data into the UI format (HTML, XML, XAML, whatever) and send that to the consumer.

Vinodonly replied on Sunday, January 20, 2008

Appreciate your detailed response on this and I understood the solution, it is a lot of work to accomplish what I want to do.

I will explain little bit more on what exactly I'm doing. WCF service is a special Excel Reporting Engine which I have created (by natively generating excel files not using the COM) and is running on server. Report parameters are passed, if for a particular report the data is already avl in memory then I simply want to pass that BO and generate report from it.

Currently I'm forcing the users to save data first and then they can generate report, if the BO can be passed directly then this gives the added advantage of previewing some special reports without saving it in Database.

But seeing the amount of work involved I guess will stick to the current mechanism only.

Copyright (c) Marimer LLC