0

我有很多 Web 服务方法,我想在一个函数中验证所有这些方法。例如;

[Intercept]
public string Method1(POS pos, int param1, string param2)
{
    return String.Format("{0}: {1}", param1,param2);
}

[Intercept]
public int Method2(POS pos, int param3)
{
    return param3 * 2;
}

public void OnPreProcessing(...)
{
     // Before Mothod1 and Method2 called, It should enter here
     // I want to be able to cancel method execution and return another value.
     // I want to get the method name, parameter names and values         
}

现在我正在使用 ContextBoundObject 和 IMessageSink 来执行此操作。我可以获取方法名称、参数和值,但我无法取消方法执行并返回另一个值。我正在使用类似下面的东西。

public IMessage SyncProcessMessage(IMessage msg)
{
    var mcm = msg as IMethodCallMessage;
    OnPreProcessing(ref mcm);
    var retMsg = _NextSink.SyncProcessMessage(msg) as IMethodReturnMessage;
    OnPostProcessing(mcm, ref retMsg);
    return retMsg;
}

如何取消方法执行并返回另一个值?

谢谢。

4

1 回答 1

0

只需放置取消检测即可忽略呼叫。

[Intercept]
public string Method1(POS pos, int param1, string param2)
{
    return String.Format("{0}: {1}", param1,param2);
}

[Intercept]
public int Method2(POS pos, int param3)
{
    return param3 * 2;
}

public bool OnPreProcessing(...)
{
     // Before Mothod1 and Method2 called, It should enter here
     // I want to be able to cancel method execution and return another value.
     // I want to get the method name, parameter names and values         
}

例如, OnPreProcessing 返回一个布尔值,如果您需要取消调用,则为真......

public IMessage SyncProcessMessage(IMessage msg)
{
    var mcm = msg as IMethodCallMessage;
    var cancel = OnPreProcessing(ref mcm);
    var retMsg = cancel ? /*IMethodReturnMessage for cancelation*/ : _NextSink.SyncProcessMessage(msg) as IMethodReturnMessage;
    OnPostProcessing(mcm, ref retMsg);
    return retMsg;
}
于 2018-12-07T11:27:10.700 回答