如何使 WCF webHttp 行为接受 HEAD 动词?

发布于 2024-08-04 05:25:10 字数 322 浏览 8 评论 0原文

我有一个托管在 Windows 服务中的 WCF 服务。 我向其中添加了一个具有 webHttp 行为的 webHttpBinding,每当我向它发送 GET 请求时,我都会得到 http 200,这就是我想要的,问题是每当我向它发送 HEAD 请求时,我都会得到 http 405。

有没有办法让它也为 HEAD 返回 http 200 ? 这可能吗?

编辑:这是运营合同:

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

I have a WCF service hosted in a Windows service.
I've added to it a webHttpBinding with a webHttp behaviour and whenever I send it a GET request I get http 200 which is what I want, problem is I get an http 405 whenever I send it a HEAD request.

Is there a way to make it return http 200 also for HEAD?
Is that even possible?

edit: that's the operation contract:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦行七里 2024-08-11 05:25:10
[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
{
#region IService 成员

    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
}
[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 works fine. I get a 405 (Method not allowed) on HEAD request. The version of assembly I am using is System.ServiceModel.Web, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.

Actually as far as I know there is no straight forward way of allowing it.However you could try something like the solution below..But this has to be done for each method that needs GET and HEAD, which makes it a not so elegant solution..

[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
}
§对你不离不弃 2024-08-11 05:25:10

听起来像是服务(甚至框架)中的严重错误。 HTTP/1.1 中对 HEAD 的支持绝不是可选的。

Sounds like a serious bug in the service (or even the framework). Support for HEAD in HTTP/1.1 is in no way optional.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文