John Isaiah Carmona
John Isaiah Carmona

Reputation: 5356

WCF Restful Service: The body encoding changes automagically?

I have a WCF Service like this:

[ServiceContract]
public class SomeService
{
    [WebInvoke(UriTemplate = "/test", Method = "POST")]
    public string Test()
    {
        using (var reader = OperationContext.Current.RequestContext.RequestMessage.GetReaderAtBodyContents())
        {
            var content = reader.ReadOuterXml().Replace("<Binary>", "").Replace("</Binary>", "");
            return content;
        }
    }
}

And has a config file like this:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Project.SomeService">
        <endpoint address="" binding="webHttpBinding" contract="Project.SomeService"
                  bindingConfiguration="webHttpBinding_SomeService" behaviorConfiguration="endpointBehavior_SomeService" />
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBinding_SomeService">
          <security mode="None"></security>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="endpointBehavior_SomeService">
          <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

But when I call it using fiddler with this url with POST method:

http://localhost:1111/SomeService.svc/Test

with the body:

asdasd

it returns YXNkYXNk instead, why was it like this?

My Code is in C#, framework 4, build in VS2010Pro.

Please help. Thanks in advance.

Upvotes: 0

Views: 1706

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499760

Something is base64-encoding either the result or the request. The ASCII bytes for asdasd come out as YXNkYXNk when base64-encoded.

It's not clear how you're providing the body, but I suggest you look at the exact request/response using WireShark or Fiddler to work out where the base64-encoding is happening, then work out why, then fix it.

EDIT: Now I've had a closer look at your code, it seems reasonably clear.

Your request is meant to include binary data, presumably - which is why you've got a Binary tag within the XML. You're deciding to ignore that, and just treat the XML representation of the binary data as text - but you shouldn't. Binary data is represented in XML via base64. So, you should:

  • Parse the XML as XML rather than getting the outer XML as a string and then performing string operations
  • Fetch the contents of the Binary tag as a string
  • Use Convert.FromBase64String to get the original binary data
  • If you believe that binary data was originally text, use Encoding.GetString to convert it back

Upvotes: 4

Related Questions