Search code examples
c#asp.netasmxjavascriptserializer

Get the application's JavascriptSerializer in a old ASP.NET ASMX web service


I have an old ASP.NET ASMX webservice

public class MyWS : System.Web.Services.WebService

I was able to use some features of the JavaScriptSerializer like ScriptIgnore, but if I want to register a custom converter, where would I grab the application's serializer object so I could register it?

I looked into GlobalConfiguration but it's not something available (yet) in this old version.

My functions are decorated with:

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

And return objects that are serialized to Json just fine, but I want more control.


Solution

  • There really isn't a need to "grab" the application's serializer to register new converters. They simply have to be defined in web.config:

    <system.web.extensions>
        <scripting>
            <webServices>
                <jsonSerialization maxJsonLength="2147483644">
                    <converters>
                        <add name="MyJavaScriptConverter"
                             type="Company.Webservice.Utilities.MyJavaScriptConverter, Company.Webservice, Version=1.0.0.0, Culture=neutral"/>
                    </converters>
                </jsonSerialization>
            </webServices>
        </scripting>
    </system.web.extensions>
    

    The MyJavaScriptConverter class above only has to inherit the JavaScriptConverter abstract class.