Я пытаюсь создать свой собственный веб-сервис, используя Rest для университетского проекта. После выполнения некоторых уроков я смог создать свой собственный, и служба работает и возвращает результаты в визуальной студии, используя тестовый клиент wcf.Служба REST, не показывающая результаты в браузере
Однако, когда я просматриваю службу (http://localhost:53215/UserService1.svc
), я могу видеть сервисную страницу, но http://localhost:53215/UserService1.svc/GetUsersNames
отправить дает мне 404, страница не найдена ошибка!
Может ли кто-нибудь увидеть, что я делаю неправильно?
Вот мой код.
Web.config
<?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>
IUserService.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();
}
}
UserService1.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();
}
}
}
}
Учебники, которые я использовал работали отлично с браузером!
Спасибо за ваш комментарий, я попробовал это, но все равно не повезло! Я изменил адрес на "" –
Попробуйте добавить UriTemplate в атрибут WebGet операции. UriTemplate = "GetUsersNames" –
Еще не повезло! Я добавил шаблон uri, но никаких изменений! –