using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace empty
{
class Program
{
static Program()
{
AppDomain.CurrentDomain.ProcessExit += ExitHandler;
}
static void Main(string[] args)
{
}
static void ExitHandler(object o, EventArgs args)
{
using (FileStream fs = new FileStream("file.bin", FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, new double[30000000]);
}
using (FileStream fs = new FileStream("file.bin", FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, new double[30000000]);
}
Console.WriteLine("end");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望得到输出:“结束”但什么也得不到。我究竟做错了什么?
我故意使用 2 个序列化,因为 1 个序列化不会发生这种行为。
我需要在 autofac 中注册一些类型,例如 MyHandler: IRequest。这些类型驻留在程序集“A”中,到目前为止尚未引用该程序集,因此未加载到应用程序域中。我尝试使用 Assembly.LoadFile(path) 加载程序集“A”,它被加载到应用程序域中,但后来.net 运行时再次在应用程序域中加载相同的 dll,并且应用程序域中有两个“A”实例,这是我的场景中的一个问题。使用 Autofac 注册资源也无济于事。我只想知道如何防止运行时加载“A”,因为我已经加载了它。
我想创建一个具有默认权限的应用程序域,并使用默认权限将程序集加载到应用程序域中,并执行程序集内的方法.
我有一个应用程序,我需要在其中创建AppDomain和Load Assembly并执行Assembly中的方法.
这是我的代码
public class CreateAppDomain
{
public void CreateAppDom()
{
AppDomain domain = AppDomain.CreateDomain("myDomain");
domain.ExecuteAssembly(@"C:\Visual Studio 2005\Projects\A1\A1\bin\Debug\A1.dll");
domain.CreateInstanceFrom(@"C:\Visual Studio 2005\Projects\A1\A1\bin\Debug\A1.dll","A1.Navigate");
}
}
Run Code Online (Sandbox Code Playgroud)
我上面的代码是在一个名为CreateAppDomain.cs的类文件中编写的
在我的Default.aspx页面中,我创建了上面类的实例并调用了create方法.这是代码
protected void Button1_Click(object sender, EventArgs e)
{
CreateAppDomain obj = new CreateAppDomain();
obj.CreateAppDom();
Response.Write("Application Domain Successfully created");
}
Run Code Online (Sandbox Code Playgroud)
当我运行default.aspx页面时,我收到一个错误说
在程序集'A1,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'中找不到入口点.
任何人都可以向我解释上述错误的含义及其解决方案.
谢谢,
我正在学习C#。我阅读了 Andrew Troelsen 的《C# and the .NET Platform》和 Jeffrey Richter 的《CLR via C#》等书。现在,我正在尝试创建应用程序,它将从某个目录加载程序集,将它们推送到 AppDomain 并运行包含的方法(支持插件的应用程序)。这是 DLL,其中是公共接口。我将其添加到我的应用程序以及所有带插件的 DLL 中。主库.DLL
namespace MainLib
{
public interface ICommonInterface
{
void ShowDllName();
}
}
Run Code Online (Sandbox Code Playgroud)
这是插件:PluginWithOutException
namespace PluginWithOutException
{
public class WithOutException : MarshalByRefObject, ICommonInterface
{
public void ShowDllName()
{
MessageBox.Show("PluginWithOutException");
}
public WithOutException()
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
另一个:PluginWithException
namespace PluginWithException
{
public class WithException : MarshalByRefObject, ICommonInterface
{
public void ShowDllName()
{
MessageBox.Show("WithException");
throw new NotImplementedException();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个应用程序,它加载 DLL 并在另一个 AppDomain 中运行它们
namespace …Run Code Online (Sandbox Code Playgroud) 所以我用Google搜索它因为使用不安全的代码而冻结,并且AbortException仅在控制流返回托管代码时抛出.所以,在我的情况下,我有一个本机库,在一个线程中调用.所以有时我不能中止它,因为库是本机的,Abort方法不仅不做任何事,而是冻结调用线程.
所以,我想解决它.
例如,使用不同的过程应该有所帮助,但它非常复杂.
因此,一个不太重的解决方案是使用'AppDomains'.但无论如何我应该创建一个exe并调用它.我试着像这样在内存中生成它
var appDomain = AppDomain.CreateDomain("newDomain");
var assemblyBuilder = appDomain.DefineDynamicAssembly(new AssemblyName("myAsm"), AssemblyBuilderAccess.RunAndCollect);
var module = assemblyBuilder.DefineDynamicModule("myDynamicModule");
var type = module.DefineType("myStaticBulder", TypeAttributes.Public);
var methBuilder = type.DefineMethod("exec", MethodAttributes.Static | MethodAttributes.Public);
var ilGenerator = methBuilder.GetILGenerator();
Run Code Online (Sandbox Code Playgroud)
但我发现只有EMIT方式,它非常复杂.
是否存在肤浅的解决方案?
在我的ASP.NET MVC应用程序中,我将运行后台任务,有时持续约10分钟.我已经阅读了这个主题,看到应用程序域有时会重新启动,从而删除我的线程.
所以我找了一些解决方案.我找到的主要解决方案是Hangfire.我知道它会在失败等情况下重新启动作业,但是他们会以某种方式"绕过"应用程序域重新启动,还是只在这种情况下重新启动作业?
我有以下代码,它加载了我的项目引用的所有组件以及它们使用的所有引用
var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(t => t.GetTypes())
.Where(t => t.IsClass && !t.IsAbstract && (typeof (MyType).IsAssignableFrom(t)));
foreach (var type in types.Where(type => typeof (MyType).IsAssignableFrom(type)))
{... do something ...}
Run Code Online (Sandbox Code Playgroud)
在我的开发箱上这按预期工作,在其他环境中这会导致异常
System.Reflection.ReflectionTypeLoadException:无法加载一种或多种请求的类型。检索 LoaderExceptions 属性以获取更多信息。
为什么会抛出异常?是否可以重构我的代码以使其忽略异常?
appdomain ×8
c# ×7
asp.net ×3
reflection ×2
.net ×1
asp.net-mvc ×1
assemblies ×1
autofac ×1
exception ×1
hangfire ×1
unit-testing ×1