我爭奪這一小時,直到最後我用這個例子,它的工作先去: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