1

由于我是 WCF 的新手,并且在 IIS 中的虚拟目录Api(网址类似于http://localhost/api/taskapi.svc)中配置了 WCF 服务端点,因此我正在寻找通过 Web 浏览器发出请求的方法,例如http://localhost/api/taskapi.svc/GetCompleted会响应JSON 列出了所有已完成的任务,因此这里的这两篇文章给了我一些答案

好的,所以我将我的 OperationContract 更改为如下所示

    [OperationContract]
    [WebGet(UriTemplate = "/GetCompleted", ResponseFormat = WebMessageFormat.Json)]
    IList<Task> GetCompleted();

http://localhost/api/tasksapi.svc/GetCompleted浏览器中的 url 仍然以400 Bad Request.

服务合约

[ServiceContract]
public interface ITaskContract
{

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/task", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    TaskLibrary.Task CreateTask(TaskLibrary.Task myTask);

    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "/task", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    IList<TaskLibrary.Task> GetTasks();

    [OperationContract]
    [WebInvoke(Method = "DELETE", UriTemplate = "/task", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    bool DeleteTask(string taskId);

    [OperationContract]
    [WebInvoke(Method = "PUT", UriTemplate = "/task", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    bool UpdateTask(TaskLibrary.Task myTask);

    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "/task/{taskId}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    IList<TaskLibrary.Task> GetById(string taskId);

    [OperationContract]
    [WebInvoke(UriTemplate = "/task/completed", ResponseFormat = WebMessageFormat.Json, Method = "GET", RequestFormat = WebMessageFormat.Json)]
    IList<TaskLibrary.Task> GetCompleted();

}

服务配置

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="TaskApi.ServiceBehavior" name="TaskService.TaskService">
        <endpoint address="" binding="wsHttpBinding" contract="TaskService.ITaskContract">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="TaskApi.ServiceBehavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

参考指令

<%@ ServiceHost Language="C#" Debug="true" Service="TaskService.TaskService" %>

该服务是从作为 WCF 服务库输出的程序集中挑选的

Url 重写以隐藏svc扩展

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <rewrite>
      <rules>
        <rule name="Svc Extension remove pattern">
          <match url="^([0-9a-zA-Z\-]+)/([0-9a-zA-Z\-\.\/\(\)]+)" />
          <action type="Rewrite" url="{R:1}.svc/{R:2}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
  • 我应该怎么做才能完成这项工作?
4

1 回答 1

2

好的,您的绑定是 wsHttpBinding。您需要将其更改为 webHttpBinding(或添加另一个端点)。然后你需要在行为部分添加一个endpointBehavior,如下所示

   <endpointBehavior>
       <behavior name="rest">
           <webHttp/>
       </behavior>
   </endpointBehavior>

此行为连接到将 Uris 映射到方法的功能。然后,您需要使用 behaviorConfiguration XML 属性从 webHttpBinding 端点引用此行为

于 2012-05-18T10:45:31.487 回答