在 IIS 上运行 .net 4 WCF 服务时出现 404(无 svc 文件) [英] 404 when running .net 4 WCF service on IIS (no svc file)

查看:18
本文介绍了在 IIS 上运行 .net 4 WCF 服务时出现 404(无 svc 文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 .net 4 上的 WCF 中测试 REST 服务 - 即没有 svc 文件.它在针对 VS 开发服务器运行时效果很好,但是当我将其切换为针对 IIS 运行时,我在尝试浏览帮助页面或点击任何服务方法时收到 404.

为了让它在 IIS 上运行,我已经退回到裸机服务,但我不确定它有什么问题.

global.asax 有

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

服务本身也很简单:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]公共类 DataPortalService : IDataPortalService{[WebGet(UriTemplate = "测试/测试方法")]公共字符串 TestMethod(){return "嗨!";}}[服务合约]公共接口 IDataPortalService{【经营合同】字符串测试方法();}

的配置文件<预><代码><配置><system.web><编译调试="true" targetFramework="4.0"/></system.web><system.serviceModel><serviceHostingEnvironment aspNetCompatibilityEnabled="true"/><标准端点><webHttpEndpoint><standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/></webHttpEndpoint></standardEndpoints></system.serviceModel></配置>

点击/help 页面或该方法会给我一个 404.0.0.

我想我只是缺少 IIS 中的一些设置来将其启动,尽管它在开发服务器上运行良好但在 IIS 上运行良好有点愚蠢.

解决方案

在其他一些论坛上挖掘后解决了.

我最初在我的网络配置中添加了以下内容:

<处理程序><remove name="svc-Integrated-4.0"/><add name="svc-Integrated-4.0" path="*" verb="*" type="System.ServiceModel.Activation.ServiceHttpHandlerFactory, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0"/></处理程序></system.webServer>

显然,默认情况下 IIS 不知道如何处理无扩展请求并将它们传递给静态文件处理程序.

鉴于 MVC 使用相同的例程架构,我认为基本 MVC 站点模板必须具有与上述类似的一些配置,因为我的 MVC 站点在移至 IIS 时运行良好.

事实证明,它们的配置略有不同,而是具有以下条目:

 <validation validateIntegratedModeConfiguration="false"/><modules runAllManagedModulesForAllRequests="true"/></system.webServer>

两个配置似乎都可以正常工作,但在这种情况下我决定使用第二个选项.

I'm testing out a REST service in WCF on .net 4 - i.e. no svc file. It works great when running against the VS dev server but when I switch it to run against IIS I get 404s when trying to browse the help page or hit any of the service methods.

I've dropped back to a bare bones service to just get it running on IIS but I'm not sure what's wrong with it.

The global.asax simply has

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

and the service itself is as simple as it gets:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DataPortalService : IDataPortalService
{
    [WebGet(UriTemplate = "Test/TestMethod")]
    public string TestMethod()
    {
        return "Hi!";
    }
}

[ServiceContract]
public interface IDataPortalService
{
    [OperationContract]
    string TestMethod();
}

and config file of

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>  

</configuration>

Hitting the /help page or the method gives me a 404.0.

I presume I'm just missing some setting in IIS to kick it in to life although it's a bit daft that it works fine on the dev server but not IIS.

解决方案

Solved it after a dig around some other forums.

I initially added the following to my web config:

<system.webServer>
    <handlers>
        <remove name="svc-Integrated-4.0" />
        <add name="svc-Integrated-4.0" path="*" verb="*" type="System.ServiceModel.Activation.ServiceHttpHandlerFactory, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
</system.webServer>

Clearly by default IIS doesn't know what to do with the extensionless requests and passed them on to the static file handler.

Given that MVC is using the same routine architecture I figured that the basic MVC site template must have some config in similar to the above since my MVC sites have worked fine when moved to IIS.

It turns out that they have a slightly different config and have the following entry instead:

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

Both configs seem to work ok but I settled on using the 2nd option in this case.

这篇关于在 IIS 上运行 .net 4 WCF 服务时出现 404(无 svc 文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆