locked
SGEN "Could not load file or assembly file '' or one of its dependences. Operation is not supported." RRS feed

  • Question

  • I am getting the following error when I try to build an ASP.NET 4 project in Visual Studio 2010: "Could not load file or assembly 'file:///C:\Dev\project\trunk\bin\Elmah.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)".

    I have verified that the dll does, in fact, exist, and is getting copied to the bin folder correctly. I have also tried removing and then re-adding the reference to the project.

    The build only fails when I switch the Solution Configuration to "Release". It does not fail when the Solution Configuration is set to "Debug".

    The only difference between the two configurations (that I know of) is shown in the following Web.config transform, Web.Release.config:

     

    <?xml version="1.0"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
     <connectionStrings>
      <add name="SqlServer" connectionString="" providerName="System.Data.SqlClient" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
     </connectionStrings>
     <system.web>
      <compilation xdt:Transform="RemoveAttributes(debug)" />
      <customErrors mode="On" xdt:Transform="Replace">
       <error statusCode="404" redirect="lost.htm" />
       <error statusCode="500" redirect="uhoh.htm" />
      </customErrors>
     </system.web>
    </configuration>
    

     

    I have tried using Fusion Log Viewer to track down the assembly binding issue, but it looks like it is finding and loading the assembly correctly. Here is the log:

     

    *** Assembly Binder Log Entry (6/8/2010 @ 10:01:54 AM) ***
    
    The operation was successful.
    Bind result: hr = 0x0. The operation completed successfully.
    
    Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Running under executable c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\sgen.exe
    --- A detailed error log follows. 
    
    === Pre-bind state information ===
    LOG: User = User
    LOG: Where-ref bind. Location = C:\Dev\project\trunk\bin\Elmah.dll
    LOG: Appbase = file:///c:/Program Files (x86)/Microsoft SDKs/Windows/v7.0A/bin/NETFX 4.0 Tools/
    LOG: Initial PrivatePath = NULL
    LOG: Dynamic Base = NULL
    LOG: Cache Base = NULL
    LOG: AppName = sgen.exe
    Calling assembly : (Unknown).
    ===
    LOG: This bind starts in LoadFrom load context.
    WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
    LOG: No application configuration file found.
    LOG: Using host configuration file: 
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
    LOG: Attempting download of new URL file:///C:/Dev/project/trunk/bin/Elmah.dll.
    LOG: Assembly download was successful. Attempting setup of file: C:\Dev\project\trunk\bin\Elmah.dll
    LOG: Entering run-from-source setup phase.
    LOG: Assembly Name is: Elmah, Version=1.1.11517.0, Culture=neutral, PublicKeyToken=null
    LOG: Re-apply policy for where-ref bind.
    LOG: Where-ref bind Codebase does not match what is found in default context. Keep the result in LoadFrom context.
    LOG: Binding succeeds. Returns assembly from C:\Dev\project\trunk\bin\Elmah.dll.
    LOG: Assembly is loaded in LoadFrom load context.
    

     

    I feel like there is a fundamental lack of understanding on my part as to what exactly is going on here. Any explanation/help is much appreciated!

    • Edited by nateirwin Tuesday, June 8, 2010 6:33 PM Formatting.
    Tuesday, June 8, 2010 6:33 PM

Answers

  • This problem occurs when sgen.exe is not able to get write access to your output assembly to generate serialization assembly.

    Reason may be file is read only or it is on UNC path.

    To resolve this issue you have following options.

    1. Copy your solution to you system local drive then build your solution.
    2. Use caspol.exe to grant full trust to the UNC path.
    3. Disable Generate serialization assembly option from your project properties build tab.
    For more info please visit http://bimbim.in/post/2010/09/06/SGEN-error-Could-not-load-file-or-assembly.aspx

     

    • Marked as answer by nateirwin Friday, October 1, 2010 6:06 PM
    Friday, September 10, 2010 11:16 AM

All replies

  • Hi Nateirwin,

    Thanks for your post.

    I suspect this issue is possibly caused by untrusted assembly. Please try to enable the LoadFromRemoteSources switch in sgen.exe.config, such as:

    <configuration>
     <runtime>
      <loadFromRemoteSources enabled="true" />
     </runtime>
    </configuration>
    
    

    For more information, please check More Implicit Uses of CAS Policy: loadFromRemoteSources article.

    Regards,

    Nancy Shao [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by jabaran81 Wednesday, June 15, 2016 3:07 AM
    • Unproposed as answer by jabaran81 Wednesday, June 15, 2016 3:07 AM
    • Proposed as answer by scmanalili7775433 Monday, February 19, 2018 12:23 PM
    • Unproposed as answer by scmanalili7775433 Monday, February 19, 2018 12:23 PM
    Wednesday, June 9, 2010 3:15 AM
  • Hi Nancy,

    I'm not sure that is the problem. The dll is in the bin folder of the application. Wouldn't that error occur when the dll was on a network share?

    Wednesday, June 9, 2010 6:53 PM
  • Hi Nateirwin,

    Could you give me a small sample or detailed steps here to repro this issue? Thanks.

    Regards,

    Nancy Shao [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, June 16, 2010 7:38 AM
  • I don't know if you are still having problems with that, as the last post is quite old, but in case somebody else is looking (as I was), I solved the problem by unblocking the files. My problem was with FluentNhibernate dlls that were in my libs folder, but had been downloaded some time ago.

     

    I had to go to each file's properties one by one  (quite annoying) and click "Unblock", and my problem was solved.

    • Proposed as answer by Taz0 Tuesday, September 27, 2011 3:23 PM
    Friday, September 10, 2010 11:08 AM
  • This problem occurs when sgen.exe is not able to get write access to your output assembly to generate serialization assembly.

    Reason may be file is read only or it is on UNC path.

    To resolve this issue you have following options.

    1. Copy your solution to you system local drive then build your solution.
    2. Use caspol.exe to grant full trust to the UNC path.
    3. Disable Generate serialization assembly option from your project properties build tab.
    For more info please visit http://bimbim.in/post/2010/09/06/SGEN-error-Could-not-load-file-or-assembly.aspx

     

    • Marked as answer by nateirwin Friday, October 1, 2010 6:06 PM
    Friday, September 10, 2010 11:16 AM
  • Cheers ghallas.

     

    I can confirm from anyone else interested that this solution of unblocking files worked for me.  Switching to a release build now works.  The third part dlls i were referencing were donloaded from the web as well (TaLib)

    Saturday, September 18, 2010 10:33 AM
  • The third step solved the problem for me!

    Sorry about letting this one slide for so long; I kind of put this problem to the side for awhile, then took another look at it today.

    Thanks for the help!

    Friday, October 1, 2010 6:07 PM
  • Slightly off topic, but I found this thread when searching for the error message, so I thought I'd write about the solution, for others to see.
    For me sgen failed with the message "Could not load file or assembly file '' or one of its dependences. Operation is not supported." when running a build on TFS. The reason to why it failed turned out to be that the Build Definition´s name contained a comma ( , ). This ended up in the path where the build was performed, and caused SGEN to fail with the message "Could not load file or assembly file '' or one of its dependences. Operation is not supported."

    The fix was to remove the comma from the Build Definition Name.

    Tuesday, May 24, 2011 8:11 AM
  • You might also try to right click on the .dll and see if the file is "blocked"...it turns out that Windows keeps track of files from other source (like downloaded from the internet) and marks them as "blocked"...you can unblock these on the propery page.  That fixed it for me because I downloaded on of the .dlls that was referenced by the project I was building.

    Thursday, February 9, 2012 8:18 PM
  • Thank you! This worked for me and I love you. 
    Monday, July 29, 2013 2:01 PM
  • Hello ghallas, 

    I had the same issue. If you unblock the source ZIP or the EXE file then all files get impacted. Spent six hours before figuring this out. Hope it helps. I have had to move a DLL into Powershell Modules folder. So it was giving more CAS based errors. Once I unblock the original downloaded ZIP, then the unblock helps. 

    We still have to allow files from remote sources to true in the .EXE.CONFIG of either Visual Studio executable or powershell .. like below. 

    <?xml version="1.0" encoding="utf-8" ?> 
    <configuration> 
    <runtime> 
    <loadFromRemoteSources enabled="true"/> 
    </runtime> 
    </configuration> 

    Saturday, November 26, 2016 2:50 PM
  • Thank you, this worked for me.

    I posted your answer in stackoverflow.com

    Saturday, August 18, 2018 4:41 AM