2012-04-12 112 views
3

我創建了一個寧靜服務的框架算法,顯示如何處理帖子和獲取請求。從我的例子得到工作正常,但後沒有。我想我應該添加到web.config的東西,但我不知道什麼和爲什麼。在此先感謝Zoli。wcf寧靜的服務配置錯誤

[ServiceContract] 
public interface IRestfulService 
{ 
    [OperationContract] 
    [WebGet(UriTemplate = "/GetAStudent")] 
    Student GetExistingStudent(); 

    [OperationContract] 
    [WebInvoke(UriTemplate = "/GetTheGivenStudent/{studentName}", Method = "POST")] 
    Student GetGivenStudent(string studentName); 
} 



public class RestfulService : IRestfulService 
{ 
    public Student GetExistingStudent() 
    { 
     Student stdObj = new Student 
     { 
      StudentName = "Foo", 
      Age = 29, 
      Mark = 95 
     }; 
     return stdObj; 
    } 

    public Student GetGivenStudent(string studentName) 
    { 
     Student stdObj = new Student 
     { 
      StudentName = studentName, 
      Age = 29, 
      Mark = 95 
     }; 
     return stdObj; 
    } 
} 

[DataContract] 
public class Student 
{ 
    [DataMember] 
    public string StudentName { get; set; } 
    [DataMember] 
    public int Age { get; set; } 
    [DataMember] 
    public double Mark { get; set; } 
} 

的web.config:

<system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
</system.web> 
<system.serviceModel> 
    <protocolMapping> 
     <add scheme="http" binding="webHttpBinding"/> 
    </protocolMapping> 


    <behaviors> 
     <serviceBehaviors> 
      <behavior> 
       <!-- 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> 
       <webHttp /> 
      </behavior > 
     </endpointBehaviors> 

    </behaviors> 


    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
</system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
</system.webServer> 

+2

您希望哪一個作爲POST工作?第一個沒有聲明方法,只有另一個GET – Michel 2012-04-12 14:43:30

+2

你會得到什麼例外? – 2012-04-12 14:44:16

+0

我修改了帖子。現在是對的,我希望第二個職位能成爲職位。我得到的錯誤是:終點沒有找到 – 2012-04-12 15:05:05

回答

0

你並不需要公開的MEX終結了REST服務。你的web.config應該是這個樣子:

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
    <services> 
     <service name="BookService"> 

     <!-- Expose an XML endpoint: --> 
     <endpoint name="xml" 
       address="xml" 
       binding="webHttpBinding" 
       contract="BookStore.Contracts.IBookService" 
       behaviorConfiguration="poxBehavior" /> 

     <!-- Expose a JSON endpoint: --> 
     <endpoint name="json" 
       address="json" 
       binding="webHttpBinding" 
       contract="BookStore.Contracts.IBookService" 
       behaviorConfiguration="jsonBehavior" /> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="poxBehavior"> 
      <webHttp /> 
     </behavior> 
     <endpointBehaviors> 
     <behavior name="jsonBehavior"> 
      <enableWebScript /> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    </system.serviceModel> 
</configuration> 

使用JSON上面會露出兩個端點,一個使用XML數據,和一個。揭示這樣的兩個端點當然是完全可選的;這只是你可以做的一個例子。

我也喜歡爲REST服務使用路由;像在你的Global.asax.cs:

protected void Application_Start(object sender, EventArgs e) 
{ 
    RouteTable.Routes.Add(
     new System.ServiceModel.Activation.ServiceRoute("books", 
      new System.ServiceModel.Activation.WebServiceHostFactory(), 
      typeof(BookStore.Services.BookService) 
     ) 
    ); 
} 

其中,使用上述端點的例子web.config中,將允許服務像這樣訪問:

http://yourdomain.com/books/xml 

,如果你選擇使用或添加json端點,如下所示:

http://yourdomain.com/books/json