wcf rest - json serialization

Posts   
 
    
avilevi3
User
Posts: 5
Joined: 03-Dec-2017
# Posted on: 03-Dec-2017 07:28:58   

Hi,

we're using wcf rest to expose our restful web service and have an issue with json serialization. when returning llblgenpro entities, the response is serialized to xml, not json, when returning all other objects, they are properly serialized to json.

I've seen the instructions in the docs explaining how to change configurations for webapi's default formatter which is json.net's, but I couldn't find a solution for wcf rest.

any help would be appreciated Avi

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 04-Dec-2017 05:20:43   

Hi Avi. Please give us more information ( http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7717 ) :

  • What LLBLGen version and runtime library version are you using?
  • What .NET version? (any reason of using WCF over WebApi?
  • How does your datacontract look like?
  • Show us the method where you return the result.
David Elizondo | LLBLGen Support Team
avilevi3
User
Posts: 5
Joined: 03-Dec-2017
# Posted on: 04-Dec-2017 14:07:27   

Hi, thanks for your reply,

  • version: 5.2 (5.2.0) RTM
  • build date: 03-May-2017
  • .NET version: 4.5.2

we use wcf over web api since we need to expose both soap+rest endpoints for some of the services.

the data contract is an LLBLGEN entity

function:

Contract

    [OperationContract]
    [WebGet()]
    ShipmentEntity GetShipmentByID(int ship_id);

Implementor

   public ShipmentEntity GetShipmentByID(int ship_id)
    {
        return _bo.GetShipmentByID(ship_id);
    }

data access code:

var query = (from shipment in md.Shipment where shipment.RecId == ship_id select shipment).WithPath(a=>a.Prefetch<TCustomerEntity>(b=>b.ConsigneeEntity)); return query.FirstOrDefault();

thanks a lot Avi

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 05-Dec-2017 05:11:58   

Try setting defaultOutgoingResponseFormat="Json" for the webhttp behavior configuration.

avilevi3
User
Posts: 5
Joined: 03-Dec-2017
# Posted on: 05-Dec-2017 07:27:53   

Hi Walaa,

like I've mentioned in the original post - when returning all other objects, they are properly serialized to json.

the configuration on WCF's level is correct and everything except LLBLGEN entities is serialized correctly to json.

the documentation describes this issue and explains how to solve it on webapi: https://www.llblgen.com/documentation/5.2/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/Adapter/Distributed%20systems/gencode_webservices.htm

I'm asking for a similar solution for wcf

thanks Avi

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 05-Dec-2017 11:06:45   
Frans Bouma | Lead developer LLBLGen Pro
avilevi3
User
Posts: 5
Joined: 03-Dec-2017
# Posted on: 05-Dec-2017 12:31:34   

Hi,

actually it doesn't as it describes a different problem :-)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 05-Dec-2017 13:05:10   

Ok, then I don't know, sorry. The problem is that we don't do the serialization to json, it is performed by wcf. I'm a bit surprised that the setting to make WCF serialize to json is ignored and it serializes to xml regardless...

For clarity: our framework can serialize to binary and xml, and implements the interfaces for that. So if the calling framework (e.g. wcf) wants to serialize the data, and it serializes the entities using an xmlserializer, then that's what's done. I don't know why it serializes things to xml while you've specified it should serialize to json, but it's not something we can do much about, as we don't serialize the data to json, wcf does. serializing to json is therefore using either binary serialization -> json, or xml -> json.

Frans Bouma | Lead developer LLBLGen Pro
avilevi3
User
Posts: 5
Joined: 03-Dec-2017
# Posted on: 06-Dec-2017 07:39:45   

I see,

we get the following error when trying to return an entity with the dataContract attributes generated, it means that the wcf serializer is looking for the IXmlSerializable.

"System.Runtime.Serialization.InvalidDataContractException: Type 'XXXEntity' cannot be IXmlSerializable and have DataContractAttribute attribute."

is there a way to generate the entities with dataContract related attributes but without the IXmlSerializable implementation?

thanks Avi

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 06-Dec-2017 11:36:28   

According to this https://social.msdn.microsoft.com/Forums/vstudio/en-US/9a432ca9-d292-4ec0-b01b-66b1d632eaba/using-webinvokejson-to-serialize-a-type-that-implements-ixmlserializable-results-in-a-big-xml?forum=wcf (and other posts online), it seems that if an object implements IXmlSerializable, the json serializer leaves it as-is. I've tried it on our WCF example https://github.com/SolutionsDesign/LLBLGenProExamples_5.x/tree/master/Example_WCF_DataScope and it indeed serializes it happily to xml and then wraps it in a json string. disappointed

Decorating them with DataContract attributes won't help, as that's just to tell the xml serializer to use these properties/types, which is of no use, as the object already implements IXmlSerializable. It's unfortunately not possible to get a runtime without IXmlSerializable as it's sadly a thing the object has to implement hard-coded to work (i.e. inherit from the interface) so you can't plug/play the interfaces.

So exposing entities directly on a service and let WCF do the json serialization automatically isn't going to work.

You can solve this in a couple of ways: - do the serialization manually using a json serializer and return the json string as-is, using the binary serialization implementation of the entities. - create a couple of derived models in the designer on top of your entity model and expose those through your service. This is the preferred solution anyway as it allows you to tailor the data towards what's needed, not how it's logically modeled in an entity model. The derived models are poco classes and serialize to json automatically.

Frans Bouma | Lead developer LLBLGen Pro