簡體   English   中英

使用IIS在WCF上托管REST Web服務

[英]Hosting REST webservice on WCF with IIS

這是我的簡化示例:

web.config中:

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="Services.DBService">
        <endpoint address="http://localhost/" binding="webHttpBinding"
          contract="Contracts.IDBService" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="">
          <webHttp defaultOutgoingResponseFormat="Json" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

受以下合同支持:

using System.ServiceModel;

namespace Contracts
{
    [ServiceContract]
    public interface IDBService
    {
        [OperationContract]
        Services.Person GetData(string id);
    }

}

和服務:

using System.ServiceModel.Web;

namespace Services
{
    public class DBService : Contracts.IDBService
    {
        [WebInvoke(Method = "GET", UriTemplate = "data/{id}")]
        public Person GetData(string id)
        {
            return new Person()
            {
                Id = int.Parse(id),
                Name = "John Doe"
            };
        }
    }

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

通過調試器中的IIS Express運行,我可以向localhost / data / 10發出GET請求。

但是,當我嘗試將其本地發布到IIS時,嘗試轉到localhost / data / 10時會收到404錯誤。 我將其放在默認Web應用程序中,其根目錄為localhost。

我需要了解一些我不了解的UriTemplate嗎? 我做錯了什么?

它是運行.NET 4.0的應用程序池的一部分,並且此應用程序池已啟動。 錯誤代碼:

Module IIS Web Core 
Notification MapRequestHandler 
Handler StaticFile 
Error Code 0x80070002 
Requested URL http://localhost:80/Data/10 
Physical Path C:\inetpub\wwwroot\Data\10 
Logon Method Anonymous 
Logon User Anonymous

它正在嘗試查找物理位置,但顯然不存在。 我如何配置它不這樣做?

不知道要提供什么其他信息,但詢問后您會收到...

您需要在Web.config中添加一些配置設置來定義路由信息。 使IIS了解將傳入請求重定向到的位置。

在您的web.config中添加以下內容:

  <modules runAllManagedModulesForAllRequests="true">
    <add name="UrlRoutingModule"
         type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  </modules>
  <handlers>
    <add name="UrlRoutingHandler"
       preCondition="integratedMode"
       verb="*" path="UrlRouting.axd"
       type="System.Web.HttpForbiddenHandler, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </handlers>

添加對System.ServiceModel.Activation庫的引用,並在WCF庫項目中添加Global.asax文件,並在Application_Start方法中添加以下代碼。

    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(<your service>)));
    }

再次發布,您應該一切順利。

您也可以檢出此項目,以獲取在IIS中托管WCF REST服務的示例 該庫還提供自定義行為,以支持在使用URI模板標記的方法中的Typed參數。

在到處尋找實際上可行的答案之后,我在這里找到了一些東西。

基本上,您需要使用AspNetCompatibilityRequirementsAttribute標記服務,以啟用AspNet兼容性:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DBService : Contracts.IDBService

然后將標簽添加到Web.Config:

<configuration>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
</configuration>

暫無
暫無

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

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