【发布时间】:2026-02-23 01:15:01
【问题描述】:
我对 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
网络服务正在向我返回以下数据。
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hello pankesh</string>
现在,我的问题是 - 在上面的 web 服务中,我传递了一个简单的数据类型,即 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>
【问题讨论】:
-
您找到解决方案了吗? ..我看起来也一样...
标签: c# rest wcf visual-studio-2013