如何使用AppDomain限制静态类的范围以进行线程安全使用? [英] How to use an AppDomain to limit a static class' scope for thread-safe use?
问题描述
我被一个架构不佳的解决方案所困扰.它不是线程安全的!
I have been bitten by a poorly architected solution. It is not thread safe!
我在解决方案中有几个共享的类和成员,在开发过程中一切都很酷...
BizTalk击沉了我的战舰.
I have several shared classes and members in the solution, and during development all was cool...
BizTalk has sunk my battle ship.
我们正在使用自定义BizTalk适配器来调用我的程序集.适配器正在调用我的代码并并行运行事物,因此我假设它在同一个AppDomain下使用多个线程.
We are using a custom BizTalk Adapter to call my assemblies. The Adapter is calling my code and running things in parallel, so I assume it is using multiple threads all under the same AppDomain.
我想做的是使我的代码在自己的AppDomain下运行,这样我遇到的共享问题将不会互相困扰.
What I would like to do is make my code run under its own AppDomain so the shared problems I have will not muck with each other.
我有一个非常简单的类,BizTalk适配器正在实例化然后运行Process()方法.
I have a very simple class that the BizTalk adapter is instantiating then running a Process() method.
我想在我的Process()方法中创建一个新的AppDomain,因此,每次BizTalk旋转另一个线程时,它将拥有自己的静态类和方法版本.
I would like to create a new AppDomain inside my Process() method, so each time BizTalk spins another thread, it will have its own version of the static classes and methods.
BizTalkAdapter代码:
BizTalkAdapter Code:
// 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调用类:
This is the class BizTalk Calls:
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中.
FYI: The Loader class is in a seperate DLL from the dataFile class.
任何帮助将不胜感激.我将继续致力于使代码成为线程安全的,但是我觉得这可能是简单"的答案.
Any help would be appreciated. I will continue to working on making the code Thread-Safe, but I feel like this could be the "simple" answer.
如果有人有其他想法,请扔进去.
If anyone has any other thought, please throw in.
谢谢,
基思
只是为了完整性.
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.
推荐答案
使用应用程序域,您可以执行以下操作:
Using app domains you could do something like this:
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);
}
}
这篇关于如何使用AppDomain限制静态类的范围以进行线程安全使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!