modelsbuilder / ModelsBuilder.Original

The Community Models Builder for Umbraco
MIT License
114 stars 49 forks source link

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

Closed theLundquist closed 7 years ago

theLundquist commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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

zpqrtbnk commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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

zpqrtbnk commented 7 years ago

thanks

theLundquist commented 7 years ago

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

zpqrtbnk commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 6 years ago

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 commented 4 years ago

@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 commented 4 years ago

@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?