Ejemplo n.º 1
0
        /// <summary>
        ///     配置使用MessageMVC
        /// </summary>
        /// <param name="builder">主机生成器</param>
        /// <param name="registAction">配置注册方法</param>
        /// <param name="autoDiscovery">自动发现</param>
        /// <param name="discovery">自定义API发现方法</param>
        internal static void UseMessageMVC(this IWebHostBuilder builder, Action <IServiceCollection> registAction, bool autoDiscovery, Action discovery)
        {
            Console.Write(@"-------------------------------------------------------------
---------------> ");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(@"Wecome ZeroTeam MessageMVC");
            Console.ResetColor();
            Console.WriteLine(@" <----------------
-------------------------------------------------------------");
            builder.ConfigureAppConfiguration((ctx, builder) =>
            {
                DependencyHelper.ServiceCollection.AddSingleton(p => builder);
                ConfigurationHelper.BindBuilder(builder);
                ZeroFlowControl.LoadConfig();

                ZeroAppOption.Instance.AutoDiscover       = autoDiscovery;
                ZeroAppOption.Instance.Discovery          = discovery;
                ConfigurationHelper.OnConfigurationUpdate = cfg => ctx.Configuration = cfg;
                ctx.Configuration = ConfigurationHelper.Root;
            })
            .ConfigureServices((ctx, services) =>
            {
                DependencyHelper.Binding(services);
                services.AddHostedService <ZeroHostedService>();
                registAction(services);
                ZeroApp.AddDependency(services);
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     调用
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        static async Task Call(HttpContext context)
        {
            if (string.Equals(context.Request.Method, "OPTIONS", StringComparison.OrdinalIgnoreCase))
            {
                //HttpProtocol.CrosOption(context.Response);
                return;
            }
            if (context.Request.Path == "/")
            {
                await context.Response.WriteAsync("Wecome MessageMVC,Lucky every day!", Encoding.UTF8);

                return;
            }
            if (!ZeroAppOption.Instance.IsRuning)
            {
                await context.Response.WriteAsync(ApiResultHelper.PauseJson, Encoding.UTF8);

                return;
            }
            //HttpProtocol.CrosCall(context.Response);
            try
            {
                //命令
                var reader = new HttpMessageReader();
                var(success, message) = await reader.CheckRequest(context);

                //开始调用
                if (success)
                {
                    var service = ZeroFlowControl.GetService(message.Service) ?? new ZeroService
                    {
                        ServiceName = message.Service,
                        Receiver    = new EmptyReceiver(),
                        Serialize   = DependencyHelper.GetService <ISerializeProxy>()
                    };
                    await MessageProcessor.OnMessagePush(service, message, false, new HttpWriter
                    {
                        Context = context
                    });
                }
                else
                {
                    await context.Response.WriteAsync(ApiResultHelper.NotSupportJson, Encoding.UTF8);
                }
            }
            catch (Exception e)
            {
                ScopeRuner.ScopeLogger.Exception(e);
                try
                {
                    await context.Response.WriteAsync(ApiResultHelper.BusinessErrorJson, Encoding.UTF8);
                }
                catch (Exception exception)
                {
                    ScopeRuner.ScopeLogger.Exception(exception);
                }
            }
        }
Ejemplo n.º 3
0
        static void RetistTest()
        {
            var service = new ZeroService
            {
                IsAutoService = true,
                ServiceName   = "MyTest",
                Receiver      = new EmptyReceiver(),
                Serialize     = new NewtonJsonSerializeProxy()
            } as IService;

            service.RegistWildcardAction(new ApiActionInfo
            {
                Name              = "*",
                Route             = "*",
                ControllerName    = "DataEventProxy",
                ControllerCaption = "DataEventProxy",
                AccessOption      = ApiOption.Public | ApiOption.Anymouse,
                ResultType        = typeof(Task),
                IsAsync           = true,
                Action            = (msg, seri, arg) => null
            });
            ZeroFlowControl.RegistService(service);
        }
Ejemplo n.º 4
0
 void IDisposable.Dispose()
 {
     ZeroFlowControl.Shutdown();
 }
Ejemplo n.º 5
0
 public void TearDown()
 {
     ZeroFlowControl.Shutdown();
 }
Ejemplo n.º 6
0
        private void RegistToZero()
        {
            foreach (var serviceInfo in ServiceInfos.Values)
            {
                if (serviceInfo.Aips.Count == 0)
                {
                    continue;
                }
                logger.Debug(() => $"【注册API】 {serviceInfo.Name}");

                var service = ZeroFlowControl.TryGetZeroObject(serviceInfo.Name);
                if (service == null)
                {
                    var receiver = serviceInfo.NetBuilder(serviceInfo.Name);
                    if (receiver == null || receiver is EmptyService)
                    {
                        service = new EmptyService
                        {
                            IsAutoService = true,
                            ServiceName   = serviceInfo.Name,
                            Serialize     = serviceInfo.Serialize switch
                            {
                                SerializeType.Json => DependencyHelper.GetService <IJsonSerializeProxy>(),
                                SerializeType.NewtonJson => new NewtonJsonSerializeProxy(),
                                SerializeType.Xml => DependencyHelper.GetService <IXmlSerializeProxy>(),
                                SerializeType.Bson => DependencyHelper.GetService <IBsonSerializeProxy>(),
                                _ => DependencyHelper.GetService <IJsonSerializeProxy>(),
                            }
                        }
                    }
                    ;
                    else
                    {
                        service = new ZeroService
                        {
                            IsAutoService = true,
                            ServiceName   = serviceInfo.Name,
                            Receiver      = receiver,
                            Serialize     = serviceInfo.Serialize switch
                            {
                                SerializeType.Json => DependencyHelper.GetService <IJsonSerializeProxy>(),
                                SerializeType.NewtonJson => new NewtonJsonSerializeProxy(),
                                SerializeType.Xml => DependencyHelper.GetService <IXmlSerializeProxy>(),
                                SerializeType.Bson => DependencyHelper.GetService <IBsonSerializeProxy>(),
                                _ => DependencyHelper.GetService <IJsonSerializeProxy>(),
                            }
                        }
                    };

                    ZeroFlowControl.RegistService(ref service);
                }
                foreach (var api in serviceInfo.Aips)
                {
                    try
                    {
                        var info = (ApiActionInfo)api.Value;
                        if (api.Key == "*")
                        {
                            logger.Debug(() => $"[注册接口] {serviceInfo.Name}/* => {info.Caption} {info.ControllerName}.{info.Name}");
                            service.RegistWildcardAction(info);
                        }
                        else if (!service.RegistAction(api.Key, info))
                        {
                            logger.Error($"[注册接口]失败,因为路由名称已存在 {serviceInfo.Name}/{api.Key} => {info.Caption} {info.ControllerName}.{info.Name}");
                        }
                        else
                        {
                            logger.Debug(() => $"[注册接口] {serviceInfo.Name}/{api.Key} => {info.Caption} {info.ControllerName}.{info.Name}");
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Exception(ex, api.Key);
                    }
                }
            }