Cannot load a reference assembly for execution from a Web-Site project

时间:2017-08-30 20:09:32

标签: c# asp.net web-site-project .net-standard

Using .NET 4.6.2 and an older Web-Site (not Web-Application) project. If I clear the BIN directory and then build and run it works, but sometimes after multiple builds and runs, it fails with this error.

for (i = 0; i < ExpensesDataGridView.Rows.Count; i++){}

Is there any trick to getting Web-Site projects to work correctly when the libraries they use are beginning to pull in netstandard 2.0?

Also, it seems that this assembly binding redirect is necessary to get it to run but nuget adds a redirect to an older version 4.1.1.0. Any suggestions on how to fix that other than manually editing this line after most nuget updates?

foreach

Would all these issues go away if the project was migrated to a Web-Application project?

2 个答案:

答案 0 :(得分:0)

您正试图加载.NET Framework程序集的非托管动态链接库或可执行文件(例如Windows系统DLL)。下面的示例通过使用Assembly.LoadFile方法加载Kernel32.dll来说明这一点。

Copy
// Windows DLL (non-.NET assembly)
string filePath = Environment.ExpandEnvironmentVariables("%windir%");
if (! filePath.Trim().EndsWith(@"\"))
   filePath += @"\";
filePath += @"System32\Kernel32.dll";

try {
   Assembly assem = Assembly.LoadFile(filePath);
}
catch (BadImageFormatException e) {
   Console.WriteLine("Unable to load {0}.", filePath);
   Console.WriteLine(e.Message.Substring(0,
                     e.Message.IndexOf(".") + 1));
}
// The example displays an error message like the following:
//       Unable to load C:\WINDOWS\System32\Kernel32.dll.
//       The module was expected to contain an assembly manifest.

答案 1 :(得分:-1)

是的,您可以将网站转换为网络应用程序:

screen grab

如果您无法看到此配置 - 可能您的类名称存在问题,例如:

  

/Directory/Index.aspx.cs

     

/Directory2/Index.aspx.cs