0

I have two behaviors configured in my endpoint:

  1. One is for json serialization which is basically very similar to the exmaple here. What's important there is the following:
public class NewtonsoftJsonBehaviorExtension : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        get { return typeof(NewtonsoftJsonBehavior); }
    }

    protected override object CreateBehavior()
    {
        return new NewtonsoftJsonBehavior();
    }
}

public class NewtonsoftJsonContentTypeMapper : WebContentTypeMapper
{
    public override WebContentFormat GetMessageFormatForContentType(string contentType)
    {
        return WebContentFormat.Raw;
    }
}
  1. The other is for error handling. So that when exception is thrown a json formatted message will be sent to the client. The code is taken from here (The answer staring with: "Here's a complete solution based on some info from above:").

when I use only behavior 1 everything works fine. When I add the second behavior I get the following exception:

{"ExceptionType":"System.InvalidOperationException","Message":"The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details."}

Here is how my web.config looks like:

<services>
      <service name="Algotec.Services.Archive.Data.ArchiveDataService" behaviorConfiguration="defaultBehavior">
        <endpoint name="soap" address="soap" binding="basicHttpBinding" contract="Algotec.Interfaces.Archive.Data.IArchiveData" bindingNamespace="http://algotec.co.il/ArchiveData"/>
        <endpoint name="restXml" address="" binding="webHttpBinding" contract="Algotec.Interfaces.Archive.Data.IArchiveData" behaviorConfiguration="restBehavior" bindingNamespace="http://algotec.co.il/ArchiveData"/>
        <endpoint name="restJson" address="json" binding="webHttpBinding" contract="Algotec.Interfaces.Archive.Data.IArchiveData" behaviorConfiguration="jsonBehavior" bindingConfiguration="jsonBinding" bindingNamespace="http://algotec.co.il/ArchiveData"/>
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
      </service>
    </services>

...

    <endpointBehaviors>
            <behavior name="restBehavior">
              <enhancedWebHttp defaultOutgoingRequestFormat="Xml" defaultOutgoingResponseFormat="Xml"/>
            </behavior>
            <behavior name="jsonBehavior">
              <enhancedWebHttp defaultOutgoingRequestFormat="Json" defaultOutgoingResponseFormat="Json" helpEnabled="true"/>
              <newtonsoftJsonBehavior/>
              <jsonErrorBehavior/>
            </behavior>
          </endpointBehaviors>

    ...

        <extensions>
              <behaviorExtensions>
                <add name="newtonsoftJsonBehavior" type="Algotec.Services.Infra.BehaviorExtensions.NewtonsoftJsonBehaviorExtension, Algotec.Services.Infra, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
                <add name="jsonErrorBehavior" type="Algotec.Services.Infra.Behaviors.JsonErrorWebHttpBehaviorElement, Algotec.Services.Infra, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
              </behaviorExtensions>
            </extensions>

Any ideas?

Community
  • 1
  • 1
julius_am
  • 1,422
  • 2
  • 23
  • 42

2 Answers2

0

Why are you returning WebContentFormat.Raw from NewtonsoftJsonContentTypeMapper? Shouldn't you be returning WebContentFormat.Json so that the format matches correctly?

Can you clarify a bit what you're trying to accomplish?

tomasr
  • 13,683
  • 3
  • 38
  • 30
0

Here is what solved my problem:

In the web.config I simply switched the order between <newtonsoftJsonBehavior/> and <jsonErrorBehavior/>. I admit that I don't understand completely all this behaviors and don't know why it helped but it did.

julius_am
  • 1,422
  • 2
  • 23
  • 42