将属性应用于 WCF 服务方法以访问 Cookie
有没有办法将自定义属性应用于可以访问 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在服务方法中使用 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