zoukankan      html  css  js  c++  java
  • 确保EF上下文线程内唯一

    ShopEntities db = null;
    if (HttpContext.Current.Items["db"] == null)
    {
    db = new ShopEntities();
    HttpContext.Current.Items["db"] = db;
    }
    else
    {
    db = HttpContext.Current.Items["db"] as ShopEntities;
    }


    CallContext 类
    .NET Framework 2.0 其他版本 0(共 1)对本文的评价是有帮助 - 评价此主题
    提供与执行代码路径一起传送的属性集。无法继承此类。

    命名空间:System.Runtime.Remoting.Messaging
    程序集:mscorlib(在 mscorlib.dll 中)

    语法
    C#C++VB
    [SerializableAttribute]
    [ComVisibleAttribute(true)]
    public sealed class CallContext
    J#
    /** @attribute SerializableAttribute() /
    /
    * @attribute ComVisibleAttribute(true) */
    public final class CallContext
    JScript
    SerializableAttribute
    ComVisibleAttribute(true)
    public final class CallContext
    备注
    CallContext 是类似于方法调用的线程本地存储区的专用集合对象,并提供对每个逻辑执行线程都唯一的数据槽。数据槽不在其他逻辑线程上的调用上下文之间共享。当 CallContext 沿执行代码路径往返传播并且由该路径中的各个对象检查时,可将对象添加到其中。

    当对另一个 AppDomain 中的对象进行远程方法调用时,CallContext 类将生成一个与该远程调用一起传播的 LogicalCallContext 实例。只有公开 ILogicalThreadAffinative 接口并存储在 CallContext 中的对象被在 LogicalCallContext 中传播到 AppDomain 外部。不支持此接口的对象不在 LogicalCallContext 实例中与远程方法调用一起传输。

    Note注意
    CallContext 中的所有方法都是静态的,并且在当前 Thread 中的调用上下文上操作。
    Note注意
    此类生成链接要求。如果直接调用方没有基础结构权限,则会引发 SecurityException。有关更多信息,请参见 链接要求。
    示例
    下面的代码示例说明如何使用 CallContext 类将 主体和标识对象 传输到远程位置以进行标识。若要查看此示例中使用的 LogicalCallContextData 类的代码,请参见 ILogicalThreadAffinative 接口的示例。若要查看此示例中使用的 HelloServiceClass 类的代码,请参见 GetData 方法的示例。若要查看该示例中使用的服务器类的代码,请参见 RegisterActivatedServiceType 类的示例。

    C#C++VB
    using System;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using System.Runtime.Remoting.Messaging;
    using System.Security.Principal;
    using System.Security.Permissions;

    public class ClientClass {
    [PermissionSet(SecurityAction.LinkDemand)]
    public static void Main() {

      GenericIdentity ident = new GenericIdentity("Bob");
      GenericPrincipal prpal = new GenericPrincipal(ident, 
                                                    new string[] {"Level1"});
      LogicalCallContextData data = new LogicalCallContextData(prpal);      
      
      //Enter data into the CallContext
      CallContext.SetData("test data", data);
    
      
      Console.WriteLine(data.numOfAccesses);
    
      ChannelServices.RegisterChannel(new TcpChannel());
    
      RemotingConfiguration.RegisterActivatedClientType(typeof(HelloServiceClass),
                                                        "tcp://localhost:8082");
    
      HelloServiceClass service = new HelloServiceClass();
    
      if(service == null) {
          Console.WriteLine("Could not locate server.");
          return;
      }
    
    
      // call remote method
      Console.WriteLine();
      Console.WriteLine("Calling remote object");
      Console.WriteLine(service.HelloMethod("Caveman"));
      Console.WriteLine(service.HelloMethod("Spaceman"));
      Console.WriteLine(service.HelloMethod("Bob"));
      Console.WriteLine("Finished remote object call");
      Console.WriteLine();
    
      //Extract the returned data from the call context
      LogicalCallContextData returnedData = 
         (LogicalCallContextData)CallContext.GetData("test data");
    
      Console.WriteLine(data.numOfAccesses);
      Console.WriteLine(returnedData.numOfAccesses);
    

    }
    }

    .NET Framework 安全性
    SecurityPermission 用于操作基础结构代码。要求值:SecurityAction.LinkDemand;权限值:SecurityPermissionFlag.Infrastructure
    继承层次结构
    System.Object
    System.Runtime.Remoting.Messaging.CallContext
    线程安全
    此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
    平台
    Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

    .NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。

  • 相关阅读:
    北京一家JAVA开发公司面试题(留给后人)
    线人
    线人
    潜罪犯
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/poli/p/4298147.html
Copyright © 2011-2022 走看看