Beispiel #1
0
        internal override async Task Excute(HttpContext ctx, ILifetimeScope scope)
        {
            OxygenIocContainer.BuilderIocContainer(scope);//仅在当前请求内创建上下文模型
            byte[] result = new byte[0];
            ctx.Response.ContentType = "application/json";
            var messageType = MessageType.Json;

            try
            {
                if (ctx.Request.ContentType == "application/x-msgpack")
                {
                    ctx.Response.ContentType = "application/x-msgpack";
                    messageType = MessageType.MessagePack;
                }
                if (ctx.Request.ContentType == null)
                {
                    ctx.Response.ContentType = "text/html";
                    messageType = MessageType.Html;
                }
                HttpContextExtension.ContextWapper.Value = new OxygenHttpContextWapper(Path, scope, ctx.Request.Headers.GetHeaderDictionary(), ctx.Request.Cookies.GetCookieDictionary(), ctx.Response);
                Tout localCallbackResult = null;
                if (noInput)
                {
                    localCallbackResult = await LocalMethodAopProvider.UsePipelineHandler(scope, HttpContextExtension.ContextWapper.Value, NoInputMethodDelegate);
                }
                else
                {
                    var messageobj = await messageHandler.ParseMessage <Tin>(ctx, messageType);

                    if (messageobj == default(Tin))
                    {
                        throw new FormatException($"参数反序列化失败,接口地址{Path},入参类型:{typeof(Tin).Name}");
                    }
                    localCallbackResult = await LocalMethodAopProvider.UsePipelineHandler(scope, messageobj, HttpContextExtension.ContextWapper.Value, MethodDelegate);
                }
                if (localCallbackResult != null)
                {
                    result = messageHandler.BuildMessage(localCallbackResult, messageType);
                }
            }
            catch (Exception e)
            {
                logger.LogError($"服务端消息处理异常: {e.GetBaseException()?.Message ?? e.Message}");
                ctx.Response.StatusCode = 502;
                if (e is FormatException)
                {
                    await ctx.Response.Body.WriteAsync(Encoding.UTF8.GetBytes(e.Message));
                }
            }
            finally
            {
                await ctx.Response.Body.WriteAsync(result, 0, result.Length);

                OxygenIocContainer.DisposeIocContainer();//注销上下文
            }
        }
Beispiel #2
0
 static IHostBuilder CreateDefaultHost(string[] args) => new HostBuilder()
 .ConfigureWebHostDefaults(webhostbuilder => {
     //注册成为oxygen服务节点
     webhostbuilder.StartOxygenServer <OxygenActorStartup>((config) => {
         config.Port = 80;
         config.PubSubCompentName     = "pubsub";
         config.StateStoreCompentName = "statestore";
         config.TracingHeaders        = "Authentication";
     });
 })
 .ConfigureContainer <ContainerBuilder>(builder =>
 {
     //注入oxygen依赖
     builder.RegisterOxygenModule();
     //注入测试demo
     builder.RegisterType <HelloServiceImpl>().As <IHelloService>().InstancePerLifetimeScope();
     builder.RegisterType <HelloEventHandler>().As <HelloEventHandler>().InstancePerLifetimeScope();
 })
 .ConfigureLogging((hostingContext, logging) => {
     logging.AddConsole();
 })
 .ConfigureServices((context, services) =>
 {
     //注册全局拦截器
     LocalMethodAopProvider.RegisterPipelineHandler((methodctx) => {
         HttpContextCurrent.SetCurrent(methodctx);
     }, async(obj, methodctx) =>
     {
         if (obj != null)
         {
             Console.WriteLine($"这里是方法前拦截器,拦截到参数:{JsonSerializer.Serialize(obj)}");
         }
         await Task.CompletedTask;
         var s = HttpContextCurrent.GetCurrent();
     }, async(result) =>
     {
         Console.WriteLine($"这里是方法后拦截器,拦截到方法结果:{JsonSerializer.Serialize(result)}");
         var s = HttpContextCurrent.GetCurrent();
         await Task.CompletedTask;
     }, async(exp) =>
     {
         Console.WriteLine($"这里是方法异常拦截器,拦截到异常:{exp.Message}");
         return(await Task.FromResult(new { Message = exp.Message }));
     });
     services.AddAutofac();
 })
 .UseServiceProviderFactory(new AutofacServiceProviderFactory());
 static IHostBuilder CreateDefaultHost(string[] _) => new HostBuilder()
 .ConfigureWebHostDefaults(webhostbuilder => {
     //注册成为oxygen服务节点
     webhostbuilder.StartOxygenServer <OxygenActorStartup>((config) => {
         config.Port = 80;
         config.PubSubCompentName     = "pubsub";
         config.StateStoreCompentName = "statestore";
         config.TracingHeaders        = "Authentication,AuthIgnore";
         config.UseCors = true;
     });
 })
 .ConfigureAppConfiguration((hostContext, config) =>
 {
     config.SetBasePath(Directory.GetCurrentDirectory());
     config.AddJsonFile("appsettings.json");
     Configuration = config.Build();
 })
 .ConfigureContainer <ContainerBuilder>(builder =>
 {
     //注入oxygen依赖
     builder.RegisterOxygenModule();
     //注入业务依赖
     builder.RegisterModule(new ServiceModule());
 })
 .ConfigureServices((context, services) =>
 {
     services.AddHttpClient();
     //注册自定义HostService
     services.AddHostedService <CustomerService>();
     //注册全局拦截器
     LocalMethodAopProvider.RegisterPipelineHandler(AopHandlerProvider.ContextHandler, AopHandlerProvider.BeforeSendHandler, AopHandlerProvider.AfterMethodInvkeHandler, AopHandlerProvider.ExceptionHandler);
     //注册鉴权拦截器
     AccountAuthenticationHandler.RegisterAllFilter();
     services.AddLogging(configure =>
     {
         configure.AddConfiguration(Configuration.GetSection("Logging"));
         configure.AddConsole();
     });
     services.AddDbContext <EfDbContext>(options => options.UseNpgsql(Configuration.GetSection("SqlConnectionString").Value));
     services.AddAutofac();
 })
 .UseServiceProviderFactory(new AutofacServiceProviderFactory());
Beispiel #4
0
 static IHostBuilder CreateDefaultHost(string[] args) => new HostBuilder()
 .ConfigureWebHostDefaults(webhostbuilder => {
     //×¢²á³ÉΪoxygen·þÎñ½Úµã
     webhostbuilder.StartOxygenServer <OxygenActorStartup>((config) => {
         config.Port = 80;
         config.PubSubCompentName     = "pubsub";
         config.StateStoreCompentName = "statestore";
         config.TracingHeaders        = "Authentication";
     });
 })
 .ConfigureAppConfiguration((hostContext, config) =>
 {
     config.SetBasePath(Directory.GetCurrentDirectory());
     config.AddJsonFile("appsettings.json");
     _configuration = config.Build();
 })
 .ConfigureContainer <ContainerBuilder>(builder =>
 {
     //×¢ÈëoxygenÒÀÀµ
     builder.RegisterOxygenModule();
     //×¢ÈëÒµÎñÒÀÀµ
     builder.RegisterModule(new ServiceModule());
 })
 .ConfigureServices((context, services) =>
 {
     //×¢²á×Ô¶¨ÒåHostService
     services.AddHostedService <CustomerService>();
     //×¢²áÈ«¾ÖÀ¹½ØÆ÷
     LocalMethodAopProvider.RegisterPipelineHandler(AopHandlerProvider.ContextHandler, AopHandlerProvider.BeforeSendHandler, AopHandlerProvider.AfterMethodInvkeHandler, AopHandlerProvider.ExceptionHandler);
     //×¢²á¼øȨÀ¹½ØÆ÷
     AuthenticationHandler.RegisterAllFilter();
     services.AddLogging(configure =>
     {
         configure.AddConfiguration(_configuration.GetSection("Logging"));
         configure.AddConsole();
     });
     services.AddDbContext <EfDbContext>(options => options.UseNpgsql(_configuration.GetSection("SqlConnectionString").Value));
     services.AddAutofac();
 })
 .UseServiceProviderFactory(new AutofacServiceProviderFactory());
Beispiel #5
0
 static IHostBuilder CreateDefaultHost(string[] args) => new HostBuilder()
 .ConfigureWebHostDefaults(webhostbuilder => {
     //注册成为oxygen服务节点
     webhostbuilder.StartOxygenServer <OxygenStartup>((config) => {
         config.Port = 80;
         config.PubSubCompentName     = "pubsub";
         config.StateStoreCompentName = "statestore";
         config.TracingHeaders        = "Authentication,AuthIgnore";
         config.UseStaticFiles        = true;
         config.UseCors = true;
     });
 })
 .ConfigureAppConfiguration((hostContext, config) =>
 {
     config.SetBasePath(Directory.GetCurrentDirectory());
     config.AddJsonFile("appsettings.json");
     _configuration = config.Build();
 })
 .ConfigureContainer <ContainerBuilder>(builder =>
 {
     //注入oxygen依赖
     builder.RegisterOxygenModule();
     //注入业务依赖
     builder.RegisterModule(new ServiceModule());
     builder.RegisterType <ImageAppService.ImageAppService>().As <IApplicationService.Base.IImageAppService>().InstancePerLifetimeScope();
 })
 .ConfigureServices((context, services) =>
 {
     //注册全局拦截器
     LocalMethodAopProvider.RegisterPipelineHandler(AopHandlerProvider.ContextHandler, AopHandlerProvider.BeforeSendHandler, AopHandlerProvider.AfterMethodInvkeHandler, AopHandlerProvider.ExceptionHandler);
     //注册鉴权拦截器
     ImageAuthenticationHandler.RegisterAllFilter();
     services.AddLogging(configure =>
     {
         configure.AddConfiguration(_configuration.GetSection("Logging"));
         configure.AddConsole();
     });
     services.AddAutofac();
 })
 .UseServiceProviderFactory(new AutofacServiceProviderFactory());