【问题标题】:Invoke a Operation Contract through web browser通过 Web 浏览器调用操作合同
【发布时间】:2012-05-17 06:11:33
【问题描述】:

由于我是 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();

但浏览器中的 url 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>

参考指令

&lt;%@ ServiceHost Language="C#" Debug="true" Service="TaskService.TaskService" %&gt;

服务是从作为 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>
  • 我应该怎么做才能完成这项工作?

【问题讨论】:

  • 你能显示你的服务配置吗?您的端点是如何定义的?你向我们展示了 .svc 文件服务指令吗?
  • @RichardBlewett 当然可以,等几分钟让我编辑帖子
  • @RichardBlewett 抱歉耽搁了,我已经添加了有关该问题的服务的配置信息。你能帮忙解决吗

标签: wcf url browser uritemplate webget


【解决方案1】:

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

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

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

【讨论】:

  • 似乎还有另一个配置错误由于 EndpointDispatcher 的 AddressFilter 不匹配,接收方无法处理带有 To URL/api/taskapi.svc/tasks 的消息。检查发送方和接收方的 EndpointAddresses 是否一致。
  • 两件事:我以为你有一个 URL 重写器来删除 .svc;您正在使用任务作为您的 uri,但您的合同声明 GetCompleted - 您能否给我们新配置,以防万一现在出现拼写错误或其他情况
  • 更新了源代码。我只是来自服务合同,当我用GET 请求调用/Tasks 时,会调用GetTasks 方法。我说的对吗?
  • GetTask 将根据您的合同使用 /task (no s) 调用,但我认为错误来自假设 SOAP 的事实 - 我们可以查看您的服务配置
  • 我对 wcf 的服务配置完全陌生,您是指 web.config 还是 app.config 文件?我在 wcf 服务库项目中获得了 app.config,在 wcf 服务应用程序中获得了 web.config
猜你喜欢
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-16
相关资源
最近更新 更多