Visual studio locking DLL because of embedding vie

2019-09-15 05:44发布

PROJECT A contains a View , let's call it View1.ascx marked as "Embedded Resource" in the properties window

both PROJECT A and PROJECT B and C load that view1 from the PROJECTA.DLL using a custom resource provider

This way I can reuse my views across projects.

Sadly, this causes visual studio to be unable to build PROJECT B , OR C the first time around, after each change to the PROJECTA.dll

"Error  12  Could not copy "C:\GIT\PROJECTA\PROJECTA\bin\PROJECTA.dll" to "bin\PROJECTA.dll". Exceeded retry count of 10. Failed."

Is there any way to make this work? or should I somehow move all "re-used" views to a seperate assembly? The views use classes from PROJECT A so that's why I kept them inside PROJECT A

To make everything clear: Building it a second time around usually works, and the code and views are all working, it's just a really big waste of time to have to wait 10 seconds for the first build attempt to fail.

1条回答
干净又极端
2楼-- · 2019-09-15 06:29

Apparantly my assemblyresourceprovider used a AssemblyResourceVirtualFile:VirtualFile oebject that was loading my dll from Assembly.LoadFile instead of using the recommended way of loading dlls in memory as described here: http://fzysqr.com/2010/04/26/asp-net-mvc2-plugin-architecture-tutorial/ I left the old line of code in comment for you guys to see where the problem was

public override System.IO.Stream Open()
{
    string[] parts = path.Split('/');
    string assemblyName = parts[2];
    string resourceName = parts[3];


    assemblyName = Path.Combine(HttpRuntime.BinDirectory, assemblyName);
    byte[] assemblyBytes = File.ReadAllBytes(assemblyName);
    System.Reflection.Assembly assembly = Assembly.Load(assemblyBytes);
    /*System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(assemblyName);*/
    if (assembly != null)
    {
        Stream resourceStream = assembly.GetManifestResourceStream(resourceName);
        return resourceStream;
    }
    return null;
}
查看更多
登录 后发表回答