通过Web浏览器调用操作合同

时间:2012-05-17 06:11:34

标签: wcf url browser uritemplate webget

由于我是WCF新手,并且已经在虚拟目录Api的IIS中配置了WCF服务端点(网址类似于 http://localhost/api/taskapi.svc ),我一直在寻找制作方法通过网络浏览器请求类似http://localhost/api/taskapi.svc/GetCompleted的内容会回复JSON,列出所有已完成的任务,因此这两个帖子给了我一些答案

好吧嗯所以我将我的OperationContract改为如下所示

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

但浏览器中的网址http://localhost/api/tasksapi.svc/GetCompleted仍以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>
  • 我该怎么做才能使这项工作?

1 个答案:

答案 0 :(得分:2)

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

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

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

从webHttpBinding端点引用此行为