1

私はWCFを初めて使用し、仮想ディレクトリのIISでWCFサービスエンドポイントを構成したのでApi(URLはhttp://localhost/api/taskapi.svchttp://localhost/api/taskapi.svc/GetCompletedのようになります)、Webブラウザーを介して次のような応答を行う方法を探していました。完了したすべてのタスクを一覧表示するJSONであるため、ここにある2つの投稿でいくつかの回答が得られました

わかりました、うーん、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

OKあなたのバインディングはwsHttpBindingです。これを webHttpBinding に変更する (または別のエンドポイントを追加する) 必要があります。次に、次のように動作セクションに endpointBehavior を追加する必要があります。

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

この動作は、Uris をメソッドにマップする機能に結び付けられます。次に、behaviorConfiguration XML 属性を使用して、webHttpBinding エンドポイントからこの動作を参照する必要があります。

于 2012-05-18T10:45:31.487 に答える