我对WCF服务有点新意。在我目前,我已经开发了一个hello world Restful WCF服务。以下是我的RESTful Web服务的代码。
RESTful服务合同如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace RestfulWCFService
{
[ServiceContract]
public interface IRestfulTestService
{
[OperationContract]
[WebInvoke(Method="GET", ResponseFormat= WebMessageFormat.Xml, UriTemplate="xml/{name}")]
string SayHelloXml(string name);
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "json/{name}")]
string SayHelloJson(string name);
}
}
接口实现如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace RestfulWCFService
{
public class RestfulTestService : IRestfulTestService
{
string IRestfulTestService.SayHelloXml(string name)
{
return "Hello " + name;
}
string IRestfulTestService.SayHelloJson(string name)
{
return "Hello " + name;
}
}
}
我已在IIS上部署此服务,现在我使用以下URL访问此Web服务。
http://localhost/RestfulWCFService/RestfulTestService.svc/xml/pankesh
webservice正在返回以下数据。
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hello pankesh</string>
现在,我的问题是 - 在上面的webservice上,我传递的是一个简单的数据类型,即STRING。现在,我的要求是我想通过REST请求传递复杂的自定义对象。你可以告诉我 - 如何在上述场景中通过REST请求传递自定义对象?
web.config
文件的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<services>
<service name="RestfulWCFService.RestfulTestService">
<endpoint behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="" contract="RestfulWCFService.IRestfulTestService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>