今天研究了一下在之前在帖子Android访问WCf得到的奇怪JSON中提到的问题, 发现跟其于操作契约中设置的BodyStyle有关, 详细参阅[WCF REST] Web消息主体风格(Message Body Style).
在这里做一下记录:
endpointBehaviors
<behavior name="webScript">
<enableWebScript />
</behavior>
<behavior name ="webHTTP">
<webHttp/>
</behavior>
endpoint
<!--原来是webScript-->
<endpoint address="" binding="webHttpBinding" contract="Host.IAndroidServices" behaviorConfiguration="webHTTP">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
操作契约
[OperationContract]
[WebInvoke(
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
RetUserInform logIn(string userID, string PWD);
操作契约实现
#region IAndroidServices Members logIn
public RetUserInform logIn(string userID, string PWD)
{
Console.WriteLine("logIn has bean satared");
RetUserInform n = new RetUserInform();
n.Userid = 123456;
n.Functions = "asdfads";
n.UserAreaCode = "012";
n.FinalDate = "afd";
return n;
}
#endregion
之前得到格式为
{"d":{"__type":"RetUserInform:#Host","FinalDate":"afd","Functions":"asdfads","UserAreaCode":"012","Userid":123456}}
的JSON串是由于对Response的包装导致的, 作出如上修改后得到
{"FinalDate":"afd","Functions":"asdfads","UserAreaCode":"012","Userid":123456}
一切正常.