2012-04-12 3 views
3

나는 게시 및 요청을 처리하는 방법을 보여주는 편안한 서비스의 골격 알고리즘을 만들고 있습니다. 내 예제에서 얻는 것은 잘 작동하지만 게시물은 그렇지 않습니다. 나는 내가 web.config에 물건을 추가해야한다고 생각하지만, 무엇과 왜 모르겠다. 미리 감사드립니다, 졸리.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 : 당신은 REST 서비스에 대한 MEX 끝점을 노출 할 필요가 없습니다

<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

+2

예외는 무엇입니까? –

+0

게시물을 수정했습니다. 지금 그것은 맞습니다, 나는 두 번째가 게시물로 작동 할 것으로 기대합니다. 내가 얻는 오류는 다음과 같습니다. 끝점을 찾지 못했습니다 –

답변

0

. 귀하의 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에서 같은 : 예제의 Web.config에 위의 엔드 포인트를 사용하여 서비스를 허용 것,

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) 
     ) 
    ); 
} 

은 다음과 같이 액세스 할 수있는 :

http://yourdomain.com/books/xml 

하고있는 경우 다음과 같이 json 엔드 포인트를 사용하거나 추가하도록 선택하십시오.

http://yourdomain.com/books/json