作为WCF新手,我试图找出从WCF服务返回JSON对象的正确配置。WCF响应中的JSON格式
结果我得到的是(在萤火虫看):
{"TestServiceResult": "{\"AccountID\":999999,\"CardNumber\":555555,\"AccountBalance\":999.99,\"GivenName\":\"Ben\",\"FamilyName\":\"Rosniak\"}"}
我感兴趣的部分是一个长字符串,而不是JSON对象我之后。
<!-- Added for Mobile Pay Service-->
<behaviors>
<serviceBehaviors>
<behavior name="MobilePayServiceBehaviour" >
<serviceDebug includeExceptionDetailInFaults="true"/>
<!--<serviceCredentials >
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MobilePayService.WtfUserNamePasswordValidator, MobilePayService" />
</serviceCredentials>
<serviceAuthorization principalPermissionMode="Custom">
<authorizationPolicies>
<add policyType="MobilePayService.WtfAuthorizationPolicy, MobilePayService" />
</authorizationPolicies>
</serviceAuthorization>-->
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebHttpBehaviour">
<webHttp automaticFormatSelectionEnabled="false" defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Json" helpEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
,我使用,以确保响应被格式化的测试方法:
[WebGet(UriTemplate = "TestService/{id}/{device}/{culture}")]
public string TestService(string id, string device, string culture)
{
WCFProfileModel profileModel = new WCFProfileModel()
{
AccountID = 999999,
AccountBalance = 999.99F,
CardNumber = 555555,
GivenName = "Ben",
FamilyName = "Rosniak"
};
return profileModel;
}
关于服务的唯一配置(该项目是由别人开始)不知何故,响应被包装在某种模板中,我想知道这是怎么发生的,但我不确定从哪里开始寻找这个。我想除掉那些"TestServiceResult"
部分,只回:
{"AccountID":999999,"CardNumber":555555,"AccountBalance":999.99,"GivenName":"Ben","FamilyName":"Rosniak"}
UPDATE: 我试图教程here(更新我的代码,以反映这一点)以下的,但我得到一个错误“的说法profileModel不能隐式转换为字符串“。
M $应该默认它为Bare,但事实并非如此。如果没有设置,JSON响应会停止工作。 – Devela 2012-10-12 07:13:46