相关疑难解决方法(0)

静态变量实例和AppDomains,发生了什么?

我有

public static class A
{
   public static string ConnString;
}

[Serializable]
public class Test{
   // Accesing A's field;
   public string ConnString{get{return A.ConnString;}set{A.ConnString=value;}}
}

void Main()
{
   A.ConnString = "InitialString"; // I set A.ConnString in the current domain

   var newDomain = AppDomain.CreateDomain("DomNew");
   Test TObj = newDomain.CreateInstanceAndUnwrap(typeof(Test).Assembly.FullName, typeof(Test).FullName) as Test ;

   TObj.ConnString = "NewDomainString"; // It is supposed to set A.ConnString in the newDomain aka a different instance of A.ConnString

   // Here it is supposed to print two different values
   Console.WriteLine(A.ConnString); …
Run Code Online (Sandbox Code Playgroud)

c# variables static instance appdomain

23
推荐指数
2
解决办法
1万
查看次数

如何使用AppDomain限制静态类的范围以便线程安全使用?

我被一个设计糟糕的解决方案所困扰.它不是线程安全的!

我在解决方案中有几个共享类和成员,在开发期间一切都很酷......
BizTalk已经沉没了我的战舰.

我们使用自定义BizTalk适配器来调用我的程序集.适配器正在调用我的代码并并行运行,所以我假设它在同一个AppDomain下使用多个线程.

我想做的是让我的代码在自己的AppDomain下运行,这样我所拥有的共享问题就不会相互混淆.

我有一个非常简单的类,BizTalk适配器实例化然后运行Process()方法.

我想在我的Process()方法中创建一个新的AppDomain,所以每次BizTalk旋转另一个线程时,它都会有自己的静态类和方法版本.

BizTalkAdapter代码:

  // this is inside the BizTalkAdapter and it is calling the Loader class //
  private void SendMessage(IBaseMessage message, TransactionalTransmitProperties properties)
    {

        Stream strm = message.BodyPart.GetOriginalDataStream();
        string connectionString = properties.ConnectionString;
        string msgFileName = message.Context.Read("ReceivedFileName", "http://schemas.microsoft.com/BizTalk/2003/file-properties") as string;


        Loader loader = new Loader(strm, msgFileName, connectionString);
        loader.Process();

        EventLog.WriteEntry("Loader", "Successfully processed: " + msgFileName);

    }
Run Code Online (Sandbox Code Playgroud)

这是BizTalk调用类:

public class Loader
{

    private string connectionString;
    private string fileName;
    private Stream stream;
    private DataFile dataFile;

    public Loader(Stream stream, string …
Run Code Online (Sandbox Code Playgroud)

c# biztalk appdomain thread-safety

7
推荐指数
1
解决办法
3628
查看次数

如果在应用程序域中访问静态数据,会发生什么?

我有一个静态类,它有一些静态数据.如果数据从不同的应用域访问,会发生什么?

  1. 每个域都会有一个静态类的副本吗?

  2. 原始类型会被复制吗?

  3. 如果数据可序列化怎么办?

.net appdomain

6
推荐指数
2
解决办法
1928
查看次数

标签 统计

appdomain ×3

c# ×2

.net ×1

biztalk ×1

instance ×1

static ×1

thread-safety ×1

variables ×1