static async Task Main(string[] args) { var host = new ServiceHost(); host.AddJsonConfiguration("Config.json", reloadOnChange: true); host.ConfigureLogging(builder => builder.UserNLog()); host.ConfigureContainer(builder => { builder.Register(c => new LoggerInterceptor(invocation => ( $"{invocation.TargetType.Name}InvokeLog", LogLevel.Debug, "\n========================================\n" + $"Method:\t{invocation.Method}\n" + $"Args:\t{string.Join('|', invocation.Arguments)}\n" + $"Return:\t{invocation.ReturnValue}\n" + "========================================" ))); builder.RegisterType <A>().As <I>().EnableInterfaceInterceptors(); builder.RegisterType <B>().As <I>().EnableInterfaceInterceptors(); }); host.OnHostStarted += provider => { ServiceHost.ParseConfiguration <string>("ConnectionString"); foreach (var item in provider.GetServices <I>()) { item.Do(5, 6); } }; await host.RunAsync(); }
static async Task Main(string[] args) { var host = new ServiceHost(); // 可在运行时修改配置文件 host.AddJsonConfiguration("Config.json", reloadOnChange: true); host.ConfigureSqlSugar(); host.ConfigureLogging(builder => builder.UserNLog()); host.OnHostStarted += p => { var s = p.GetSqlSugar(); var c = s.CreateConnection("Main"); var d = c.DbMaintenance.GetTableInfoList(); }; await host.RunAsync(); }
static async Task Main(string[] args) { var host = new ServiceHost(); var configuration = new ConfigurationBuilder().AddJsonFile("Config.json").Build(); host.ConfigureWeb(configuration, builder => builder.UseStartup <Startup>(), "web"); host.ConfigureSocket(configuration, "tcp"); host.ConfigureSocket(configuration, "udp"); host.ConfigureLogging(builder => builder.UserNLog()); host.OnHostStarted += p => { var web = p.GetWeb(); web.OnWebSocketReceive += (c, a) => Console.WriteLine($"{c.ID} {Encoding.UTF8.GetString(a.Data)}"); web.OnWebSocketClose += (c, a) => Console.WriteLine($"{c.ID} 断开连接"); var tcp = p.GetTcp(); tcp.OnConnect += (c, a) => Console.WriteLine($"{c.ID} connected"); tcp.OnReceive += (c, a) => { Console.WriteLine($"{c.ID} received {Encoding.UTF8.GetString(a.Data)}"); var body = @"<h1>Hello World</h1><span>XXXXXXX</span>"; _ = c.SendAsync(Encoding.UTF8.GetBytes( "HTTP/1.1 200 OK\r\n" + "Date: Sat, 31 Dec 2005 23:59:59 GMT\r\n" + "Content-Type: text/html;charset=UTF8\r\n" + $"Content-Length: {Encoding.UTF8.GetByteCount(body)}\r\n\n" + $"{body}" )); }; tcp.OnSend += (c, a) => Console.WriteLine($"{c.ID} sent {Encoding.UTF8.GetString(a.Data)}"); tcp.OnClose += (c, a) => Console.WriteLine($"{c.ID} closed"); var udp = p.GetUdp(); udp.OnReceive += (c, a) => { Console.WriteLine($"{c} received {Encoding.UTF8.GetString(a.Data)}"); udp.SendTo(Encoding.UTF8.GetBytes("World"), c); }; udp.OnSend += (c, a) => Console.WriteLine($"{c} sent {Encoding.UTF8.GetString(a.Data)}"); }; await host.RunAsync(); }