Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net web api操作筛选器中设置的CallContext值未流向控制器操作_Asp.net_Asp.net Web Api2_Custom Action Filter_Callcontext - Fatal编程技术网

Asp.net web api操作筛选器中设置的CallContext值未流向控制器操作

Asp.net web api操作筛选器中设置的CallContext值未流向控制器操作,asp.net,asp.net-web-api2,custom-action-filter,callcontext,Asp.net,Asp.net Web Api2,Custom Action Filter,Callcontext,我有一个asp.net web api,其中我们在OnActionExecuted中使用TransactionScope(仅用于POST、PUT和DELETE,但不用于GET)启动事务,执行全局操作筛选器,然后在OnActionExecuted中完成或回滚它。 最近,我们决定进行更改,以便至少可以为GET调用添加SqlAzureExecutionStrategy(因为它仅在没有用户启动的事务时才起作用),以便可以为数据获取处理瞬时故障。我遵循本文的思路,在我们的应用程序中实现了相同的东西 创建了

我有一个asp.net web api,其中我们在OnActionExecuted中使用TransactionScope(仅用于POST、PUT和DELETE,但不用于GET)启动事务,执行全局操作筛选器,然后在OnActionExecuted中完成或回滚它。 最近,我们决定进行更改,以便至少可以为GET调用添加SqlAzureExecutionStrategy(因为它仅在没有用户启动的事务时才起作用),以便可以为数据获取处理瞬时故障。我遵循本文的思路,在我们的应用程序中实现了相同的东西

创建了一个新的数据库配置类

public class AzureDbConfiguration : DbConfiguration
{
    public const string CallContextKey = "SuspendExecutionStrategy";
    public AzureDbConfiguration()
    {
        this.SetExecutionStrategy("System.Data.SqlClient", () => SuspendExecutionStrategy
          ? (IDbExecutionStrategy)new DefaultExecutionStrategy()
          : new SqlAzureExecutionStrategy());   
    }

    public static bool SuspendExecutionStrategy
    {
        get
        {
            return (bool?)CallContext.LogicalGetData(CallContextKey) ?? false;
        }
        set
        {
            CallContext.LogicalSetData(CallContextKey, value);
        }
    }


}
每当需要启动事务时,将suspendExecutionStrategy设置为true

public override void OnActionExecuting(HttpActionContext actionContext)
    {

        //checks if it is not a GET call
        if (RequiresTransactionInitiation(actionContext))
        {

           AzureDbConfiguration.SuspendExecutionStrategy = true;

            var transactionCompleter = new TransactionCompleter(GetDependencyScope(actionContext));
            transactionCompleter.UnitOfWork.StartTransaction(System.Data.IsolationLevel.ReadCommitted);

        }
        base.OnActionExecuting(actionContext);

    }

    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        //checks if it is not a GET call
        if (RequiresTransactionCompletion(actionExecutedContext))
        {
            var transactionCompleter = new TransactionCompleter(GetDependencyScope(actionExecutedContext));
            if (actionExecutedContext.Exception == null)
            {

                transactionCompleter.Complete();
                AddEventsToResponseHeader(transactionCompleter.MessageTransactions.OfType<IEventTransaction>(), actionExecutedContext.Response);
            }
            else
            {
                transactionCompleter.Rollback();
            }
        //*********** line is added to just test the value of the property .This always return false.**********
         var testValue = AzureDbConfiguration.SuspendExecutionStrategy;
        }
        base.OnActionExecuted(actionExecutedContext);
    }
控制器操作和OnActionExecuted中Thread.CurrentThread.ExecutionContext.ToStringJson()的值

{
      "LogicalCallContext": {
        "E2ETrace.ActivityID": "80000013-0003-fd00-b63f-84710c7967bb",
        "ApplicationInsights.OwinExtensions.OperationIdContext": "1c218be6-1fbb-44bf-b994-4db84115b5a3",
        "ApplicationInsights.OwinExtensions.OperationParentIdContext": "72d35ac6-a394-4205-8452-a95b36f8857a",
    })
所以我的问题是,执行上下文如何从操作过滤器更改为控制器操作,以及执行上下文中的所有其他值如何仍然保留

{
      "LogicalCallContext": {
        "E2ETrace.ActivityID": "80000013-0003-fd00-b63f-84710c7967bb",
        "ApplicationInsights.OwinExtensions.OperationIdContext": "1c218be6-1fbb-44bf-b994-4db84115b5a3",
        "ApplicationInsights.OwinExtensions.OperationParentIdContext": "72d35ac6-a394-4205-8452-a95b36f8857a",
    })