我在我的项目中使用 Unity 2.0,我在 Parallel.ForEach 代码块中同时读取大量文件:
Parallel.ForEach(files, currentFile =>
{
using(IMsBuildProjectLoader msBuildProject = Container.Resolve<IMsBuildProjectLoader>(new ParameterOverride("projectFileName", currentFile)))
{
// file processing
}
}
Resolve(new ParameterOverride("projectFileName", currentFile) 函数有时会抛出 ResolutionFailedException:
ResolutionFailedException: Resolution of the dependency failed,
type = "Porthus.Build.Common.Interfaces.IMsBuildProjectLoader", name = "(none)".
Exception occurred while: Calling constructor XXX.Build.Common.Types.MsBuildProjectLoader(System.String projectFileName).
Exception is: ArgumentException - Item has already been added. Key in dictionary: 'xxx' Key being added: 'xxx'
这是同时加载同一个文件的时候 - Resolve 函数正在同时创建两个具有相同参数的 IMsBuildProjectLoader 实例。它无法通过 files.Distinct() 过滤器解决。上面的代码只是解释我的问题的代码示例。
问题是:如何实现线程安全的 UnityContainer.Resolve 函数?是否可以使用一些 Unity 扩展类来做到这一点?
IMsBuildProjectLoader:
public interface IMsBuildProjectLoader : IDisposable
{
}
MsBuildProjectLoader:
public class MsBuildProjectLoader : Project, IMsBuildProjectLoader
{
public MsBuildProjectLoader(string projectFileName)
: base()
{
// Load the contents of the specified project file.
Load(projectFileName);
}
}
MsBuildProjectLoader 是这样注册的:
container.RegisterType<IMsBuildProjectLoader, MsBuildProjectLoader>();