2012-04-12 12 views
3

私は、投稿と取得要求を処理する方法を示す安らかなサービスのスケルトンアルゴリズムを作成しています。私の例からgetはうまくいきますが、投稿はしません。私は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

このようなjsonエンドポイントを使用または追加することを選択してください。どちらをPOSTとして使用すると思いますか?最初のメソッドは宣言されていません。もう1つはGET – Michel

+2

例外はありますか? –

+0

投稿を修正しました。今それは正しい、私はポストとして働くことを期待する。私が得るエラーは次のとおりです。エンドポイントが見つかりません –

答えて

0

あなたはRESTサービスのMEXエンドポイントを公開する必要はありません。上記

<?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> 

2つのエンドポイント、XMLデータを使用して1、およびJSONを使用するものを公開します:あなたのweb.configファイルは次のようになります。このような2つのエンドポイントを公開することはもちろん完全にオプションです。それは単にあなたができることの例です。

また、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 

、あなたの場合

http://yourdomain.com/books/json 
関連する問題