내가 코드의 Parallel.ForEach 블록 내에서 동시에 파일을 많이 읽고 있어요 어디에 내 프로젝트에 유니티 2.0을 사용하고 있습니다 :UNITY : 생성자 삽입을 사용하여 스레드 안전 Container.Resolve() 함수를 구현하는 방법은 무엇입니까?
Parallel.ForEach(files, currentFile =>
{
using(IMsBuildProjectLoader msBuildProject = Container.Resolve<IMsBuildProjectLoader>(new ParameterOverride("projectFileName", currentFile)))
{
// file processing
}
}
해결 (새 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'
이 같은 파일을 동시에로드 할 때입니다 - 함수가 같은 시간에 같은 매개 변수를 사용하여 두 개의 IMsBuildProjectLoader 인스턴스를 만드는 해결이 files.Distinct (에 의해 해결 될 수 없습니다.) 위의 코드는 내 문제를 설명하는 코드 예제 일뿐입니다.
질문은입니다 : 스레드 안전 UnityContainer.Resolve 함수를 구현하는 방법은 무엇입니까? Unity 확장 클래스를 사용하여이 작업을 수행 할 수 있습니까?
IMsBuildProjectLoader :
public interface IMsBuildProjectLoader : IDisposable
{
}
MsBuildProjectLoader : MsBuildProjectLoader이 방법 등록
public class MsBuildProjectLoader : Project, IMsBuildProjectLoader
{
public MsBuildProjectLoader(string projectFileName)
: base()
{
// Load the contents of the specified project file.
Load(projectFileName);
}
}
:
container.RegisterType<IMsBuildProjectLoader, MsBuildProjectLoader>();
이 질문에 직접 대답하지는 않지만 잘못 생각한 것 같습니다. 클래스에 IMsBuildProjectLoader가 삽입되어야하고 루프에서 파일 이름을 매개 변수로 사용하는 IMsBuildProjectLoader에서 메서드를 호출해야합니다. 스레드 안전은 IMsBuildProjectLoader 구현에 구워 져야합니다. 내 0.02 달러. – BFree
네, 그 또 다른 옵션입니다. 감사 – Ludwo