简体   繁体   English

如何在ASP.NET MVC 5应用程序中使用Roslyn?

[英]How Can I Use Roslyn in an ASP.NET MVC 5 App?

I have an ASP.NET MVC 5 app (5.2.7 to be exact) targetting .NET Framework 4.7.2. 我有一个针对.NET Framework 4.7.2的ASP.NET MVC 5应用程序(准确地说是5.2.7)。 I'm trying to add a feature for validation rules on objects. 我正在尝试为对象的验证规则添加功能。 I have a bunch of conditions that I transform into a C# script which I want to evaluate using CSharpScript.EvaluateAsync . 我有很多条件要转换为C#脚本,我想使用CSharpScript.EvaluateAsync进行评估。

I have a prototype of this working in LINQPad and was trying to port it into the main project. 我有一个在LINQPad中工作的原型,并试图将其移植到主要项目中。 Because the main project was being developed on C# 5 in VS2013, I decided it was time to upgrade to C# 7.3 and VS2017, so I did. 因为主要项目是在VS2013中使用C#5开发的,所以我决定是时候升级到C#7.3和VS2017,所以我这样做了。 After cleaning up everything after the upgrade, I decided to implement the prototype. 在清理完升级后的所有内容之后,我决定实施原型。

I first added the Microsoft.CodeAnalysis.Scripting NuGet package to the main project, but when I tried to run the application, I got all kinds of exceptions from what I presume was library conflicts between MVC 5 and whatever the package dependencies were. 我首先将Microsoft.CodeAnalysis.Scripting NuGet程序包添加到了主项目中,但是当我尝试运行该应用程序时,我从MVC 5与程序包相关性之间的库冲突中得到了各种异常。

I then reverted and tried making a separate project in which to have the validation rules code. 然后,我还原并尝试制作一个单独的项目,在其中包含验证规则代码。 When I ran the application, it loaded and everything seemed fine, until I tried to trigger a validation rule. 当我运行该应用程序时,它已加载,并且一切看起来都很好,直到我尝试触发验证规则。 At that point it threw an exception: 当时它抛出了一个异常:

Could not load file or assembly 'Microsoft.CodeAnalysis.Scripting, Version=2.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. 无法加载文件或程序集'Microsoft.CodeAnalysis.Scripting,Version = 2.10.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'或其依赖项之一。 The system cannot find the file specified. 该系统找不到指定的文件。

In a round about way I'm back to the problem of the main project trying to load the NuGet package. 在某种程度上,我将回到主要项目尝试加载NuGet包的问题。

So,... Is there a way I can make the two work together or no? 那么,...有没有办法让我两个人一起工作? If no, what are my next choices to make it work? 如果没有,那么我接下来要做什么呢? Migrate to ASP.NET Core? 迁移到ASP.NET Core? I intend to do that in the future, just trying to avoid it right now because the solution is rather large, after 4 years of working on it... 我打算在将来做,只是尝试现在就避免,因为经过4年的努力,解决方案相当大...

The alternative, and proper, option is really to migrate to ASP.NET Core. 替代的,正确的选择实际上是迁移到ASP.NET Core。 Since 3.0 is around the corner I will probably do so at that time. 由于3.0指日可待,我那时可能会这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM