我正在尝试使用 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="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\cs4_database.mdf;integrated security=True;user instance=True;MultipleActiveResultSets=True;App=EntityFramework"" 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();
}
}
}
}
我使用的教程在浏览器上运行良好!