public ComponentRegisterTests() { _hub = Substitute.For <IMessageHub>(); _logger = Substitute.For <ILogger>(); _componentRegister = new ComponentRegister(_hub, _logger); }
public void Test_construction_with_mixed_null_parameters_throws_null_exception(IMessageHub hub, ILogger logger) { // Arrange. // Act. // Assert. Assert.Throws <ArgumentNullException>(() => _componentRegister = new ComponentRegister(hub, logger)); }
public void ProcessItemsAsync() { // Need to set up the logs. // Need to set up the event system. // Need to create everything. // Need to register the component(s). // Need to raise the start event. // Need to subscribe to the completed events for each component. var coreLogger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console() .WriteTo.File("coreLog.txt", rollingInterval: RollingInterval.Day) .CreateLogger(); var hub = MessageHub.Instance; coreLogger.Information($"{GetType().Name} (ProcessItemsAsync): Logs created."); coreLogger.Information($"{GetType().Name} (ProcessItemsAsync): Creating components..."); coreLogger.Information($"{GetType().Name} (ProcessItemsAsync): Creating 'Weather' component."); IComponentCreator weatherComponentCreator = new WeatherComponentCreator(); //IComponentCreator weatherComponentCreatorTwo = new WeatherComponentCreator(); //IComponentCreator weatherComponentCreatorThree = new WeatherComponentCreator(); coreLogger.Information($"{GetType().Name} (ProcessItemsAsync): Creating 'Football' component."); IComponentCreator footballComponentCreator = new FootballComponentCreator(); var componentRegister = new ComponentRegister(hub, coreLogger); var registeredCorrectly = componentRegister.RegisterComponent(weatherComponentCreator, WeatherComponent.Constants.WeatherConstants.FullFileName); //registeredCorrectly = componentRegister.RegisterComponent(weatherComponentCreatorTwo, WeatherComponent.Constants.WeatherConstants.FullFileNameTwo); //registeredCorrectly = componentRegister.RegisterComponent(weatherComponentCreatorThree, WeatherComponent.Constants.WeatherConstants.FullFileNameThree); registeredCorrectly = componentRegister.RegisterComponent(footballComponentCreator, FootballComponent.Constants.FootballConstants.FullFileName); componentRegister.RegisterSubscriptions(); try { // We don't want to call this anymore, what we want to do is set up the event hub to subscribe // to a call to process the registered components. // Then we want the components to publish a completed event. // Business Business, Numbers... (Psst, is this working?) (Yes) YAAAAYY!! hub.Publish("Start Processing..."); } catch (Exception exception) { coreLogger.Error($"{GetType().Name} (ProcessItemsAsync): The application threw the following exception: {exception.Message}."); } }
private static void StartService(string[] args) { ComponentRegister.Register(ComponentType.Server); _logger = IoCContainer.Instance.Resolve <ILogger>(); AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; var logFileConfig = GetLogConfiguration(); _logger.AddLogDestination(new FileLogDestination(logFileConfig)); _logger.Log(string.Format("Server starting up local time - {0}", DateTime.UtcNow.ToLocalTime())); if (Environment.UserInteractive) { _logger.AddLogDestination(new ConsoleLogDestination()); _logger.Log("Detected User Interactive session."); Console.CancelKeyPress += Console_CancelKeyPress; _logger.Log("Press Ctrl+C to shut down server."); } _logger.Start(); _logger.Log("Starting up HostService"); _hostService = new HostService(); _logger.Log("HostService running."); _stopService.WaitOne(); _logger.Log("Server shutting down..."); _hostService.Stop(); _logger.Log("Server shut down."); //last thing to do is shut down logging system _logger.Stop(); //last thing that happens ever _serviceStopped.Set(); }
public async Task MainAsync() { var client = new DiscordSocketClient(); var container = ComponentRegister.BuildContainer(); var commandcontroller = container.Resolve <CommandController>(); client.Log += Log; client.MessageReceived += commandcontroller.Hanlde; string token = File.ReadAllText("token.txt"); await client.LoginAsync(TokenType.Bot, token); await client.StartAsync(); // Block this task until the program is closed. await Task.Delay(-1); }
static void Main(string[] args) { ComponentRegister.RegistDomain <NatashaAssemblyDomain>(); ComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); ComponentRegister.RegisteSyntax <NatashaCSharpSyntax>(); //var hwFunc = FastMethodOperator // .RandomDomain() // .Param(typeof(string), "str1") // .Param<string>("str2") // .Body("return str1+str2;") // .Return<string>() // .Compile<Func<string, string, string>>(); //Console.WriteLine(hwFunc("Hello", " World!")); var a123 = NClass.UseDomain(typeof(Program).GetDomain()); var domain = DomainManagement.Random; var type = NDelegate.UseDomain(domain, item => item.AssemblyName = "a").GetType($"[assembly: AssemblyKeyFileAttribute(\"c:\\\\vs2019\\\\natasha.snk\")]" + "[assembly: AssemblyVersion(\"1.3.3.3\")]public class A{ public A(){Name=\"1\"; }public string Name;}"); var func = NDelegate.UseDomain(domain).Func <string>("return (new A()).Name;"); Console.WriteLine(type.FullName); Console.WriteLine(func()); //type.RemoveReferences(); type = NDelegate.UseDomain(domain, item => item.AssemblyName = "a").GetType($"[assembly: AssemblyKeyFileAttribute(\"c:\\\\vs2019\\\\natasha.snk\")]" + "[assembly: AssemblyVersion(\"2.3.3.4\")]public class A{ public A(){Name=\"2\"; }public string Name;}"); func = NDelegate.UseDomain(domain).Func <string>("return (new A()).Name;"); Console.WriteLine(type.FullName); Console.WriteLine(func()); domain = DomainManagement.Create("a"); using (DomainManagement.Lock("a")) { Console.WriteLine(domain == (NatashaAssemblyDomain)AssemblyLoadContext.CurrentContextualReflectionContext); } Console.ReadKey(); }
static PrepareTest() { ComponentRegister.RegistDomain <NatashaAssemblyDomain>(); ComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); ComponentRegister.RegisteSyntax <NatashaCSharpSyntax>(); }
public IGameRenderer CreateGameRenderer(Type boardType) { var componentRegister = ComponentRegister.GetComponentRegisterInstance(); return(componentRegister.ResolveComponent <IGameRenderer>(boardType)); }
static void Main(string[] args) { ComponentRegister.RegistDomain <NatashaAssemblyDomain>(); ComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); ComponentRegister.RegisteSyntax <NatashaCSharpSyntax>(); NErrorLog.Enabled = false; NSucceedLog.Enabled = false; NWarningLog.Enabled = false; Stopwatch watch = new Stopwatch(); double tempTotleTime; #region Natasha Preheating var preTime = Process.GetCurrentProcess().TotalProcessorTime; watch.Start(); tempType = NClass.CreateDomain("tes1t") .Namespace("Test") .UseRandomName() .PublicField <string>("Name") .PublicField <string>("Age") .PublicField <int[]>("Temp") .Ctor(item => item.Body("Temp = new int[40960];")) .GetType(); watch.Stop(); tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime); if (watch.Elapsed.Seconds > 0) { tempTotleTime = tempTotleTime / watch.Elapsed.Seconds; } Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("Natasha预热:"); Console.WriteLine("-----------------------------------------------------------------------------------------"); Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); Console.WriteLine("-----------------------------------------------------------------------------------------"); #endregion Thread.Sleep(1000); #region Run Compiler preTime = Process.GetCurrentProcess().TotalProcessorTime; watch.Restart(); Test(); watch.Stop(); tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime); if (watch.Elapsed.Seconds > 0) { tempTotleTime = tempTotleTime / watch.Elapsed.Seconds; } Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"{count}个独立域编译后:"); Console.WriteLine("-----------------------------------------------------------------------------------------"); Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); Console.WriteLine("-----------------------------------------------------------------------------------------"); #endregion Thread.Sleep(1000); #region Release Handler preTime = Process.GetCurrentProcess().TotalProcessorTime; watch.Restart(); Release(); watch.Stop(); tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime); if (watch.Elapsed.Seconds > 0) { tempTotleTime = tempTotleTime / watch.Elapsed.Seconds; } Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("释放中:"); Console.WriteLine("-----------------------------------------------------------------------------------------"); Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); Console.WriteLine("-----------------------------------------------------------------------------------------"); #endregion Thread.Sleep(1000); #region Run GC preTime = Process.GetCurrentProcess().TotalProcessorTime; watch.Restart(); RunGc(); watch.Stop(); tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime); if (watch.Elapsed.Seconds > 0) { tempTotleTime = tempTotleTime / watch.Elapsed.Seconds; } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(); Console.WriteLine("回收后:"); Console.WriteLine("-----------------------------------------------------------------------------------------"); Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); Console.WriteLine("-----------------------------------------------------------------------------------------"); #endregion Thread.Sleep(1000); #region Check Alive preTime = Process.GetCurrentProcess().TotalProcessorTime; watch.Restart(); var alive = CheckAlive(); DomainManagement.Clear(); watch.Stop(); tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime); if (watch.Elapsed.Seconds > 0) { tempTotleTime = tempTotleTime / watch.Elapsed.Seconds; } Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(); Console.WriteLine($"存活检测: {alive}"); Console.WriteLine("-----------------------------------------------------------------------------------------"); Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); Console.WriteLine("-----------------------------------------------------------------------------------------"); #endregion //for (int i = 0; i < 10; i++) //{ // Thread.Sleep(3000); // preTime = Process.GetCurrentProcess().TotalProcessorTime; // Console.WriteLine($"第{i}次静默检测:"); // Console.WriteLine("-----------------------------------------------------------------------------------------"); // Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); // Console.WriteLine("-----------------------------------------------------------------------------------------"); //} Console.ReadKey(); }
static void Main(string[] args) { ComponentRegister.RegistDomain <NatashaAssemblyDomain>(); ComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); ComponentRegister.RegisteSyntax <NatashaCSharpSyntax>(); //var @operator = FastMethodOperator.DefaultDomain(); //var actionDelegate = @operator // .Param(typeof(string), "parameter") // .Body("Console.WriteLine(parameter);") // .Compile(); //actionDelegate.DynamicInvoke("HelloWorld!"); //var action = (Action<string>)actionDelegate; //action("HelloWorld!"); //actionDelegate.DisposeDomain(); //起个类 NClass nClass = NClass.DefaultDomain(); nClass .Namespace("MyNamespace") .Public() .Name("MyClass") .Ctor(ctor => ctor.Public().Body("MyField=\"Hello\";")) .Property(prop => prop .Type(typeof(string)) .Name("MyProperty") .Public() .OnlyGetter("return \"World!\";") ); //添加方法 MethodBuilder mb = new MethodBuilder(); mb .Public() .Override() .Name("ToString") .Body("return MyField+\" \"+MyProperty;") .Return(typeof(string)); nClass.Method(mb); //添加字段 FieldBuilder fb = nClass.GetFieldBuilder(); fb.Public() .Name("MyField") .Type <string>(); //动态调用动态创建的类 var action = NDelegate .RandomDomain() .Action("Console.WriteLine((new MyClass()).ToString());", nClass.GetType()); action(); action.DisposeDomain(); //Console.WriteLine(typeof(List<int>[]).GetRuntimeName()); //Console.WriteLine(typeof(List<int>[,]).GetRuntimeName()); //Console.WriteLine(typeof(int[,]).GetRuntimeName()); //Console.WriteLine(typeof(int[][]).GetRuntimeName()); //Console.WriteLine(typeof(int[][,,,]).GetRuntimeName()); Console.ReadKey(); }