【发布时间】:2020-04-30 10:47:25
【问题描述】:
问题
我正在尝试使用 WCF 创建一个 Rest 服务。我在调用堆栈命名空间中创建了一个名为“Coronado.HUB”的 WCF 类,如下所示。当我向服务发出 http 请求时,我收到 404 错误。
源代码
using Coronado.HUB.Model;
using Coronado.HUB.Service;
using System;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace Coronado.HUB
{
/// <summary>
/// Returns Driver By ID
/// </summary>
[ServiceContract]
public interface IDriverService
{
[OperationContract]
[WebGet]
Driver GetDriverById(Guid id);
}
/// <summary>
/// Returns Driver Object By ID
/// </summary>
public class DriverService : IDriverService
{
public Driver GetDriverById(Guid id)
{
return DriverServiceProvider.Instance.GetDriverById(id);
}
}
}
因此,在我的 ASP.NET 项目中,我添加了以下 DriverService.svc 文件和以下参考
<%@ ServiceHost Service="Coronado.HUB.DriverService" %>
配置
最后,我在 web.config 中添加了以下配置
<system.serviceModel>
<services>
<service name="Coronado.HUB.DriverService">
<endpoint address="" binding="webHttpBinding" contract="Coronado.HUB.IDriverService" behaviorConfiguration="RestService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/XXX/DriverService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="RestService">
<webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Bare" />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
我已经尝试解决这个问题一天半了,但我无法让它发挥作用。如果有人能告诉我我哪里出错了,我将不胜感激。感谢您的时间和考虑。
-- 更新--
我要解析的网址如下
http://localhost/XXX/DriverService.svc/GetDriverById?id=F200A17F-9E03-41EB-90FB-01B8665246BB
【问题讨论】:
-
您在项目中使用了哪个项目模板?我想更多地了解您的项目结构。在我看来,您没有使用 WCF 服务应用程序项目模板。因此,对服务实现类进行编程可能是无效的。此外,指定的基地址无效。它应该在 IIS 站点绑定模块中配置。
-
嗨,亚伯拉罕,我确实尝试使用 VS 项目模板,但遇到了问题。我正在使用几年前完成的另一个实现,该实现目前正在运行,没有问题。我逐字使用这些模式来实现上面演示的模式。诚然,这个实现与 VS 模板不一致,它似乎是一个有效的实现。也许我应该尝试使用现有的项目模板。
-
是的,这个实现是一个高效的实现。它与项目模板无关。在我尝试重现您的问题后,它证明可以正常工作。您如何托管您的服务?你确定服务地址是对的吗?
-
我能够确定 WCF 未在 IIS 中正确注册。一旦我通过本地计算机上的操作系统程序文件和功能在 .Net 4.7 的 WCF 选项下注册并激活了 HTTP,它就可以工作了。非常感谢您的反馈 - 这对我让这个较旧的实现和 VS 项目模板都能正常工作非常有帮助。我正在争论是使用 VS 项目模板还是坚持原来的实现。再次感谢您的帮助。