如何在Web应用程序ASPX代码之前的页面中使用C#7? [英] How to use C# 7 within Web Application ASPX code-before pages?

查看:49
本文介绍了如何在Web应用程序ASPX代码之前的页面中使用C#7?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这是对我的问题"如何在网站项目类型中使用C#6的后续措施?)

我试图在Visual Studio 2017中使用C#7功能.

I'm trying to use C# 7 features with Visual Studio 2017.

为此,我更新了 在我现有的ASP.NET WebForms 4.6.2应用程序中,将Microsoft.Net.Compilers NuGet包打包到v2.0.0-rc4 .

For this, I've updated the Microsoft.Net.Compilers NuGet package to v2.0.0-rc4 in my existing ASP.NET WebForms 4.6.2 application.

从Visual Studio 2017内部看来,它运行良好.

It seems to work well from inside Visual Studio 2017.

当部署到我的网站时,我看到的只是一个黄色的死亡屏幕:

When deployed to my website, all I see is a yellow screen of death:

编译器错误,错误代码为255.

Compiler error with error code 255.

[详细的编译器输出]

版本信息:Microsoft .NET Framework版本:4.0.30319;ASP.NET版本:4.6.1590.0

Version information: Microsoft .NET Framework version: 4.0.30319; ASP.NET version: 4.6.1590.0

完整的编译器源代码太长,无法在此处发布(超过3万个字符),我已将其上传到Pastebin.

The full compiler source is too long to be posted here (more than 30k chars), I've uploaded it to Pastebin.

我的问题:

是否有机会在ASP.NET WebForms Web应用程序中使用最新的Roslyn编译器?

Is there any chance to use the latest Roslyn compiler within an ASP.NET WebForms Web Application?

更新1:

当然,我已经包含了 Microsoft.CodeDom.Providers.DotNetCompilerPlatform NuGet包和web.config条目,

Of course, I've included the Microsoft.CodeDom.Providers.DotNetCompilerPlatform NuGet package and web.config entries, as described here.

<system.codedom>
  <compilers>
    <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
    <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
  </compilers>
</system.codedom>

我什至尝试将上述XML中的/langversion:6 参数更改为/langversion:7 ,结果没有不同.

I've even tried to change the /langversion:6 parameter in above's XML to /langversion:7 with no different result.

更新2:

在.cshtml Razor文件中的ASP.NET MVC视图中使用C#7功能时,问题和答案可能相似.

Probably the question and answer would be similar for using C# 7 features from within ASP.NET MVC views inside .cshtml Razor files.

更新3:

我已经浏览了事件日志并找到了这些(德国系统,很抱歉):

I've dug through the Event Log and found these (German system, sorry):

Anwendung: csc.exe    
Frameworkversion: v4.0.30319    
Beschreibung: Der Prozess wurde aufgrund einer unbehandelten Ausnahme beendet.    
Ausnahmeinformationen: System.IO.FileLoadException    
   bei Microsoft.CodeAnalysis.CommandLine.BuildClient.GetCommandLineWindows(System.Collections.Generic.IEnumerable'1<System.String>)    
   bei Microsoft.CodeAnalysis.CommandLine.DesktopBuildClient.Run(System.Collections.Generic.IEnumerable'1<System.String>, System.Collections.Generic.IEnumerable'1<System.String>, Microsoft.CodeAnalysis.CommandLine.RequestLanguage, Microsoft.CodeAnalysis.CommandLine.CompileFunc, Microsoft.CodeAnalysis.IAnalyzerAssemblyLoader)    
   bei Microsoft.CodeAnalysis.CSharp.CommandLine.Program.Main(System.String[], System.String[])    
   bei Microsoft.CodeAnalysis.CSharp.CommandLine.Program.Main(System.String[])    

和:

Exception information:     
   Exception type: HttpCompileException     
   Exception message: Eine externe Komponente hat eine Ausnahme ausgelöst.    
  bei System.Web.Compilation.AssemblyBuilder.Compile()    
  bei System.Web.Compilation.BuildProvidersCompiler.PerformBuild()    
  bei System.Web.Compilation.CodeDirectoryCompiler.GetCodeDirectoryAssembly(VirtualPath virtualDir, CodeDirectoryType dirType, String assemblyName, StringSet excludedSubdirectories, Boolean isDirectoryAllowed)    
  bei System.Web.Compilation.BuildManager.CompileCodeDirectory(VirtualPath virtualDir, CodeDirectoryType dirType, String assemblyName, StringSet excludedSubdirectories)    
  bei System.Web.Compilation.BuildManager.CompileResourcesDirectory()    
  bei System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled()    
  bei System.Web.Compilation.BuildManager.CallAppInitializeMethod()    
  bei System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException)    

推荐答案

最后,我解决了.

借助 Sysinternals进程监视器,我发现了csc.exe尝试加载一个不存在的名为" csc.exe.config "的文件.

With the help of Sysinternals Process Monitor I discovered that csc.exe tries to load a file called "csc.exe.config" which was not present.

因此,我将本地网站的"bin \ roslyn"文件夹中的"csc.exe.config"文件复制到了实时网站的"bin \ roslyn"文件夹中.

So I copied the "csc.exe.config" file from my local website's "bin\roslyn" folder into my live website's "bin\roslyn" folder.

之后,一切都会按预期进行.

After that, everything works as expected.

为了安全起见,我也以相同的方式复制了"csi.exe.config",尽管似乎至少在我的测试中不需要这样做.

Just to be safe, I also copied "csi.exe.config" the same way, although it seems that this is not required, at least in my tests.

(最初不存在配置文件的原因是,我的发布脚本通常有意忽略配置文件以不覆盖现有配置).

(The reason that the config files were initially not present is that my publish scripts usually intentionally ignore configuration files to not overwrite existing configurations).

这篇关于如何在Web应用程序ASPX代码之前的页面中使用C#7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆