Wcf Json

Posts   
 
    
ch
User
Posts: 91
Joined: 25-Apr-2015
# Posted on: 25-Jun-2017 23:59:54   

I have a question regarding wcf and json.

I have a wcf service that uses defaultOutgoingResponseFormat="Json" as a setting resulting in a clean Json based on generated DTO objects.

My question is: Is there a way I can dynamically remove the DataMember attribute so I can control to generated Json output. I want to remove the fields if they were not selected based on the ExcludeIncludeFieldList.

The version I'm using is 2.6.

Any suggestions are welcome.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Jun-2017 07:18:56   

Hi ch,

What do you mean when you say "...generated based on DTO objects"? Are these your own DTOs or LLBLGen entities?

If possible, please show us the relevant code involved in this scenario.

David Elizondo | LLBLGen Support Team
ch
User
Posts: 91
Joined: 25-Apr-2015
# Posted on: 26-Jun-2017 08:52:00   

The DTO objects are classes generated based on a template. So they are used instead of the regular generated entities.

But the question would remain the same for both types of objects. (DTO and LLbl Entities)

Once you use them in WCF, the datacontractserializer will process all the properties that have a datamember attribute. What I want is to dynamically decide the ones I want in the resulting Json.

I know how to intercept it in the wcf pipeline but it seems a lot of code to just dynamically decide to use the DataMember attribute.

I need to decide myself if I want to serialize the property or not.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 26-Jun-2017 09:16:02   

No idea how to control that, seems to me an issue related to the serializer used, not something we can help with as it's outside the scope of our framework. The attributes are static meta-data so you likely need to augment the serializer (subclassing or configuration, no idea though how to do that in this case) to use other sets of metadata than reflection and the attributes.

Frans Bouma | Lead developer LLBLGen Pro
ch
User
Posts: 91
Joined: 25-Apr-2015
# Posted on: 26-Jun-2017 09:36:45   

I agree, I was only hoping you guys already had some experience in thissimple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 26-Jun-2017 10:44:18   

ch wrote:

I agree, I was only hoping you guys already had some experience in thissimple_smile

No sorry simple_smile

Frans Bouma | Lead developer LLBLGen Pro
ch
User
Posts: 91
Joined: 25-Apr-2015
# Posted on: 01-Jul-2017 22:06:18   

I finally found the solution. My returned Json from the Wcf service now only returns the fields that are set in the original fetched ExcludeIncludeFieldList. This way the returned Json is massively smallersmile

The core of the solution is to:

  1. write your own IDispatchMessageFormatter and a custom DefaultContractResolver
  2. add the ExcludeIncludeFieldsList as a property to the dto object because you need to access it from the contractresolver
  3. update the web config to use your new IDispatchMessageFormatter

I have included the necessary code should some1 have the same problem I had.


<extensions>
      <behaviorExtensions>
        <add name="newtonsoftJsonBehavior" type="yournamespace.NewtonsoftJsonBehaviorExtension, yourtype" />
      </behaviorExtensions>
    </extensions>

<behavior name="ormsEndpointBehavior">
          <webHttp defaultOutgoingResponseFormat="Json" helpEnabled="true" defaultBodyStyle="Bare" faultExceptionEnabled="true"/>
          <newtonsoftJsonBehavior/>
        </behavior>

Attachments
Filename File size Added on Approval
NewtonsoftContractResolver.cs 2,508 01-Jul-2017 22:06.47 Approved
NewtonsoftJsonBehavior.cs 4,021 01-Jul-2017 22:06.51 Approved
NewtonsoftJsonBehaviorExtension.cs 530 01-Jul-2017 22:06.54 Approved
NewtonsoftJsonContentTypeMapper.cs 456 01-Jul-2017 22:06.57 Approved
NewtonsoftJsonConvertor.cs 1,237 01-Jul-2017 22:06.59 Approved
NewtonsoftJsonDispatchFormatter.cs 5,942 01-Jul-2017 22:07.01 Approved
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 02-Jul-2017 09:43:54   

Thanks for sharing and I'm glad you found a solution! simple_smile The thread is in the helpdesk forum btw, so no-one but us and you can see it. Do you want us to move the thread to a general forum so more people can see it?

Frans Bouma | Lead developer LLBLGen Pro
ch
User
Posts: 91
Joined: 25-Apr-2015
# Posted on: 03-Jul-2017 18:14:42   

Yes off course. Maybe someone will find it useful