【问题标题】:WCF REST 404 when trying GET尝试 GET 时出现 WCF REST 404
【发布时间】:2012-01-27 00:21:50
【问题描述】:

我在一个非常基本的 WCF 服务上不断收到 404,我想通过 REST 公开该服务 - 我试图在调试时通过访问此 URL 来访问它: http://localhost:62888/Service1.svc/xml/data/test

我可以在http://localhost:62888/Service1.svc查看服务信息,但不能在http://localhost:62888/Service1.svc/xml查看服务信息

我正在使用 .Net 4 及其 WCF 服务应用程序项目。我尝试使用 Cassini 和 IIS Express 进行调试

Web.Config

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Service1">
        <!-- address is relative-->
        <endpoint address="xml" binding="webHttpBinding" behaviorConfiguration="webHttp" contract="IService1" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttp">
          <webHttp />         <!-- enables RESTful in conjunction with webHttpBinging -->
          <enableWebScript /> <!-- allows ajax communication -->
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

IService.cs

using System.ServiceModel;
using System.ServiceModel.Web;

namespace CI.WcfRestTest
{
    public interface IService1
    {
        [OperationContract]
        [WebGet(UriTemplate = "/data/{id}")]
        string GetData(string id);
    }
}

Service1.svc.cs

namespace CI.WcfRestTest
{
    public class Service1 : IService1
    {
        public string GetData(string id)
        {
            return string.Format("You entered: {0}", id);
        }
    }
}

我已经阅读了很多关于这个主题的文章,包括这篇 REST / SOAP endpoints for a WCF service 。也许卡西尼或我的机器配置中有什么东西搞砸了?我已经阅读了有关 Windows Vista 的问题,但我使用的是 Windows 7 Pro。也许它与 WCF 服务应用程序而不是 WCF 服务库有关?

【问题讨论】:

    标签: c# .net wcf rest wcf-rest


    【解决方案1】:

    这可能就像在您的配置中使用错误的服务名称一样简单。

    目前,您有:

    <services>
      <service name="Service1">
    

    但是:这需要是完全限定服务名称 - 包括任何命名空间!

    所以试试这个吧:

     <services>
        <service name="CI.WcfRestTest.Service1">
           <!-- address is relative-->
           <endpoint address="xml" 
                     binding="webHttpBinding" behaviorConfiguration="webHttp" 
                     contract="CI.WcfRestTest.IService1" />
          <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        </service>
    </services>
    

    只需将Service1 替换为CI.WcfRestTest.Service1(合同也是如此)。这能解决你的问题吗??

    【讨论】:

    • 谢谢!这有效(facepalm),我还必须删除 因为它不适用于 UriTemplate
    猜你喜欢
    • 2017-07-29
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 2010-11-07
    • 2016-03-03
    • 2016-11-23
    相关资源
    最近更新 更多