Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The type initializer for 'File' threw an exception. #124

Closed
theLundquist opened this issue Nov 18, 2016 · 15 comments
Closed

The type initializer for 'File' threw an exception. #124

theLundquist opened this issue Nov 18, 2016 · 15 comments
Assignees
Labels
type/discuss Discussion, Question

Comments

@theLundquist
Copy link

Hi guys

I've just started up a totally empty instance of Umbraco, I've created some simple DocumentTypes with a couple of properties and the model builder keeps throwing an error.

I'll include the stack trace below, any help would be greatly appreciated.

Failed to build models.
The type initializer for 'File' threw an exception.

at Roslyn.Utilities.FileUtilities.OpenFileStream(String path)
at Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(String path, MetadataReferenceProperties properties, DocumentationProvider documentation)
at Umbraco.ModelsBuilder.ReferencedAssemblies.<>c.<.cctor>b__2_2(String x) in X:\Projects\Umbraco\ModelsBuilder\Zbu.ModelsBuilder\Umbraco.ModelsBuilder\ReferencedAssemblies.cs:line 24
at System.Linq.Enumerable.WhereSelectListIterator2.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source)
at Umbraco.ModelsBuilder.ReferencedAssemblies.<>c.<.cctor>b__2_1() in X:\Projects\Umbraco\ModelsBuilder\Zbu.ModelsBuilder\Umbraco.ModelsBuilder\ReferencedAssemblies.cs:line 23
at System.Lazy1.CreateValue() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Lazy1.get_Value()
at Umbraco.ModelsBuilder.Umbraco.ModelsBuilderBackOfficeController.GenerateModels(String modelsDirectory, String bin) in X:\Projects\Umbraco\ModelsBuilder\Zbu.ModelsBuilder\Umbraco.ModelsBuilder\Umbraco\ModelsBuilderBackOfficeController.cs:line 108
at Umbraco.ModelsBuilder.Umbraco.ModelsBuilderBackOfficeController.BuildModels() in X:\Projects\Umbraco\ModelsBuilder\Zbu.ModelsBuilder\Umbraco.ModelsBuilder\Umbraco\ModelsBuilderBackOfficeController.cs:line 35

@zpqrtbnk zpqrtbnk added type/bug Bug type/discuss Discussion, Question and removed type/bug Bug labels Nov 18, 2016
@zpqrtbnk zpqrtbnk self-assigned this Nov 18, 2016
@zpqrtbnk
Copy link
Collaborator

That's a weird one. It would indicate that BuildManager is referencing an assembly that cannot be added as a Metadata Reference by Roslyn. Just to be sure, which version of ModelsBuilder is this?

@theLundquist
Copy link
Author

The one that came bundled with Umbraco, let me get you an exact version
...
I've got the following NuGet packages installed:

  • Umbraco.ModelsBuilder v3.0.5
  • Zbu.ModelsBuilder v2.1.5

@zpqrtbnk
Copy link
Collaborator

Uh, you probably want to get rid of Zbu.ModelsBuilder to ensure you are running the latest Umbraco.ModelsBuilder v3.0.5 (no idea what happens when both are installed) - and try again?

@theLundquist
Copy link
Author

theLundquist commented Nov 18, 2016

Okay I've started again, clean MVC application installed, Umbraco installed, Umbraco.ModelsBuilder v3.0.5 installed.

Same error message :(

In the web.config I've got...

    <add key="Umbraco.ModelsBuilder.Enable" value="true" />
    <add key="Umbraco.ModelsBuilder.ModelsMode" value="AppData" />

Any ideas?

@zpqrtbnk
Copy link
Collaborator

Any chance you can zip and share the project so I can see if it does the same on my machine?
Can email at stephane@umbraco.com

@theLundquist
Copy link
Author

I'll backup the database as well for you and send that over with it :)

@zpqrtbnk
Copy link
Collaborator

thanks

@theLundquist
Copy link
Author

I've sent that off, let me know what you find out :)
Thanks for the help.

@zpqrtbnk
Copy link
Collaborator

Reproduced here. Weird issue with Roslyn trying to load 'System.IO.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies and failing... trying to understand what's going on.

@zpqrtbnk
Copy link
Collaborator

OK, turns our Roslyn (used by ModelsBuilder) cannot create metadata reference for assemblies because it is looking for, and not finding, 'System.IO.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Fusion Log shows that it does find version 4.0.1.0 in the bin directory of your site. So... it thinks that it should use that version, and fails.

I have no idea why you have System.IO.FileSystem in your bin - probably came with the other libs that you added there. One way to fix the situation is to tell .NET to always use version 4.0.1.0. In your web.config file, locate the section that looks like

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="..." publicKeyToken="..." />
      <bindingRedirect ... />
    </dependentAssembly>

and add the following redirect:

  <dependentAssembly>
    <assemblyIdentity name="System.IO.FileSystem" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="4.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
  </dependentAssembly>

With that binding in place it now works on my machine.

@theLundquist
Copy link
Author

Thanks zpqrtbnk! That fixed it, not sure were that .dll came from, apart from stock Mvc, the only nuget packages I installed were Umbraco.Cms and Microsft.CodeAnalysis.CSharp.

I'm guessing one of those dropped it in?

Anyway, thanks for the assist :)

@zpqrtbnk
Copy link
Collaborator

apart from stock Mvc, the only nuget packages I installed were Umbraco.Cms and Microsft.CodeAnalysis.CSharp

Are you 100% sure about this? I see assemblies in ~/bin such as Microsoft.AI.WindowsServer.dll that... I am not sure where they come from. Also when you say you installed Microsft.CodeAnalysis.CSharp does it mean you accepted the one that comes with Umbraco, or did you upgrade it to a more recent version?

Anyways... I tend to think that somehow you installed something that added the .dll. Never mind, glad it works!

@aranm
Copy link

aranm commented Nov 3, 2017

I had this same problem and did not find any of the above solutions to work for me. What did work was removing the reference to System.IO.FileSystem from the project. This would probably not work as well if you are storing media items locally, but my project uses the Azure blob storage provider. What seems to end up happening is that the project uses whatever assembly it can find.

@ReVoid
Copy link

ReVoid commented Mar 30, 2020

@zpqrtbnk
Same issue with Umbraco 7.15.3 :(

@aranm

I had this same problem and did not find any of the above solutions to work for me. What did work was removing the reference to System.IO.FileSystem from the project. This would probably not work as well if you are storing media items locally, but my project uses the Azure blob storage provider. What seems to end up happening is that the project uses whatever assembly it can find.

Your advice helped me (I removed the System.IO.Filesystem.dll from bin folder), but it all weird.Why it happens? Did you found a solution?

@ReVoid
Copy link

ReVoid commented Mar 30, 2020

@zpqrtbnk Do you know why that happens? I have a long life solution that was updated many times before.

I installed pure umbraco 7.15.3 solution and there was System.IO.Filesystem.dll in bin folder and it looks normal, but why that could happened with live environment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/discuss Discussion, Question
Projects
None yet
Development

No branches or pull requests

4 participants