将属性应用于 WCF 服务方法以访问 Cookie

发布于 2024-12-20 12:01:10 字数 1283 浏览 1 评论 0原文

有没有办法将自定义属性应用于可以访问 Cookies 标头的 WCF 服务方法? WCF 服务基于 REST,并且只能通过 HTTP 传输使用。

到目前为止,我发现获取应用于我的 WCF 服务方法的属性的唯一方法是实现 IOperationBehavior 接口。这样我至少可以实例化我的属性并调用 IOperationBehavior 方法,但是我如何从那里到达可以访问 HttpContext.Current 来获取 Cookie 的地方?

我的属性是:

[AttributeUsage(AttributeTargets.Method)]
public class MyAttribute : Attribute, IOperationBehavior
{
    public MyAttribute()
    {
        int x = 1;
    }

    void IOperationBehavior.ApplyDispatchBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)
    {
        dispatchOperation.ParameterInspectors.Add(this);
    }

    void IOperationBehavior.Validate(OperationDescription operationDescription)
    {
        var context = HttpContext.Current;
        int y = 2;
    }

    public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
    {
        var context = HttpContext.Current;
        int y = 2;
    }

    public object BeforeCall(string operationName, object[] inputs)
    {
        return null;
    }
}

我使用以下方法将其应用到我的服务方法上:

public class MyService : IMyService
{
    [MyAttribute]
    public bool IsAlive()
    {
        return true;
    }
}

Is there a way to apply a custom attribute to a WCF service method that can access the Cookies header? The WCF service is REST based and will only ever be consumed over HTTP transports.

So far the only way I have found to get an attribute to apply to my WCF service methods is by implementing the IOperationBehavior interface. With that I can at least get my attribute instantiated and the IOperationBehavior methods get called but how do I get from there to somewhere I can access HttpContext.Current to get at the Cookies?

My attribute is:

[AttributeUsage(AttributeTargets.Method)]
public class MyAttribute : Attribute, IOperationBehavior
{
    public MyAttribute()
    {
        int x = 1;
    }

    void IOperationBehavior.ApplyDispatchBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)
    {
        dispatchOperation.ParameterInspectors.Add(this);
    }

    void IOperationBehavior.Validate(OperationDescription operationDescription)
    {
        var context = HttpContext.Current;
        int y = 2;
    }

    public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
    {
        var context = HttpContext.Current;
        int y = 2;
    }

    public object BeforeCall(string operationName, object[] inputs)
    {
        return null;
    }
}

I'm applying it on my service method using:

public class MyService : IMyService
{
    [MyAttribute]
    public bool IsAlive()
    {
        return true;
    }
}

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

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

发布评论

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

评论(1

故人的歌 2024-12-27 12:01:10

您可以在服务方法中使用 HttpContext.Current.Request.Cookies[] 来访问 cookie。您不需要编写自定义属性来实现它

You can use HttpContext.Current.Request.Cookies[] in your service method to access cookies. You don't need to write custom attribute to achieve it

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