How to force WCF webHttp behavior to accept HEAD verbs?

I have a WCF service hosted in a windows service. I added webHttpBinding to it with the behavior of webHttp, and whenever I send him a GET request, I get the http 200 I want, the problem is that I get http 405 whenever I send him a HEAD request.

Is there any way to return HTTP 200 for HEAD? Is it possible?

edit: what is the work contract:

    [OperationContract]
    [WebGet(UriTemplate = "MyUri")]
    Stream MyContract();
+3
source share
2 answers
[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();

    }
}

The above code is working fine. I get 405 (method not allowed) on HEAD request. The version of the assembly I'm using is 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();
}

public class Service: IService   {       #region IService Members

    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
}
+3

( ). HEAD HTTP/1.1 .

+1

Source: https://habr.com/ru/post/1717043/


All Articles