WCF:从OperationContext检索MethodInfo [英] WCF: Retrieving MethodInfo from OperationContext
问题描述
是否有一种优雅的方法可以从MessageInspector / AuthorizationPolicy /其他扩展点获取将在服务实例上执行的方法?我可以使用
Is there an elegant way to get the method that will be executed on a service instance from MessageInspector/AuthorizationPolicy/some other extension point? I could use
OperationContext.Current.IncomingMessageHeaders.Action
OperationContext.Current.IncomingMessageHeaders.Action
,但我希望有某种方法可以将SOAP操作与OperationContracts手动匹配。
but I hope there's some way to do it without manually matching SOAP actions with OperationContracts.
我要做的是在检查方法的属性之前
What I'm trying to do is examine the method's attributes before it executes.
推荐答案
花了我一辈子的时间,但我确实找到了一种比查找和拖延整个合同更好的方法:
It took me forever, but I did find a way that's better than finding and slogging through the entire contract:
string action = operationContext.IncomingMessageHeaders.Action;
DispatchOperation operation =
operationContext.EndpointDispatcher.DispatchRuntime.Operations.FirstOrDefault(o =>
o.Action == action);
// Insert your own error-handling here if (operation == null)
Type hostType = operationContext.Host.Description.ServiceType;
MethodInfo method = hostType.GetMethod(operation.Name);
您在那里。您可以获取属性,也可以执行其他任何操作。
And there you are. You can get the attributes or do whatever else you like.
注意:您可能很想尝试在DispatchRuntime中使用OperationSelector。我发现的问题是,就我而言,在处理的特定阶段,OperationSelector是空引用。如果您可以访问此属性,则使用它可能比上面的扫描 OperationCollection更快,更可靠。
Note: You might be tempted to try to use the OperationSelector in the DispatchRuntime. The problem I found was that in my case, at the particular stage of processing, the OperationSelector was a null reference. If you have access to this property, it's probably faster and more reliable to use than "scanning" the OperationCollection as above.
这篇关于WCF:从OperationContext检索MethodInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!