对WCF托管REST Web服务与IIS
这里是我的简单的例子:对WCF托管REST Web服务与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时,我尝试访问本地主机/数据/ 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
它试图找到物理位置,显然它不存在。我怎样才能配置它不这样做?
不知道给什么其他的信息,但要求您将收到...
到处找一个答案,实际上会工作后,我发现了一些here。
基本上,你需要用AspNetCompatibilityRequirementsAttribute
像这样以纪念服务,使ASPNET兼容性:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DBService : Contracts.IDBService
,然后标记添加到Web.config:
<configuration>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
</configuration>
启用Aspnet兼容性是一个古老的技巧,除非您的REST API仅针对与.Net生态系统客户端配合工作,否则您不应该赞成。 – vendettamit
@vendettamit,有什么选择? –
我的不好!在配置中它将是必需的,因为路由是来自Asp.net的功能之一。但是,除非您在Asp.net Ajax服务中使用AspNetCompatibilityRequirements,否则不需要使用AspNetCompatibilityRequirements标记方法。 – vendettamit
您需要在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>)));
}
再次发布,你应该很好去。
您也可以看看这个project为sample在IIS托管WCF
REST
服务。此外,该库还提供自定义行为以支持使用URI模板标记的方法中的Typed参数。
这是干什么的?它看起来像application_start是一个事件处理程序。它在哪里注册?由于它在调试器主机(IIS Express)中工作,但不在IIS上......两者有什么不同?如果它适用于一个为什么我需要添加额外的配置到另一个? –
如果你阅读答案,一切都是解释哪个部分属于哪里。它在调试器中工作,因为它们都是diff服务器。开发服务器与IIS不完全兼容。开发服务器与VS一起工作。当应用程序在IIS上时,它基于配置中指定的组件工作。 – vendettamit
GET请求使用[WebGet]属性。 见https://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.aspx
应该
[WebGet(UriTemplate = "data/{id}")]
public Person GetData(string id)
这不是可怕很有帮助,但对于REST,WCF已关闭,并且MVC/WebAPI位于。 – Chet
WebInvoke属性应该与POST请求一起使用。对于GET,您应该使用WebGet属性。 – Mimas