繁体   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