gpt4 book ai didi

c# - 如何使用 AppDomain 来限制静态类的线程安全使用范围?

转载 作者:太空狗 更新时间:2023-10-29 20:26:19 25 4
gpt4 key购买 nike

我一直被一个架构不佳的解决方案所困扰。它不是线程安全的!

我在解决方案中有几个共享类和成员,在开发过程中一切都很酷...
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);

}

这是类 BizTalk 调用:

public class Loader
{

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

public Loader(Stream stream, string fileName, string connectionString)
{
this.connectionString = connectionString;
this.fileName = fileName;
this.stream = stream;
}

public void Process()
{

//***** Create AppDomain HERE *****
// run following code entirely under that domain
dataFile = new DataFile(aredStream, fileName, connectionString);
dataFile.ParseFile();
dataFile.Save();
// get rid of the AppDomain here...

}

}

仅供引用:Loader 类位于与 dataFile 类不同的 DLL 中。

如有任何帮助,我们将不胜感激。我将继续努力使代码线程安全,但我觉得这可能是“简单”的答案。

如果大家有其他想法,欢迎补充。

谢谢你,
基思

Just for completeness.

I did find that if I marked the send adapter as "Ordered Delivery" in the "Transport Advanced Options" dialog I was able to avoid the multi-thread issues I was having.

I figure this is another possible answer to my problem, but not necessarily to the question.

最佳答案

使用应用域你可以做这样的事情:

public class Loader
{

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

public Loader(Stream stream, string fileName, string connectionString)
{
this.connectionString = connectionString;
this.fileName = fileName;
this.stream = stream;
}

public void Process()
{
//***** Create AppDomain HERE *****
string threadID = Thread.CurrentThread.ManagedThreadId.ToString();
AppDomain appDomain = AppDomain.CreateDomain(threadID);

DataFile dataFile =
(DataFile) appDomain.CreateInstanceAndUnwrap(
"<DataFile AssemblyName>",
"DataFile",
true,
BindingFlags.Default,
null,
new object[]
{
aredstream,
filename,
connectionString
},
null,
null,
null);
dataFile.ParseFile();
dataFile.Save();

appDomain.Unload(threadID);
}
}

关于c# - 如何使用 AppDomain 来限制静态类的线程安全使用范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/307292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com