简体   繁体   English

调用REST服务时如何解决404错误?

[英]How do I troubleshoot a 404 when calling my REST service?

I created a WCF service and exposed two endpoints; 我创建了WCF服务,并公开了两个端点。 one for SOAP and one for REST. 一种用于SOAP,另一种用于REST。 The SOAP endpoint works and is reachable from web clients, console apps, etc. The REST endpoint was created this morning and doesn't work. SOAP端点可以工作,并且可以从Web客户端,控制台应用程序等访问。REST端点是今天早上创建的,无法正常工作。 I confirmed the SOAP service is still reachable so this problem is specific to the REST portion or configuration changes made this morning. 我确认SOAP服务仍然可以访问,因此此问题特定于REST部分或今天早上进行的配置更改。

In the browser, I try to navigate to http://ABC:99/json/categories . 在浏览器中,我尝试导航到http://ABC:99/json/categories I get a 404 - File or directory not found. 我得到404-找不到文件或目录。

I added the following attribute to a method in the class which implements the service interface: 我在实现服务接口的类中的方法中添加了以下属性:

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "categories")]

and this to another method: 这是另一种方法:

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "reports/{categoryId}")]

The relevant portions of the config file are here: 配置文件的相关部分在这里:

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="NewBinding0" maxBufferSize="524288" maxReceivedMessageSize="524288" transferMode="StreamedResponse"
      closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00" />
    </basicHttpBinding>
  </bindings>

  <services>
    <service name="XXX.ReportGenerator">
      <endpoint address="" binding="basicHttpBinding" bindingConfiguration="NewBinding0"
      contract="XXX.YYY.IReportGenerator">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    </service>

    <service name="ReportingSvc.JSON">
      <endpoint address="json" binding="webHttpBinding"
      contract="XXX.YYY.ReportingSvc.IReportGenerator" behaviorConfiguration="jsonEndpoint" />
      <host>
        <baseAddresses>
          <add baseAddress="http://ABC:99/" />
        </baseAddresses>
      </host>
    </service>

  </services>

  <behaviors>

    <endpointBehaviors>
      <behavior name="jsonEndpoint">
        <webHttp />
      </behavior>
    </endpointBehaviors>

    <serviceBehaviors>
      <behavior>


        <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
        <serviceMetadata httpGetEnabled="true"/>
        <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
        <serviceDebug includeExceptionDetailInFaults="true"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>

  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

Any suggestions are greatly appreciated! 任何建议,不胜感激! Thanks! 谢谢!

I can't give you specific help with REST services in WCF as I've no experience there, but WCF Tracing has been a lifesaver for me in multiple instances in the past. 由于没有经验,我无法在WCF中为您提供有关REST服务的特定帮助,但是在过去的多个实例中,WCF跟踪一直是我的救星。 Simply follow instructions to add the following to your WCF service's web.config: 只需按照说明将以下内容添加到WCF服务的web.config:

<configuration>
   <system.diagnostics>
      <sources>
            <source name="System.ServiceModel" 
                    switchValue="Information, ActivityTracing"
                    propagateActivity="true">
            <listeners>
               <add name="traceListener" 
                   type="System.Diagnostics.XmlWriterTraceListener" 
                   initializeData= "c:\log\Traces.svclog" />
            </listeners>
         </source>
      </sources>
   </system.diagnostics>
</configuration>

Obviously you'd change the path for your log as necessary and make sure the correct permissions were set on the folder you set, but that's essentially it. 显然,您将根据需要更改日志的路径,并确保在您设置的文件夹上设置了正确的权限,但实际上就是这样。 Once the trace starts being logged, you can use the Service Trace Viewer , which I believe is installed by default, to view the trace and see if you can't pinpoint the source of your problem. 一旦开始记录跟踪,就可以使用Service Trace Viewer (我相信默认情况下已安装)来查看跟踪,看看是否无法查明问题的根源。

EDIT 编辑

Please note though, that service tracing is super resource-intensive, so only turn it on when you need it to debug problems and then turn it off when you don't need it anymore. 但是请注意,服务跟踪是超级资源密集型的,因此仅在需要它以调试问题时将其打开,然后在不再需要它时将其关闭。

If your method is intended to just perform a GET operation its better to use the WebGet attribute rather than using WebInvoke and specifying your method to be GET. 如果您的方法仅用于执行GET操作,则最好使用WebGet属性而不是使用WebInvoke并将您的方法指定为GET。

Also the URL for your service for the first method would be as below: 同样,第一种方法的服务网址如下:

http://ABC:99/categories/json

URL for 2nd method would be 第二种方法的网址为

http://ABC:99/reports/5/json

This is because you are defining the address as "json" as your endpoint address which gets appended to the base URL + Resource URL + endpoint address. 这是因为您将地址定义为“ json”作为端点地址,该地址被附加到基本URL +资源URL +端点地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过失败的负载测试对WCF服务进行故障排除? - How do I troubleshoot a WCF service with failing load tests? 404在javascript中调用我的wcf服务时 - 我错过了什么? - 404 when calling my wcf service in javascript - what am I missing? 为什么WCF REST服务中的自定义动词返回404? - Why do custom verbs in my WCF REST Service return 404? 如何解决WCF中的PipeExceptions和CommunicationExceptions? - How do I troubleshoot PipeExceptions and CommunicationExceptions in WCF? 错误404调用Restful Wcf服务时 - Error 404 When calling Restful Wcf Service 当我尝试从我的服务中删除时找不到404 - 404 not found when I try to DELETE from my service 我怎么知道有多少个客户端正在调用我的WCF服务功能 - how do i know how many clients are calling my WCF service function 为什么在调用WCF服务时必须反转参数? - Why do I have to reverse the parameters when calling a WCF service? 使用Fiddler测试WCF REST服务时,如何获取基本身份验证握手? - How Do I Get By The Basic Authentication Handshake When Using Fiddler To Test A WCF REST Service? 当我在IIS中发布WCF休息服务时,服务不起作用? - When I publish a WCF rest service in IIS the services do not work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM