尝试GET时WCF REST 404

时间:2011-12-23 18:33:54

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

我继续在一个非常基本的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。也许在Cassini或我的机器配置中有什么东西可以搞砸了?我已经阅读过Windows Vista的问题,但我使用的是Windows 7专业版。也许它与WCF服务应用程序有关,而不是WCF服务库?

1 个答案:

答案 0 :(得分:3)

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

目前,您有:

<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(合同相同)。这能解决你的问题吗?