簡體   English   中英

wcf restful service配置錯誤

[英]wcf restful service config error

我正在創建一個寧靜服務的框架算法,顯示如何處理發布和獲取請求。 從我的例子得到的工作正常,但是帖子沒有。 我想我應該添加東西到web.config,但我不知道是什么和為什么。 Zoli,提前謝謝。

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

您不需要公開REST服務的mex端點。 您的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>

上面將公開兩個端點,一個使用XML數據,另一個使用JSON。 像這樣暴露兩個端點當然是完全可選的; 這只是你能做的一個例子。

我也喜歡使用路由進行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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM