简体   繁体   中英

Visual studio locking DLL because of embedding views using “embedded resource”

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM