0

I have an old project that is serializing TimeSpans like "PT21H", this is happening because DCJS is the default serializer for the project, I assume. I tried to implement the instructions here. But, when I hit the endpoint it is still returning the object serialized using DCJS with the Timespan looking like "PT21H"I don't want to overload this with all of my code so I will include what I believe is useful but feel free to ask for more information.

Relevant section of Web.Config

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <location path="." inheritInChildApplications="false">
    
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
    
  <system.web>
      <compilation debug="true" targetFramework="4.8" />
      <httpRuntime targetFramework="4.5.1" />
  </system.web>
    
    
  <system.webServer>
      <asp scriptErrorSentToBrowser="true" />
      <modules runAllManagedModulesForAllRequests="true">
          <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      </modules>
  </system.webServer>

    <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

        <extensions>
            <behaviorExtensions>
                <add name="newtonsoftJsonBehavior" type="Test.WcfNewtonsoftJsonSerializer.NewtonsoftJsonBehaviorExtension, Test, Culture=neutral" />
            </behaviorExtensions>
        </extensions>

        <behaviors>

            <endpointBehaviors>
                <behavior name="restEndPointBehavior">
                    <webHttp helpEnabled="false" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" faultExceptionEnabled="false" />
                    <newtonsoftJsonBehavior />
                </behavior>
            </endpointBehaviors>

            <serviceBehaviors>

                <behavior name="restServiceBehavior">
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                    <serviceDebug httpHelpPageEnabled="false" includeExceptionDetailInFaults="true" />
                </behavior>
            </serviceBehaviors>

        </behaviors>


        <services>
            <service name="Test.WebserviceAscent.Webservice" behaviorConfiguration="restServiceBehavior">
                <endpoint address="" behaviorConfiguration="restEndPointBehavior" binding="webHttpBinding" bindingConfiguration="restWebHttpBinding" contract="Test.Web.IWebservice" />
            </service>
        </services>


        <bindings>
            <webHttpBinding>
                <binding name="restWebHttpBinding" contentTypeMapper="Test.WcfNewtonsoftJsonSerializer.NewtonsoftJsonContentTypeMapper, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
                </binding>
            </webHttpBinding>
        </bindings>

    </system.serviceModel>
  </location>
  <runtime>
...
  </runtime>
</configuration>

I have included the same files as the example, I even kept the same namespace for them:


What am I doing wrong?
I looked at a similar post here, but I couldn't get it to work. Are these solutions simply outdated and there is a better way to do this?

Edit: Here is my testcode that is having the same problem: https://github.com/JasonRAndersen00/SerializerTest

  • I checked your code, is the content of contentTypeMapper in your binding correct? I suggest you double check the steps, you can check out [this similar post](https://stackoverflow.com/questions/8082231/custom-behavior-wont-register-in-my-web-config). – Lan Huang Apr 06 '22 at 06:42
  • This is the content of contentTypeMapper: using System.ServiceModel.Channels; namespace WcfNewtonsoftJsonSerializer { public class NewtonsoftJsonContentTypeMapper : WebContentTypeMapper { public override WebContentFormat GetMessageFormatForContentType(string contentType) { return WebContentFormat.Raw; } } } So my web.config looks right? I will check out the post. Thanks! – Jason Andersen Apr 06 '22 at 16:59
  • Compare the code here: `contentTypeMapper="WcfNewtonsoftJsonSerializer.NewtonsoftJsonContentTypeMapper, (Project.dll), Culture=neutral, PublicKeyToken=null"`. – Lan Huang Apr 07 '22 at 09:08
  • @lanHuang, I updated the code and I have a repo with a test project here: https://github.com/JasonRAndersen00/SerializerTest I am looking at the code you mentioned and I updated it to the following: `` But it still isn't working. Thank you so much for your responses! – Jason Andersen Apr 07 '22 at 14:13
  • If I understand this line of code, the pseudo code should look like this: `contentTypeMapper="Namespace.class, app.dll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" ` Is this right? – Jason Andersen Apr 07 '22 at 14:19
  • @LanHuang, I think I found my problem. But I just want to make sure I understand what you are trying to help me see. Is there something wrong with this line of code you pointed out? – Jason Andersen Apr 07 '22 at 19:59

1 Answers1

0

I found the problem with my web.config. It is such a newb mistake I am honestly tempted to not say anything. I was putting the wrong namespaces on the service.

<service name="Test.WebserviceAscent.Webservice" 

Should be

<service name="Test.Web.Webservice" 

Because of this it just wasn't assigning the serializer to my endpoints.