5

Windows サービスでホストされている WCF サービスがあります。私はそれに webHttp 動作を持つ webHttpBinding を追加しました。GET 要求を送信するたびに、必要な http 200 を取得します。問題は、HEAD 要求を送信するたびに http 405 を取得することです。

HEAD に対しても http 200 を返すようにする方法はありますか? それは可能ですか?

編集:それは操作契約です:

    [OperationContract]
    [WebGet(UriTemplate = "MyUri")]
    Stream MyContract();
4

2 に答える 2

3
[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebGet(UriTemplate="/data")]
    string GetData();
}

public class Service : IService
{
    #region IService Members

    public string GetData()
    {
        return "Hello";

    }

    #endregion
}

public class Program
{
    static void Main(string[] args)
    {
        WebHttpBinding binding = new WebHttpBinding();
        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:9876/MyService"));
        host.AddServiceEndpoint(typeof(IService), binding, "http://localhost:9876/MyService");
        host.Open();
        Console.Read();

    }
}

上記のコードは正常に機能します。HEADリクエストで405(メソッドは許可されていません)を受け取ります。私が使用しているアセンブリのバージョンは、System.ServiceModel.Web、Version = 3.5.0.0、Culture = neutral、PublicKeyToken=31bf3856ad364e35です。

実際、私が知る限り、それを許可する簡単な方法はありません。ただし、以下のソリューションのようなものを試すことができます。しかし、これはGETとHEADを必要とするメソッドごとに実行する必要があるため、それほどエレガントなソリューションではありません。 ..

[ServiceContract]
public interface IService
{
    [OperationContract]

    [WebInvoke(Method = "*", UriTemplate = "/data")]        
    string GetData();
}

パブリッククラスサービス:IService{#regionIServiceメンバー

    public string GetData()
    {
        HttpRequestMessageProperty request = 
            System.ServiceModel.OperationContext.Current.IncomingMessageProperties["httpRequest"] as HttpRequestMessageProperty;

        if (request != null)
        {
            if (request.Method != "GET" || request.Method != "HEAD")
            {
                //Return a 405 here.
            }
        }

        return "Hello";

    }

    #endregion
}
于 2009-09-07T09:07:05.013 に答える
1

サービス (ま​​たはフレームワーク) の重大なバグのように思えます。HTTP/1.1 での HEAD のサポートは、オプションではありません。

于 2009-09-07T07:35:22.473 に答える