我正在开发一个 WCF resful 服务,它将基本上从一些移动应用程序中使用。通过 POST 我试图发送一个 DataContract 对象 [实际上我必须发送一个对象列表] 和另一个单个 id 作为字符串。我的问题是是否可以定义我的函数来接受 DataContract 对象和单个字符串?
以下是我的代码:接口声明:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetDataUsingDataContract/{id}")]
CompositeType GetDataUsingDataContract(string id, CompositeType composite );
}
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
函数的实际定义:
public CompositeType GetDataUsingDataContract(string id, CompositeType composite )
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite .BoolValue)
{
composite .StringValue += "- Suffix and the id is"+id;
}
return report;
}
我试图从 Fiddler 发送的 json 对象是
{"BoolValue":true,"StringValue":"sdfsdfsf"}
以上是我正在测试服务的提琴手的快照。经过几次谷歌搜索后,我得到了以下链接,其中客户端实际使用 Web 服务引用来获取 DataContract 类型并在作为请求正文发送之前序列化为 json。但是为什么我的 Fiddler 测试没有成功?! http://dotnetmentors.com/wcf/wcf-rest-service-to-get-or-post-json-data-and-retrieve-json-data-with-datacontract.aspx
有人可以提出任何建议吗?
web.config 如下:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="JSONWebService.Service1" behaviorConfiguration="JSONWebService.Service1Behavior">
<endpoint address="../Service1.svc"
binding="webHttpBinding"
contract="JSONWebService.IService1"
behaviorConfiguration="webBehaviour" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="JSONWebService.Service1Behavior">
<!-- 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>
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>