0

very stuck atm, have been in .net core world but back to .net3.5 and struggling.

I have a asmx webservice that I haven't worked on before, it uses jsonserialisation set in the web.config

<system.web.extensions>
<scripting>
  <webServices>
    <jsonSerialization maxJsonLength="2147483647" />
  </webServices>
</scripting>

My web methods are like so

[ScriptMethod(ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]
    [WebMethod]
    public SyncDownResponse SyncDown(string syncDownReq)
    {
        return Wrap<SyncDownSummaryRequest, SyncDownSummaryResponse>(_service.SyncDownSummary, syncDownSummaryRequestObject);
    }

What this does it returns a SyncDownResponse object that is serialised by json in the webservice setup. This all works fine.

Now what I want to do is to ignore null values when serialising the data down. I have tried this

public bool ShouldSerializeCompanyParameters()
    {
        return CompanyParameters.HasValue;
    }

And adding the json property attribute

 [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]

But the code is not being hit. If I call JsonConvert.SerializeObject it does call the should serialise method incidently.

Anyway, there must be a way to set the serialisation settings to ignore null values but I'm struggling. I have found articles to set GlobalConfiguration in the Global.asax file but am getting a reference missing for GlobalConfiguration. I have added all the System.Web references but nothing. I suspect that it may not be present in .net3.5. Any help would be great, I guess I could set something in the web.config or the Global.asax but stuck at the moment

Thanks

Punchmeister
  • 183
  • 2
  • 9

1 Answers1

1

So through trial and error, I got it working, so here it is if anyone else has same issue. There may be better solution but this works for me...

Change webmethod to return void

[WebMethod]
public void SyncDown(string syncDownReq)
{
    objectToSync = Wrap<SyncDownSummaryRequest, SyncDownSummaryResponse>(_service.SyncDownSummary, syncDownSummaryRequestObject);
    SerialiseObjectAndSync(objectToSync);
}

The SerialiseObjectAndSync calls json serialiser with the ignore null property, then writes it to the response. I had to call flush to get rid of {d: null} string that was appended to end of my json

public void SerialiseObjectAndSync<T>(T objectToSync) where T : class
    {
        // Need to serialise object and inform serialiser to ignore nulls
        var serialisedObject = JsonConvert.SerializeObject(objectToSync, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });

        // Now create the response
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "application/json";
        HttpContext.Current.Response.AddHeader("content-length", serialisedObject.Length.ToString());
        HttpContext.Current.Response.Flush(); // Flushing gets rid of the {d: null} that gets deserialised with object otherwise
        HttpContext.Current.Response.Write(serialisedObject);
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    }
Punchmeister
  • 183
  • 2
  • 9