Assembly.UnsafeLoadFrom 导致网络应用程序崩溃
Assembly.UnsafeLoadFrom causes web app crash
我正在尝试从 Internet 加载 DLL,更具体地说,它是 Azure 存储 (Blob),所以我使用 "Assembly.UnsafeLoadFrom" 这样的:
Assembly.UnsafeLoadFrom(@"https://accountname.blob.core.windows.net/test/calculator.dll");
但是因为设置了这个特定的调用,我的网络应用程序(已发布)returns:
"The specified CGI application encountered an error and the server
terminated the process."
奇怪的是,如果我使用的是本地构建,那没问题。没有崩溃,return 结果正确。
我正在使用 Visual Studio 2015 和 .net 5。
请告诉我如何解决这个问题或如何调试它。
谢谢
简单的方法,您可以通过以下代码实现您的目的:
calculator.dll
public class Calculator
{
public string HelloWorld(string userName)
{
return string.Format("Hello world, {0}!", userName);
}
}
HomeController.cs
public async Task<ActionResult> Index()
{
string url = "https://brucechen.blob.core.windows.net/dll/calculator.dll";
HttpClient client = new HttpClient();
var bytes = await client.GetByteArrayAsync(url);
//load assembly from bytes
Assembly assembly = Assembly.Load(bytes);
var calc = assembly.CreateInstance("calculator.Calculator");
//invoke the method and get result
var result = calc.GetType().InvokeMember("HelloWorld", BindingFlags.InvokeMethod, null, calc, new[] { "Bruce" });
ViewData["result"] = result;
return View();
}
结果
我正在尝试从 Internet 加载 DLL,更具体地说,它是 Azure 存储 (Blob),所以我使用 "Assembly.UnsafeLoadFrom" 这样的:
Assembly.UnsafeLoadFrom(@"https://accountname.blob.core.windows.net/test/calculator.dll");
但是因为设置了这个特定的调用,我的网络应用程序(已发布)returns:
"The specified CGI application encountered an error and the server terminated the process."
奇怪的是,如果我使用的是本地构建,那没问题。没有崩溃,return 结果正确。
我正在使用 Visual Studio 2015 和 .net 5。
请告诉我如何解决这个问题或如何调试它。
谢谢
简单的方法,您可以通过以下代码实现您的目的:
calculator.dll
public class Calculator
{
public string HelloWorld(string userName)
{
return string.Format("Hello world, {0}!", userName);
}
}
HomeController.cs
public async Task<ActionResult> Index()
{
string url = "https://brucechen.blob.core.windows.net/dll/calculator.dll";
HttpClient client = new HttpClient();
var bytes = await client.GetByteArrayAsync(url);
//load assembly from bytes
Assembly assembly = Assembly.Load(bytes);
var calc = assembly.CreateInstance("calculator.Calculator");
//invoke the method and get result
var result = calc.GetType().InvokeMember("HelloWorld", BindingFlags.InvokeMethod, null, calc, new[] { "Bruce" });
ViewData["result"] = result;
return View();
}
结果