107

I am experiencing a strange issue with VS2010. We use TFS to build our API dlls and we used to reference them in our projects usign a mapped network drive that was fully trusted. We have been working like that for at least two years and everything worked perfectly.

Today, I converted a webapp to vs2010 and when I compile it in Release, it's giving me:

SGEN : error : Could not load file or assembly 'file:///L:\Api\Release API_20100521.1\Release\CS.API.Exceptions.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)

The strange thing is that it's working when it's under the Debug profile...

I tried adding the

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

into app.config and still no luck (See http://social.msdn.microsoft.com/Forums/en/msbuild/thread/d12f6301-85bf-4b9e-8e34-a06398a60df0 and http://msdn.microsoft.com/en-us/library/dd409252(VS.100).aspx)

I am pretty sure that this issue is from visual studio or msbuild, as our code won't run from a network share when in prod because all the referenced dll's are copied into the bin folder.

If anyone has an solution (or just an idea for a search path) please let me know !

Edit : It turns out that it was working in Debug mode because generation of serialisation assemblies was turned Off. As the title say, it's really a SGEN problem since it is this utility that says that the path is not trusted...

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Developer IT
  • 1,203
  • 2
  • 11
  • 17

13 Answers13

217

I was able to fix this error by finding the assembly DLL in Windows Explorer, right clicking, choosing Properties, and then pressing the "unblock" button. The DLL has a stream that is marking it as an external file - and by clicking unblock you remove that designation.

Slaggg
  • 6,381
  • 7
  • 28
  • 27
  • worked... only developer to have this issue.. right from TFS... weird – spaghetticowboy Mar 07 '12 at 17:04
  • The reason it was locked is that my source code sat on a share. Moved the code to local disk - all went fine. (.NET4 SGEN permissions on shares issues). – thedrs May 22 '12 at 19:34
  • Please note, most corporations do not allow local admin access, or expressly disable access to the "Unblock" button for regular users, including developers. – kevinarpe Dec 11 '12 at 10:38
  • 2
    I had this problem with DLLs copied from a zip file. – 79IT May 27 '13 at 15:21
  • I just came across this issue myself today, and unblocking all of the related DLL files worked like a charm. – Seiyria Jun 27 '13 at 17:05
  • Exactly the problem I was having – Sean Aug 13 '13 at 09:00
  • @Jonas Stawski because it either didn't fix the developer's issue or they didn't try it and can't confirm it. Just because it worked for other people experiencing a similar issue doesn't mean it's the correct solution for the OP. Based on his edit the answer further down should be the accepted one. – akousmata May 07 '14 at 20:46
  • Unblocking worked for me in a solution today. This issue was only cropping up for the release build, debug build had no issue. Also, I have built this project literally dozens of times in the past to a release build without this problem. What is different with the references to the offending dlls today from what they were yesterday? I have no idea. – hermes the goat Jul 01 '14 at 20:41
  • You can unblock an entire directory using PowerShell. Navigate to the directory and then issue. `dir -Recurse | Unblock-File` – m1dst Jul 11 '14 at 08:46
  • Thanks this worked for me because I took dll from another developer – Irfan Ashraf Sep 25 '20 at 10:53
60

I just had the same/similar issue on a TFS build server where a build was referencing dll's from a network share.

The problems is that the CLR v4 security policy model has changed since previous versions and are not sandboxing assemblies as before.

To fix your issue just find the location of sgen.exe and create a sgen.exe.config in the same folder with following contents:

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

sgen.exe is usually at

"C:\Program Files\Microsoft SDKs\Windows\v[current version]\bin\NETFX 4.0 Tools"

You can read about some of the changes around CAS policies in .NET 4.0 in this blogpost: Link

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Martin Hyldahl
  • 1,638
  • 1
  • 15
  • 17
  • 1
    yeah I came across this solution but was useless for me, I did not changed anything ... we resolved as serialisation assemblies turned Off – Developer IT Aug 04 '10 at 17:50
  • 7
    For other's info, SGEN is usually at "C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools" – Steve Cooper Feb 09 '11 at 09:05
  • Developer IT: In my case couldn't turn off generation of serialisation assemblies since I needed them. But of cause turning of the serialization assemblies can be a solution as well. – Martin Hyldahl May 11 '11 at 22:20
  • I had just changed my project's "Generate serialization assembly" option from "Auto" to "On" (to try to improve WCF runtime performance) and got this error; my project was indeed on a network share (my My Documents folder is housed there). Creating the sgen.exe.config file with the above contents allowed me to build successfully. – ALEXintlsos Sep 02 '11 at 15:23
  • another simple solution - move your code from the share to a local disk. :) – thedrs May 22 '12 at 19:36
  • I get 'access is denied' error when I try to edit sgen.exe.config, even when Process Explorer (from SysInternals) shows no processes that are using that resource. How can I edit it then? – East of Nowhere May 29 '12 at 22:21
  • Found my own answer: need to run whatever text editor As Administrator (in Windows Vista/7). – East of Nowhere May 29 '12 at 22:29
  • 1
    Note that if it's a 64 bit machine you need to create under ...\Bin\NETFX 4.0 Tools\x64\ – Vivek Ayer Oct 21 '13 at 08:36
  • My install had sgen.exe in both ...\bin\ and ...\bin\NETFX 4.0 Tools, and the latter was the one that needed the config file. Confused me for a bit. – Joshua P. Swanson Jan 04 '14 at 12:02
  • 1
    For VS2015 locate it at: C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools – Farshid Dec 19 '15 at 19:26
26

Had the same problem and the config change didnt work. Only when i set Generate Serialization Assembly to off in the project properties did it work.

Rob
  • 1,663
  • 1
  • 19
  • 16
  • Worked for me as well. This is the correct answer for the OP too based on his edited comment in the question. – akousmata May 07 '14 at 20:18
  • Worked for me. Tks. – Vinicius Gonçalves May 18 '18 at 12:42
  • 3
    Project properties -> Build -> Generate serialization assembly was Auto, after setting it Off, compilation started to work like a charm. +1 and thanx. – Honza P. Mar 07 '19 at 07:36
  • That worked in my case too. Anyway I wonder what exactly means to turn it off, since by default it is on for Release configuration: I would like to be sure that it doesn't have any side-effect on the application when I publish it on production environment. – Asimov Dec 04 '19 at 11:17
  • Only one project had this issue - turned it from Auto to Off - this project was referencing a SOAP WS. – Subha Mar 28 '20 at 21:08
6

I had the same error and found my DLL was "blocked". Open up the DLL in explorer, right click -> properties -> press 'Unblock'.

http://cantgrokwontgrok.blogspot.com/2009/10/visual-studio-unknown-build-error.html

Nate Zaugg
  • 4,202
  • 2
  • 36
  • 53
3

I had this exact same problem and fixed it by adding the sgen.exe.config under C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools

with this simple config as others have said

<?xml version ="1.0"?>
<configuration>
  <runtime>
    <loadFromRemoteSources enabled="true" />
  </runtime>
</configuration>
Matt Watson
  • 980
  • 7
  • 10
2

For those of you running a 64bit version of the TFS build service, I had to create the config file in the following path:

 C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\x64

And the file contents:

<?xml version ="1.0"?>
<configuration>
<runtime>
    <loadFromRemoteSources enabled="true" />
</runtime>
</configuration>
gmasselli
  • 121
  • 1
  • 7
1

I had the same issue, loaded the assembly in the GAC and worked

gabouy
  • 755
  • 1
  • 7
  • 16
1

Just in case like me, Unblock was not a solution, as Unblock does not appear on my dll file properties. Kept looking and ended up closing my solution file and re-opening using the local C: copy instead of network UNC path to project sln file. Was able to publish after going this route.

Taersious
  • 751
  • 9
  • 20
1

Adding the snippet below to the app.config file worked in my case. I'm Running Windows XP, with VS2010 service pack 1.

<configuration>
  <runtime>
    <loadFromRemoteSources enabled="true" />
  </runtime>
</configuration>
venetia
  • 11
  • 2
1

In my case bunch of dlls were blocked.

To unblock all files in folder I used power shell with following command

dir -Path [directory path] -Recurse | Unblock-File
Daniil Grankin
  • 3,841
  • 2
  • 29
  • 39
0

I got a similar problem and I finally got over with it by removing the licenses.licx file in the Properties folder of the solution.

clemchen
  • 21
  • 5
0

Just as an FYI if you are running Windows 7 the sgen.exe file can be found at:

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools

I had to create a sgen.exe.config and place it there and then this problem went away.

ewahner
  • 1,149
  • 2
  • 11
  • 23
0

Neither the unblock nor the config worked for me. What did the trick for me was this tip about caspol. I ran

 %windir%\Microsoft.NET\Framework\v2.0.50727\CasPol.exe -m -ag 1.2 -url file://UncPathName/UncSubPath/* FullTrust

And I was ready to go, not even a VisualStudio restart required.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Yahoo Serious
  • 3,728
  • 1
  • 33
  • 37