“您正在寻找的资源(或其依赖项之一)”错误 WCF REST

Posted

技术标签:

【中文标题】“您正在寻找的资源(或其依赖项之一)”错误 WCF REST【英文标题】:"The resource you are looking for (or one of its dependencies)" error WCF REST 【发布时间】:2020-04-05 22:30:24 【问题描述】:

我有一个 RESTful 服务无法运行,尽管我验证了所有代码:web.config、Iservic 和实现:

IServiceImport:

 namespace SysLap.Services.Web.ImportAutoLiasse

 [ServiceContract]
public interface IServiceImportAutoLiasse

    [OperationContract]
    [WebGet]
    string GetData(int value);   


ServiceImport.svc.cs:

namespace SysLap.Services.Web.ImportAutoLiasse
 
   public class ServiceImportAutoLiasse: IServiceImportAutoLiasse
   
    public string GetData(int value)
    
        return string.Format("You entered: 0", value);
    
   
 

Web.config:

<system.serviceModel>
<services>
  <!--SOAP-->
  <service behaviorConfiguration="ServBehavior" name="SysLap.Services.Web.ImportAutoLiasse.ServiceImportAutoLiasse">
    <endpoint address="soap" binding="basicHttpBinding" bindingConfiguration=""
      contract="SysLap.Services.Web.ImportAutoLiasse.IServiceImportAutoLiasse" />
    <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
      contract="IMetadataExchange" />

    <!--REST-->
    <endpoint address="rest" behaviorConfiguration="restBehavior"
      binding="webHttpBinding" bindingConfiguration="" contract="SysLap.Services.Web.ImportAutoLiasse.IServiceImportAutoLiasse" />
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="restBehavior">
      <webHttp helpEnabled="true" />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="ServBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

当我测试 SOAP 时,它运行良好, 但如果我测试 wcf REST:

https://localhost:44355/ServiceImportAutoLiasse.svc/rest/GetData?value=19

我有这个错误:

  HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, 
 had its name changed, or is temporarily unavailable.  Please review the following URL and make sure 
 that it is spelled correctly.

另一方面,我有其他 wcf 项目包含 SOAP 和 REST 等服务,我比较了其中的代码,一切都很好!

我在使用 POSTMAN 时得到这些信息:

[EndpointNotFoundException]: There was no channel actively listening at &#39;https://localhost:44355/ServiceImportAutoLiasse.svc/rest/GetData?value=19&#39;. This is often caused by an incorrect address URI. Ensure that the address to which the message is sent matches an address on which a service is listening.
at System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
[HttpException]: There was no channel actively listening at &#39;https://localhost:44355/ServiceImportAutoLiasse.svc/rest/GetData?value=19&#39;. This is often caused by an incorrect address URI. Ensure that the address to which the message is sent matches an address on which a service is listening.
at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result)
at System.ServiceModel.Activation.ServiceHttpHandlerFactory.ServiceHttpHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.InvokeEndHandler(IAsyncResult ar)
at System.Web.HttpApplication.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar)

我该如何解决?谢谢,

【问题讨论】:

【参考方案1】:

服务的名称属性和合同不能随意更改。它应该是实际的完全限定的服务实现类和服务接口,它由命名空间、句点和类型名称组成。例如WcfService1.Service1。 因此,上面的接口定义和服务实现与services部分的下面的属性不对应。

<services>
  <!--SOAP-->
  <service behaviorConfiguration="ServBehavior" name="SysLap.Services.Web.ImportAutoLiasse.ServiceImportAutoLiasse">
    <endpoint address="soap" binding="basicHttpBinding" bindingConfiguration=""
      contract="SysLap.Services.Web.ImportAutoLiasse.IServiceImportAutoLiasse" />
    <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
      contract="IMetadataExchange" />

    <!--REST-->
    <endpoint address="rest" behaviorConfiguration="restBehavior"
      binding="webHttpBinding" bindingConfiguration="" contract="SysLap.Services.Web.ImportAutoLiasse.IServiceImportAutoLiasse" />
  </service>
</services>

请参考官方文档。https://docs.microsoft.com/en-us/dotnet/framework/wcf/configuring-services-using-configuration-files 已更新。 配置的 WCF 服务只能通过 HTTP 协议而不是 HTTPS 工作,为了通过 HTTPS 工作,我们需要使用传输安全模式配置一个额外的服务端点。

    <services>
      <service name="WcfService1.Service1" behaviorConfiguration="myb">
        <endpoint address="soap" binding="basicHttpBinding" contract="WcfService1.IService1"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
        <endpoint address="rest" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="restbeh"></endpoint>
      <endpoint address="rest" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="restbeh" bindingConfiguration="httpsbinding"></endpoint>
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="httpsbinding">
          <security mode="Transport">
            <transport clientCredentialType="None"></transport>
          </security>
        </binding>
      </webHttpBinding>
</bindings>

如果有什么我可以帮忙的,请随时告诉我。

【讨论】:

谢谢你的回复,对不起,名字的错误是我的错,现在好了,我检查了名字的属性和合同,但总是同样的问题!那么应该怎么做才能修复呢?谢谢 你的代码sn-p还有一些小错误,请仔细检查并提问。 在我这边,它工作正常。请仔细检查客户端的调用。服务地址可能有问题。 这很奇怪!我修改我的帖子:我添加服务地址,地址就可以了, 为了使 WCF Web Http 服务在 HTTPS 协议上工作,我们需要使用传输安全模式配置一个额外的服务端点。请参考我的更新回复。

以上是关于“您正在寻找的资源(或其依赖项之一)”错误 WCF REST的主要内容,如果未能解决你的问题,请参考以下文章

WCF 请求返回错误响应

WCF 错误:相对端点地址

WCF 错误处理

如何使用故障契约和 jquery ajax 处理 wcf 错误

WCF 显示超时错误

WCF + JSONP:总是收到“方法不允许”错误消息