我有这样的WCF服务:
[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;
}
}
}
并有一个这样的配置文件:
<?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>
但是当我用fiddler用这个url用POST
方法调用它时:
http://localhost:1111/SomeService.svc/Test
身体:
asdasd
它会返回YXNkYXNk
,为什么会这样?
我的代码在C#,框架4中,在VS2010Pro中构建。
请帮忙。提前谢谢。
答案 0 :(得分:4)
Something 是base64编码的结果或请求。当base64编码时,asdasd
的ASCII字节为YXNkYXNk
。
目前尚不清楚您是如何提供正文的,但我建议您使用WireShark或Fiddler查看确切的请求/响应,以找出base64编码发生的位置,然后再进行工作为什么,然后解决它。
编辑:现在我已经仔细研究了你的代码,看起来相当清楚。您的请求可能包含二进制数据,大概是 - 这就是您在XML中有Binary
标记的原因。您决定忽略它,只是将二进制数据的XML表示视为文本 - 但您不应该这样做。二进制数据通过base64以XML表示。所以,你应该:
Binary
标记的内容作为字符串Convert.FromBase64String
获取原始二进制数据Encoding.GetString
将其转换回来