c# - “您正在寻找的资源(或其依赖项之一)”错误 WCF REST
问题描述
尽管我验证了所有代码:web.config、Iservic 和实现,但我有一个不起作用的 RESTful 服务:
服务导入:
namespace SysLap.Services.Web.ImportAutoLiasse
{
[ServiceContract]
public interface IServiceImportAutoLiasse
{
[OperationContract]
[WebGet]
string GetData(int value);
}
}
服务导入.svc.cs:
namespace SysLap.Services.Web.ImportAutoLiasse
{
public class ServiceImportAutoLiasse: IServiceImportAutoLiasse
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
}
网络配置:
<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 'https://localhost:44355/ServiceImportAutoLiasse.svc/rest/GetData?value=19'. 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 'https://localhost:44355/ServiceImportAutoLiasse.svc/rest/GetData?value=19'. 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)
我该如何解决?谢谢,
解决方案
服务的名称属性和合同不能随意更改。它应该是实际的完全限定的服务实现类和服务接口,它由命名空间、句点和类型名称组成。例如WcfService1.Service1
.
因此,上面的接口定义和服务实现不对应于服务部分中的以下属性。
<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>
如果有什么我可以帮忙的,请随时告诉我。
推荐阅读
- python - 自动选择最适合磁盘/系统的块大小
- python - 问:从 2 个长度为 n 的列表 -> 1 个长度为 n 的列表,以及长度为 2 的子列表?
- graphql - 在 graphql 中替换占位符忽略枚举和字符串化
- swift - 如何创建与多个可点击按钮相结合的水平滚动视图?
- google-admin-sdk - Google Admin SDK 报告 API 活动 OAuth 令牌审核事件
- javascript - ts 文件中的模块导出不是模块错误
- c++ - 调整对象不是小部件类型 GtkBuilder
- javascript - 在 css 中用 % 改变我的宽度,改变 bootstrap flex
- javascript - 消息提及和作者返回 TypeError:无法读取未定义 Discord.js 的属性“id”
- c++ - 碎片着色器和玩家移动的 glsl 奇怪行为