C# WCF 服务在方法请求上返回 404

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16240217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:29:28  来源:igfitidea点击:

WCF service returning 404 on method requests

c#wcfssliis-7.5

提问by Snuffleupagus

I have a WCF service page running only WebGets/WebInvokes over SSL - it works fine on my local machine (self signed cert). On production, however, I can reach service.svc (and it gives me the message about how to consume) but service.svc/AnyRequest returns a 404. Both environments are hosted in IIS 7.5.

我有一个 WCF 服务页面,仅通过 SSL 运行 WebGets/WebInvokes - 它在我的本地机器上运行良好(自签名证书)。但是,在生产中,我可以访问 service.svc(它给了我有关如何使用的消息)但 service.svc/AnyRequest 返回 404。这两种环境都托管在 IIS 7.5 中。

I've enabled tracing and the service isn't even picking up any of the method requests (e.g. service.svc/SomeRequest), however it is processing service.svcjust fine. It's also listening at https://computername.domain.net/path/service.svc- is this normal? Should it normally be pointing to https://publicfacing.com/path/service.svc?

我启用了跟踪,服务甚至没有接收任何方法请求(例如 service.svc/SomeRequest),但是它处理service.svc得很好。它也在监听https://computername.domain.net/path/service.svc——这正常吗?它通常应该指向https://publicfacing.com/path/service.svc吗?

Also note that the production server is hosting multiple sites within IIS.

另请注意,生产服务器在 IIS 中托管多个站点。

Below is the system.serviceModel section of my web.config. The SSLBehave was suggested from here.

下面是我的 web.config 的 system.serviceModel 部分。SSLBehave 是从这里建议的。

  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="TransportSecurity">
          <security mode="Transport">
            <transport clientCredentialType="None"></transport>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="SSLBehave">
          <useRequestHeadersForMetadataAddress>
            <defaultPorts>
              <add scheme="https" port="443"/>
            </defaultPorts>
          </useRequestHeadersForMetadataAddress>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="UserManagement.ajaxAspNetAjaxBehavior">
          <webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Wrapped" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    <services>
      <service name="UserManagement.ajax" behaviorConfiguration="SSLBehave">
        <endpoint address="" behaviorConfiguration="UserManagement.ajaxAspNetAjaxBehavior"
          binding="webHttpBinding" bindingConfiguration="TransportSecurity" contract="UserManagement.ajax" />
      </service>
    </services>
  </system.serviceModel>

采纳答案by Derek Nigel Bartram

I would start by checking a number of things;

我会先检查一些事情;

  • Permissions on the hosted directory?
  • .Net version is correct?
  • Have you added the certificate to the site?
  • Try putting an image in the same path, can you navigate to that (rule out the odd occasional path mappings)
  • 托管目录的权限?
  • .Net 版本是否正确?
  • 您是否已将证书添加到站点?
  • 尝试将图像放在同一路径中,您可以导航到该路径吗(排除奇怪的偶然路径映射)

Good luck!

祝你好运!

回答by Jeffrey Kinzer

I had the same problem. From what I read, WCF isnt NT Authenticated authorization (or HTTPContext compatible) by default.

我有同样的问题。从我读到的,WCF 默认情况下不是 NT Authenticated 授权(或 HTTPContext 兼容)。

I had to add this to my config file for the WCF service web.config in the section:

我必须将它添加到我的 WCF 服务 web.config 部分的配置文件中:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

Which you did, plus this:

你做了什么,加上这个:

And on the actual service class definiation I had to add:

在实际的服务类定义中,我必须添加:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DataService : IDataDeliveryServiceContract

This fixed my problem.

这解决了我的问题。

回答by Only You

You can implement transport level security using WsHttp bindings. See this article; in your bindings try this biding instead:

您可以使用 WsHttp 绑定实现传输级安全性。见这篇文章;在你的绑定中试试这个出价:

<wsHttpBinding>
<binding name="TransportSecurity">
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</wsHttpBinding>

The article mentions you should tie up the bindings with the end points.

文章提到您应该将绑定与端点联系起来。

回答by Kambiz Shahim

As you mentioned you can access your service by .svc extension service.svcbut not in REST format service.svc/AnyRequest, the problem must be in routing integration.

正如您提到的,您可以通过 .svc 扩展名访问您的服务,service.svc但不能以 REST 格式访问service.svc/AnyRequest,问题一定出在路由集成上

add this to your web.config

将此添加到您的 web.config

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

In the IIS 6 The cause of this error must be Check that file existssetting of svc extention, make sure "Check that file exists is unchecked". For more information see IIS Hosted Service Fails.

在 IIS 6 中,此错误的原因必须是Check that file existssvc 扩展的设置,请确保未选中“检查该文件是否存在”。有关详细信息,请参阅IIS 托管服务失败

回答by Dan Leksell

Perhaps in your RouteConfig.cs file add this line:

也许在您的 RouteConfig.cs 文件中添加以下行:

routes.IgnoreRoute("{resource}.svc/{*pathInfo}");

So long as your .svc file is in the root of the application.

只要您的 .svc 文件位于应用程序的根目录中。

回答by Darkseal

The first thing I do whenever I hit a 404 with a newly-developed WCF Web Service is checking the handler mapping required to interpret this type of call, because it's often the cause of the issue. There are several ways to work around the problem, many of which require a manual execution of the ServiceModelReg.execonsole command: these are undoubtedly valid procedures but might also not work – or create additional problems – if your development machine has a particularly complex configuration. The resolution method I propose below is slightly longer to pull off, but has the advantage of solving the problem more safely and securely.

每当我使用新开发的 WCF Web 服务遇到 404 时,我做的第一件事就是检查解释此类调用所需的处理程序映射,因为这通常是问题的原因。有多种方法可以解决该问题,其中许多方法需要手动执行ServiceModelReg.exe控制台命令:这些无疑是有效的过程,但如果您的开发机器具有特别复杂的配置,它们也可能不起作用或产生其他问题。我在下面提出的解决方法要拉断时间稍长,但具有更安全可靠地解决问题的优点。

  • Open the Server Managerinterface for machine management, usually present in both the Task Barand the Start menu.
  • Go to the Dashboard (or Control Panel)and select Add Role or Featureto open the Wizard.
  • Select the Role-based or Feature-based installation typeand the server you want to work on, that is, your local / local server.
  • Go to the Featuressection: Once there, expand the .NET Framework 3.5 Featuresnode and / or the .NET Framework 4.5 Featuresnode, depending on what you have installed: if you have both, you should perform the following step twice (for each one of them).
  • Expand the WCF Services section (if available), then select HTTP Activation(see screenshot below).
  • Continue until you complete the Wizard, then click Install.
  • 打开服务器管理器界面进行机器管理,通常出现在任务栏开始菜单中
  • 转到仪表板(或控制面板)并选择添加角色或功能以打开向导。
  • 选择基于角色或基于功能的安装类型和您要使用的服务器,即您的本地/本地服务器。
  • 转到“功能”部分:在那里,展开.NET Framework 3.5 功能节点和/或.NET Framework 4.5 功能节点,具体取决于您安装的内容:如果两者都有,则应执行以下步骤两次(对于每个其中之一)。
  • 展开 WCF 服务部分(如果可用),然后选择HTTP 激活(参见下面的屏幕截图)。
  • 继续直到您完成向导,然后单击安装

enter image description here

在此处输入图片说明

Once the installation is complete, you should be able to run your WCF Service without incurring in the 404 error ever again.

安装完成后,您应该能够运行 WCF 服务而不会再次出现 404 错误。

For additional info regarding this specific issue and how to fix it, you can also read this poston my blog.

有关此特定问题以及如何修复它的其他信息,您还可以阅读我博客上的这篇文章。

回答by HeyJude

To help others that find themselves stuck with this - It may be that your service name is not the fully qualified name, which it must be.

帮助发现自己陷入困境的其他人 - 您的服务名称可能不是完全限定名称,而必须是完全限定名称

回答by Iomm1

The following setting in web.config fixed a WCF .svc 404 on a HTTPS web site :

web.config 中的以下设置修复了 HTTPS 网站上的 WCF .svc 404:

      <webHttpBinding>
             <!-- https -->
             <security mode="Transport">
                    <transport clientCredentialType = "None" proxyCredentialType="None"/>
            </security>
    </binding>
  </webHttpBinding>