0

我有以下代码的服务

任务服务.svc

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class TodoService : ITodoService
{
    private readonly List<Todo> _dataSource;

    public TodoService()
    {
        _dataSource = new List<Todo>();
    }

    [WebGet(UriTemplate = "/Todos")]
    public List<Todo> Todos()
    {
        return _dataSource;
    }
}

数据传输对象如下

[ServiceContract]
public interface ITodoService
{
    [OperationContract]        
    List<Todo> Todos();
}

[DataContract]
public class Todo
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public bool Completed { get; set; }

}

web.config 如下

<services>
  <service name="SampleService.TodosBehavior">
    <endpoint address="" behaviorConfiguration="SampleService.TodosBehavior"
      binding="webHttpBinding" contract="SampleService.TodoService" />
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="SampleService.TodosBehavior">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>

该应用程序托管在带有虚拟目录的 IIS 7 中SampleService。当我使用浏览器调用时,我http://localhost/SampleService/TaskService.svc/Todos一无所获。但我期望一个空数组。为什么会这样?

4

1 回答 1

1

我认为 WebGet() 需要在您的界面(ServiceContract)中,而不是您对该界面的实现。此外,您是否需要 UriTemplate 中“Todos”之前的斜线?

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class TodoService : ITodoService
{
    private readonly List<Todo> _dataSource;

    public TodoService()
    {
        _dataSource = new List<Todo>();
    }

    public List<Todo> Todos()
    {
        return _dataSource;
    }
}

[ServiceContract]
public interface ITodoService
{
    [WebGet(UriTemplate = "Todos")]
    [OperationContract]        
    List<Todo> Todos();
}

如果您在 IIS 中托管,您的 TaskService.svc 文件是什么样的?

编辑:哦,这是你的 .svc 文件。没关系。

于 2012-06-27T17:07:51.750 回答