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

assembly 'System.Runtime' is needed to compile #416

Open
juanjose53 opened this issue Sep 13, 2016 · 10 comments
Open

assembly 'System.Runtime' is needed to compile #416

juanjose53 opened this issue Sep 13, 2016 · 10 comments

Comments

@juanjose53
Copy link

juanjose53 commented Sep 13, 2016

Hi Team,

I am getting the following error when I try to cumpile my template. (I am using .net 4.6.2)

Thanks for your help

  • error: (0, 0) The type 'System.Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
    Temporary files of the compilation can be found in (please delete the folder)
@ierof
Copy link

ierof commented Sep 30, 2016

I had the same problem on windows 10 update 1607.
In my case it was because of .winmd files in my app domain loaded assemblies.
They have version 255.255.255.255 of mscorlib ref (http://screencast.com/t/xZlsNAoKKga)
I solved it by ignoring .winmd files in custom ReferenceResolver https://antaris.github.io/RazorEngine/ReferenceResolver.html

private class MyIReferenceResolver : IReferenceResolver
{
    public IEnumerable<CompilerReference> GetReferences(TypeContext context, IEnumerable<CompilerReference> includeAssemblies)
    {
        return new UseCurrentAssembliesReferenceResolver()
                    .GetReferences(context, includeAssemblies)
                    .Where(f => !f.GetFile().EndsWith(".winmd"));
    }
}

@juanjose53
Copy link
Author

Can you post a complete example about how are you using that code? I am still having that problem. Thanks.

@ierof
Copy link

ierof commented Oct 11, 2016

@juanjose53 written above class usage:

var config = new TemplateServiceConfiguration { Debug = true, ReferenceResolver = new MyIReferenceResolver() };
Engine.Razor = RazorEngineService.Create(config);
var template = "Hello @Model.Name, welcome to RazorEngine!";
var text = Engine.Razor.RunCompile(template, "templateKey", null, new { Name = "World"});

Maybe you have some other problem? Can you provide some diagnostic info about your problem? Usually razor outputs debug information in %temp% directory.

For example in my case there was %temp%/RazorEngine_xrkf2y5t.mjy/ folder and vuolyf4m.cmdline file there with command parameters for csc.exe

@juanjose53
Copy link
Author

Hi Irina,

I attach the output of your example running from my app.

Thanks in advance for your help.

razorout.zip

@ierof
Copy link

ierof commented Oct 13, 2016

@juanjose53 I think you have the same issue.

I see in your output some suspicious libs:

/R:"C:\WINDOWS\system32\WinMetadata\Windows.UI.winmd" 
/R:"C:\WINDOWS\system32\WinMetadata\Windows.Foundation.winmd" 

I think custom ReferenceResolver will help you

@matthid
Copy link
Collaborator

matthid commented Oct 28, 2016

@ierof Do you want to contribute this to the docs. Or should we add this to RazorEngine itself?

@ierof
Copy link

ierof commented Nov 10, 2016

@matthid up to you. If you think it necessary, you can add in to the docs :)

@dalekawamura
Copy link

Side note to ierof's Sept 30 comment above:

The where clause is in the example is incorrect.

The where clause should be:
.Where(f => !f.GetFile().EndsWith(".winmd"));
instead of:
.Where(f => !f.GetFile().EndsWith("*.winmd"));

The example still returns "C:\WINDOWS\system32\WinMetadata\Windows.UI.winmd" and "C:\WINDOWS\system32\WinMetadata\Windows.Foundation.winmd"

@ierof
Copy link

ierof commented Mar 7, 2017

@dalekawamura thanks! You're right. My example is corrent now.

@krk
Copy link

krk commented Mar 30, 2017

Another workaround is to add System.Runtime to the assembly list.

private class SystemRuntimeResolver : IReferenceResolver
{
	private static readonly IReferenceResolver _resolver = new UseCurrentAssembliesReferenceResolver();
	private static readonly Assembly _systemRuntime = Assembly.Load("System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");

	public IEnumerable<CompilerReference> GetReferences(TypeContext context, IEnumerable<CompilerReference> includeAssemblies = null)
	{
		IEnumerable<CompilerReference> assemblies;

		var newReference = new[] { CompilerReference.From(_systemRuntime), };

		if (includeAssemblies == null)
		{
			assemblies = newReference;
		}
		else
		{
			assemblies = includeAssemblies.Concat(newReference);
		}

		return _resolver.GetReferences(context, assemblies);
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants