2

我正在尝试使用 Rest 为大学项目创建自己的 Web 服务。在完成一些教程之后,我已经能够创建自己的并且该服务正在运行并使用 wcf 测试客户端在 Visual Studio 中返回结果。

但是,当我浏览到服务时(http://localhost:53215/UserService1.svc),我可以看到服务页面,但http://localhost:53215/UserService1.svc/GetUsersNames发送给我 404,找不到页面错误!

谁能看到我做错了什么?

这是我的代码。

网页配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>    
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
    </compilation>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfRestSample.IUserService1">
        <endpoint address="" contract="WcfRestSample.IUserService1" binding="webHttpBinding" behaviorConfiguration="restBehavior"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="restBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <connectionStrings>
      <add name="cs4_databaseEntities" connectionString="metadata=res://*/cs4_model.csdl|res://*/cs4_model.ssdl|res://*/cs4_model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\cs4_database.mdf;integrated security=True;user instance=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

用户服务.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfRestSample
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IUserService1" in both code and config file together.
    [ServiceContract]
    public interface IUserService1
    {
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Xml)]
        List<string> GetUsersNames();
    }
}

用户服务1.svc

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfRestSample
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "UserService1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select UserService1.svc or UserService1.svc.cs at the Solution Explorer and start debugging.
    public class UserService1 : IUserService1
    {
        public List<string> GetUsersNames()
        {
            using (cs4_databaseEntities entities = new cs4_databaseEntities())
            {
                return entities.Users.Select(user => user.Name).ToList();
            }
        }
    }
}

我使用的教程在浏览器上运行良好!

4

2 回答 2

4

您的端点是 http://localhost:53215/UserService1.svc/rest/GetUsersNames. “rest”部分来自您的配置 'address="rest"'

编辑:

服务名称设置为接口而不是实现类,更改:

<service name="WcfRestSample.IUserService1">

<service name="WcfRestSample.UserService1">

这样做的一个后果是服务不再加载到 wcf 测试客户端中,也许这就是它造成混淆的原因 - 有些人记录了他们使用 wcf 测试客户端的步骤,而其他人则记录了使用 Web 浏览器的步骤,但它很容易解决改回使用界面!

于 2013-11-12T15:51:03.757 回答
1

更改<service name="WcfRestSample.IUserService1"><service name="WcfRestSample.UserService1">. 感谢评论中的用户-Marvin Smitt

于 2013-11-13T12:55:54.717 回答