//Add a listener to the event manager that will receive any events of the supplied event name. public bool AttachListener(IEventListener listener, string eventName, DelegateEventHandler handler) { if (listener == null || eventName == null) { Debug.Log("Event Manager: AttachListener failed due to no listener or event name specified."); return false; } if (!m_listenerTable.ContainsKey(eventName)) { m_listenerTable.Add(eventName, new ArrayList()); } ArrayList listenerList = m_listenerTable[eventName] as ArrayList; if (listenerList.Contains(handler)) { Debug.Log("Event Manager: Listener: " + listener.GetType().ToString() + " is already in list for event: " + eventName); return false; //listener already in list } listenerList.Add(handler); return true; }
public static void ShutdownRequest_Cancelled() { var queue = new ManualEventQueue(); queue.Subscribers.Add( DelegateEventHandler.OnShutdownRequest( e => e.Cancel = true), weakRef: false); // you can only add one request at a time Assert.True(queue.RequestShutdown()); Assert.False(queue.RequestShutdown()); // cancel the request Assert.True(queue.HandleNext()); // no shutting down event Assert.False(queue.HandleNext()); // you can add another request, once the previous one is out of the queue Assert.True(queue.RequestShutdown()); Assert.True(queue.HandleNext()); Assert.False(queue.HandleNext()); }
public ServiceContainerBuilderBuildTest(ITestOutputHelper output) { var services = new ServiceContainerBuilder(); services.AddSingleton <IIdGenerator, GuidIdGenerator>(); services.AddSingleton(GuidIdGenerator.Instance); services.AddSingleton <IUserIdProvider, EnvironmentUserIdProvider>(); services.AddSingleton <EnvironmentUserIdProvider>(); services.AddSingleton <IEventPublisher, EventQueuePublisher>(); services.AddSingleton <IOptions <EventQueuePublisherOptions> >( new OptionsWrapper <EventQueuePublisherOptions>(new EventQueuePublisherOptions())); services.AddSingleton <IEventQueue, EventQueueInMemory>(); services.AddSingleton <EventHandlerBase <TestEvent> >(DelegateEventHandler.FromAction <TestEvent>(_ => { })); services.AddSingleton(typeof(IEventHandler <>), typeof(ServiceCollectionBuildTest.TestGenericEventHandler <>)); _serviceProvider = services.BuildFluentAspectsContainer(options => { options.InterceptAll() .With <TestOutputInterceptor>(); }); }
//Add a listener to the event manager that will receive any events of the supplied event name. public bool AttachListener(IEventListener listener, string eventName, DelegateEventHandler handler) { if (listener == null || eventName == null) { Debug.Log("Event Manager: AttachListener failed due to no listener or event name specified."); return(false); } if (!m_listenerTable.ContainsKey(eventName)) { m_listenerTable.Add(eventName, new ArrayList()); } ArrayList listenerList = m_listenerTable[eventName] as ArrayList; if (listenerList.Contains(handler)) { Debug.Log("Event Manager: Listener: " + listener.GetType().ToString() + " is already in list for event: " + eventName); return(false); //listener already in list } listenerList.Add(handler); return(true); }
public static void Main(string[] args) { Console.WriteLine("----------DotNetCoreSample----------"); // var dataLogger = LogHelper.GetLogger(typeof(DataExtension)); // DataExtension.CommandLogAction = msg => dataLogger.Debug(msg); //AspectTest.ServiceContainerTest(); //ProcessExecutorTest.RawProcessTest(); //ProcessExecutorTest.DotNetInfoTest(); ProcessExecutorTest.DotNetNugetGlobalPackagesInfoTest(); Console.ReadLine(); var services = new ServiceCollection(); services.AddTransient <IFly, MonkeyKing>(); IConfiguration configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .Build(); var city = configuration.GetAppSetting("City"); var number = configuration.GetAppSetting <int>("Number"); Console.WriteLine($"City:{city}, Number:{number}"); services.AddSingleton(configuration); //services.AddSingleton<IEventStore, EventStoreInMemory>(); //services.AddSingleton<IEventBus, EventBus>(); //services.AddSingleton(DelegateEventHandler.FromAction<CounterEvent>(@event => // LogHelper.GetLogger(typeof(DelegateEventHandler<CounterEvent>)) // .Info($"Event Info: {@event.ToJson()}") // ) //); services.AddSingletonProxy <IFly, MonkeyKing>(); Action <DbContextOptionsBuilder> dbContextOptionsAction = options => { options.UseInMemoryDatabase("Test"); }; services.AddDbContext <TestDbContext>(dbContextOptionsAction); services.AddScopedProxy <TestDbContext>(); services.AddEvents(); services.AddSingletonProxy <IEventBus, EventBus>(); services.AddFluentAspects(options => { options.NoInterceptPropertyGetter <IFly>(f => f.Name); options.InterceptAll() .With <LogInterceptor>() ; options.InterceptMethod <DbContext>(x => x.Name == nameof(DbContext.SaveChanges) || x.Name == nameof(DbContext.SaveChangesAsync)) .With <DbContextSaveInterceptor>() ; options.InterceptMethod <IFly>(f => f.Fly()) .With <LogInterceptor>(); options.InterceptType <IFly>() .With <LogInterceptor>(); options .WithProperty("TraceId", "121212") ; }) // .UseCastleProxy() // .UseAspectCoreProxy() ; DependencyResolver.SetDependencyResolver(services); //var fly = DependencyResolver.ResolveService<IFly>(); //Console.WriteLine(fly.Name); //fly.Fly(); //fly.OpenFly<int>(); //fly.OpenFly<string>(); //fly.FlyAway(); //var animal1 = FluentAspects.AspectOptions.ProxyFactory.CreateInterfaceProxy<IAnimal<int>>(); //animal1.Eat(); //var animal2 = FluentAspects.AspectOptions.ProxyFactory.CreateInterfaceProxy<IAnimal<string>>(); //animal2.Eat(); //var animal = FluentAspects.AspectOptions.ProxyFactory.CreateProxy<Animal<string>>(); //animal.Eat(); //animal.Eat(); //Console.WriteLine(animal.GetEatCount()); //animal.Drink("xxx"); //Console.WriteLine(animal.GetDrinkCount()); //animal = FluentAspects.AspectOptions.ProxyFactory.CreateProxyWithTarget<Animal<string>>(new Animal<string>()); //animal.Eat(); //animal.Eat(); //Console.WriteLine(animal.GetEatCount()); //animal.Drink("xxx"); //Console.WriteLine(animal.GetDrinkCount()); //DependencyResolver.TryInvokeService<TestDbContext>(dbContext => //{ // dbContext.TestEntities.Add(new TestEntity() { Token = "sasa", CreatedTime = DateTime.Now, }); // var hasChanges = dbContext.ChangeTracker.HasChanges(); // Console.WriteLine($"hasChanges:{hasChanges}"); // dbContext.SaveChanges(); //}); //DependencyResolver.TryInvokeService<IEventBus>(eventBus => //{ // eventBus.Publish(new CounterEvent()); //}); DependencyResolver.TryInvoke <IProxyFactory>(proxyFactory => { var counterEvent = new CounterEvent() { Counter = 1 }; var eventBusProxy = proxyFactory.CreateProxy <IEventBus, EventBus>(); eventBusProxy.Publish(counterEvent); var handlerProxy = proxyFactory.CreateProxyWithTarget <IEventHandler <CounterEvent> >(DelegateEventHandler.FromAction <CounterEvent>(e => { Console.WriteLine(e.ToJson()); })); handlerProxy.Handle(counterEvent) .GetAwaiter().GetResult(); }); //DependencyResolver.TryInvokeServiceAsync<TestDbContext>(dbContext => //{ // dbContext.TestEntities.Add(new TestEntity() { Token = "sasa", CreatedTime = DateTime.Now, }); // if (dbContext.ChangeTracker is null) // { // Console.WriteLine($"{nameof(dbContext.ChangeTracker)} is null ..."); // } // else // { // foreach (var entry in dbContext.ChangeTracker.Entries<TestEntity>()) // { // Console.WriteLine(entry.Entity.Token); // } // } // if (dbContext.Database is null) // { // Console.WriteLine($"{nameof(dbContext.Database)} is null ..."); // } // if (dbContext.Database is null) // { // Console.WriteLine($"{nameof(dbContext.Database)} is null ..."); // } // return dbContext.SaveChangesAsync(); //}); //DependencyResolver.ResolveRequiredService<IFly>() // .Fly(); //DependencyInjectionTest.Test(); // EventTest.MainTest(); // SerilogTest.MainTest(); //var builder = new ContainerBuilder(); //builder.RegisterType<MonkeyKing>().As<IFly>(); //DependencyResolver.SetDependencyResolver((IServiceProvider)new AutofacDependencyResolver(builder.Build())); //DependencyInjectionTest.Test(); //int a = 1; //Console.WriteLine(JsonConvert.SerializeObject(a));// output 1 //var pagedListModel = new PagedListModel<int>() //{ // PageNumber = 1, // PageSize = 4, // TotalCount = 10, // Data = new[] { 1, 2, 3, 4 } //}; //Console.WriteLine(pagedListModel.ToJson()); // log test // LoggerTest.MainTest(); // Log4NetTest.MainTest(); //ILoggerFactory loggerFactory = new LoggerFactory(); //loggerFactory.AddConsole(); //loggerFactory.AddDebug(); //loggerFactory.AddDelegateLogger( // (category, logLevel, exception, msg) => // { // Console.WriteLine($"{category}:[{logLevel}] {msg}\n{exception}"); // } //); //var logger = new Logger<Program>(loggerFactory); //logger.LogInformation("Logging information from Microsoft.Extensions.Logging"); //InvokeHelper.TryInvoke(DataExtensionTest.MainTest); //TaskTest.TaskWhenAllTest().GetAwaiter().GetResult(); //Base64UrlEncodeTest.MainTest(); //var a = new { Name = "2123" }; //var name = a.GetProperty("Name").GetValueGetter().Invoke(a); //Console.WriteLine(name); //var structTest = new TestStruct() { Name = "1233" }; //var obj = (object)structTest; //Console.WriteLine(structTest.GetProperty("Name").GetValueGetter<TestStruct>().Invoke(structTest)); //structTest.GetProperty("Name").GetValueSetter().Invoke(obj, "Name1"); //structTest = (TestStruct)obj; //Console.WriteLine(structTest.Name); //Expression<Func<TestEntity, bool>> exp = t => t.Id > 10 && t.Token == "123" && t.Token.Contains("12"); //var str = SqlExpressionParser.ParseExpression(exp); //Console.WriteLine("sql: {0}", str); //exp = t => true; //str = SqlExpressionParser.ParseExpression(exp); //Console.WriteLine("sql: {0}", str); //RepositoryTest.MainTest(); //var securityToken = ApplicationHelper.ApplicationName + "_test_123"; //var code123 = TotpHelper.GenerateCode(securityToken); //Console.WriteLine(code123); //var result = TotpHelper.ValidateCode(securityToken, code123); //Console.WriteLine($"validate result: {result}"); //var ttl = 2; //while (ttl > 1) //{ // ttl = TotpHelper.TTL(securityToken); // Console.WriteLine($"Current ttl: {ttl}, newId: {ObjectIdGenerator.Instance.NewId()}"); // Thread.Sleep(1000); //} //result = TotpHelper.ValidateCode(securityToken, code123); //Console.WriteLine($"validate result1: {result}"); //result = TotpHelper.ValidateCode(securityToken, code123, 60); //Console.WriteLine($"validate result2: {result}"); //var code1234 = TotpHelper.GenerateCode(ApplicationHelper.ApplicationName + "test_1234"); //Console.WriteLine(code1234); // InvokeHelper.TryInvoke(HttpRequesterTest.MainTest); //var pagedListModel = new PagedListModel<int>() //{ // PageNumber = 2, PageSize = 2, TotalCount = 6, Data = new int[] {1, 2}, //}; //var pagedListModel1 = new PagedListModel1<int>() //{ // PageNumber = 2, // PageSize = 2, // TotalCount = 6, // Data = new int[] { 1, 2 }, //}; //Console.WriteLine($"pagedListModel:{JsonConvert.SerializeObject(pagedListModel)}, pagedListModel1:{JsonConvert.SerializeObject(pagedListModel1)}"); //var posts = new[] { new { PostId = 1, PostTitle = "12333", }, new { PostId = 2, PostTitle = "12333", }, }; //var postTags = new[] { new { PostId = 1, Tag = "HHH" } }; //var result = posts.LeftJoin(postTags, p => p.PostId, pt => pt.PostId, (p, pt) => new { p.PostId, p.PostTitle, pt?.Tag }).ToArray(); //Console.WriteLine(result.ToJson()); // CronHelperTest.MainTest(); // DependencyInjectionTest.BuiltInIocTest(); //Expression<Func<TestEntity, bool>> expression = // t => t.Id > 0 && t.CreatedTime < DateTime.Now && t.Token == null; //var visitor = new SqlExpressionVisitor(null); //visitor.Visit(expression); //Console.WriteLine(visitor.GetCondition()); //PipelineTest.TestV2(); //PipelineTest.AsyncPipelineBuilderTestV2().Wait(); Console.ReadLine(); }
//Remove a listener from the subscribed to event. public bool DetachListener(IEventListener listener, string eventName, DelegateEventHandler handler) { if (!m_listenerTable.ContainsKey(eventName)) return false; ArrayList listenerList = m_listenerTable[eventName] as ArrayList; if (!listenerList.Contains(handler)) return false; listenerList.Remove(handler); return true; }
internal void AddToEventQueue(DelegateEventHandler handler) { if (handler == null) { Debug.Log("AddToEventQueue - handler is null."); return; } lock (event_lock_) { event_queue_.Enqueue(handler); } }
public static void Main(string[] args) { Console.WriteLine("----------DotNetCoreSample----------"); // var dataLogger = LogHelper.GetLogger(typeof(DataExtension)); // DataExtension.CommandLogAction = msg => dataLogger.Debug(msg); var services = new ServiceCollection(); services.AddTransient <IFly, MonkeyKing>(); IConfiguration configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .Build(); var city = configuration.GetAppSetting("City"); var number = configuration.GetAppSetting <int>("Number"); Console.WriteLine($"City:{city}, Number:{number}"); services.AddSingleton(configuration); services.AddSingleton <IEventStore, EventStoreInMemory>(); services.AddSingleton <IEventBus, EventBus>(); services.AddSingleton(DelegateEventHandler.FromAction <CounterEvent>(@event => LogHelper.GetLogger(typeof(DelegateEventHandler <CounterEvent>)) .Info($"Event Info: {@event.ToJson()}") ) ); DependencyResolver.SetDependencyResolver(services); //DependencyResolver.Current.ResolveRequiredService<IFly>() // .Fly(); //DependencyInjectionTest.Test(); // EventTest.MainTest(); // SerilogTest.MainTest(); //var builder = new ContainerBuilder(); //builder.RegisterType<MonkeyKing>().As<IFly>(); //DependencyResolver.SetDependencyResolver((IServiceProvider)new AutofacDependencyResolver(builder.Build())); //DependencyInjectionTest.Test(); //int a = 1; //Console.WriteLine(JsonConvert.SerializeObject(a));// output 1 //var pagedListModel = new PagedListModel<int>() //{ // PageNumber = 1, // PageSize = 4, // TotalCount = 10, // Data = new[] { 1, 2, 3, 4 } //}; //Console.WriteLine(pagedListModel.ToJson()); // log test // LoggerTest.MainTest(); // Log4NetTest.MainTest(); ILoggerFactory loggerFactory = new LoggerFactory(); //loggerFactory.AddConsole(); //loggerFactory.AddDebug(); loggerFactory.AddDelegateLogger( (category, logLevel, exception, msg) => { Console.WriteLine($"{category}:[{logLevel}] {msg}\n{exception}"); } ); var logger = new Logger <Program>(loggerFactory); logger.LogInformation("Logging information from Microsoft.Extensions.Logging"); InvokeHelper.TryInvoke(DataExtensionTest.MainTest); //TaskTest.TaskWhenAllTest().GetAwaiter().GetResult(); //Base64UrlEncodeTest.MainTest(); //var a = new { Name = "2123" }; //var name = a.GetProperty("Name").GetValueGetter().Invoke(a); //Console.WriteLine(name); //var structTest = new TestStruct() { Name = "1233" }; //var obj = (object)structTest; //Console.WriteLine(structTest.GetProperty("Name").GetValueGetter<TestStruct>().Invoke(structTest)); //structTest.GetProperty("Name").GetValueSetter().Invoke(obj, "Name1"); //structTest = (TestStruct)obj; //Console.WriteLine(structTest.Name); //Expression<Func<TestEntity, bool>> exp = t => t.Id > 10 && t.Token == "123" && t.Token.Contains("12"); //var str = SqlExpressionParser.ParseExpression(exp); //Console.WriteLine("sql: {0}", str); //exp = t => true; //str = SqlExpressionParser.ParseExpression(exp); //Console.WriteLine("sql: {0}", str); //RepositoryTest.MainTest(); //var securityToken = ApplicationHelper.ApplicationName + "_test_123"; //var code123 = TotpHelper.GenerateCode(securityToken); //Console.WriteLine(code123); //var result = TotpHelper.ValidateCode(securityToken, code123); //Console.WriteLine($"validate result: {result}"); //var ttl = 2; //while (ttl > 1) //{ // ttl = TotpHelper.TTL(securityToken); // Console.WriteLine($"Current ttl: {ttl}, newId: {ObjectIdGenerator.Instance.NewId()}"); // Thread.Sleep(1000); //} //result = TotpHelper.ValidateCode(securityToken, code123); //Console.WriteLine($"validate result1: {result}"); //result = TotpHelper.ValidateCode(securityToken, code123, 60); //Console.WriteLine($"validate result2: {result}"); //var code1234 = TotpHelper.GenerateCode(ApplicationHelper.ApplicationName + "test_1234"); //Console.WriteLine(code1234); // InvokeHelper.TryInvoke(HttpRequesterTest.MainTest); //var pagedListModel = new PagedListModel<int>() //{ // PageNumber = 2, PageSize = 2, TotalCount = 6, Data = new int[] {1, 2}, //}; //var pagedListModel1 = new PagedListModel1<int>() //{ // PageNumber = 2, // PageSize = 2, // TotalCount = 6, // Data = new int[] { 1, 2 }, //}; //Console.WriteLine($"pagedListModel:{JsonConvert.SerializeObject(pagedListModel)}, pagedListModel1:{JsonConvert.SerializeObject(pagedListModel1)}"); //var posts = new[] { new { PostId = 1, PostTitle = "12333", }, new { PostId = 2, PostTitle = "12333", }, }; //var postTags = new[] { new { PostId = 1, Tag = "HHH" } }; //var result = posts.LeftJoin(postTags, p => p.PostId, pt => pt.PostId, (p, pt) => new { p.PostId, p.PostTitle, pt?.Tag }).ToArray(); //Console.WriteLine(result.ToJson()); // CronHelperTest.MainTest(); // DependencyInjectionTest.BuiltInIocTest(); Expression <Func <TestEntity, bool> > expression = t => t.Id > 0 && t.CreatedTime < DateTime.Now && t.Token == null; var visitor = new SqlExpressionVisitor(null); visitor.Visit(expression); Console.WriteLine(visitor.GetCondition()); Console.ReadLine(); }
public static void EventHandlerExceptions() { var queue = new ManualEventQueue(); queue.Subscribers.Add( DelegateEventHandler.On <TestEvent>( e => throw new ArgumentOutOfRangeException()), weakRef: false); queue.Subscribers.Add( DelegateEventHandler.On <TestEvent>( e => throw new MissingMemberException()), weakRef: false); bool argumentExceptionFound = false; bool memberExceptionFound = false; bool unhandledExceptionEventHandled = false; queue.Subscribers.Add( DelegateEventHandler.On <UnhandledExceptionEvent>( e => { unhandledExceptionEventHandled = true; if (e.Type.IndexOf(nameof(ArgumentOutOfRangeException), StringComparison.Ordinal) != -1) { argumentExceptionFound = true; } else if (e.Type.IndexOf(nameof(MissingMemberException), StringComparison.Ordinal) != -1) { memberExceptionFound = true; } }), weakRef: false); // unhandled exception events are generated by default queue.Enqueue(new TestEvent()); Assert.False(argumentExceptionFound); Assert.False(memberExceptionFound); Assert.False(unhandledExceptionEventHandled); Assert.True(queue.HandleNext()); // test event Assert.True(queue.HandleNext()); // argument exception Assert.True(queue.HandleNext()); // missing member exception Assert.True(argumentExceptionFound); Assert.True(memberExceptionFound); Assert.True(unhandledExceptionEventHandled); // unhandled exception event suppression queue.Enqueue(new TestEvent()); argumentExceptionFound = false; memberExceptionFound = false; unhandledExceptionEventHandled = false; queue.RaiseUnhandledEvents.Suspend(); Assert.True(queue.HandleNext()); Assert.False(queue.HandleNext()); queue.RaiseUnhandledEvents.Resume(); Assert.False(argumentExceptionFound); Assert.False(memberExceptionFound); Assert.False(unhandledExceptionEventHandled); // event addition suppression queue.Enqueue(new TestEvent()); queue.EventAdding.Suspend(); Assert.True(queue.HandleNext()); Assert.False(queue.HandleNext()); queue.EventAdding.Resume(); Assert.False(argumentExceptionFound); Assert.False(memberExceptionFound); Assert.False(unhandledExceptionEventHandled); }