我争夺这一小时,直到最后我用这个例子,它的工作先去:http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide
我知道唯一的链接的答案是不好的,别人都用这个CP链接solve this type of problem here at Stackoverflow所以这里如果文章不断下降的基本步骤:
STEP 1
首先推出的Visual Studio 2010中点击文件 - >新建>项目。创建新的“WCF服务应用程序”。
STEP 2
一旦创建了项目,你可以看到,默认情况下WCF服务和接口文件已创建解决方案(Service1.cs & IService.cs)。删除这两个文件,我们将创建我们自己的接口和WCF服务文件。
第3步
现在右键单击解决方案并创建一个新的WCF服务文件。我已将服务文件的名称命名为“RestServiceImpl.svc”。
STEP 4
正如我在,我们会写,在XML和JSON格式返回数据的API的文章开始解释,这里是该接口。在IRestServiceImpl中,添加以下代码
在上面的代码中,可以看到两种不同的IRestService方法,它们是XMLData和JSONData。 XMLData以JSON的形式返回结果,而JSONData。
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "xml/{id}")]
string XMLData(string id);
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/{id}")]
string JSONData(string id);
}
STEP 5
打开文件RestServiceImpl.svc.cs和写入以下代码那边:
public class RestServiceImpl : IRestServiceImpl
{
public string XMLData(string id)
{
return "You requested product " + id;
}
public string JSONData(string id)
{
return "You requested product " + id;
}
}
步骤6
的Web.Config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<!-- 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="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
STEP 7
在IIS:
这是在远程服务器上orlocalhost?如果偏远,你是否先验证了本地所有工作? – 2009-01-17 17:59:07