Beispiel #1
0
        public void TestNetHelper()
        {
            NetHelper.IsLoopBack("localhost").ShouldBe(true);
            NetHelper.IsLoopBack("127.0.0.1").ShouldBe(true);
            NetHelper.IsLoopBack("::1").ShouldBe(true);
            NetHelper.IsLoopBack("+").ShouldBe(true);

            NetHelper.ChangeToExternal("localhost").ShouldNotBe("127.0.0.1");
        }
Beispiel #2
0
        //public static IApplicationBuilder UseCobMvc<T>(this IApplicationBuilder mvcBuilder, Action<CobMvcStartupOptions> optionSetup = null)
        //{
        //    return mvcBuilder.UseCobMvc(opt=>
        //    {
        //        opt.ServiceName = typeof(T).Assembly.GetName().Name;
        //        optionSetup?.Invoke(opt);
        //    });
        //}

        /// <summary>
        /// 启用cobmvc服务注册,放置在UseMvc()之前
        /// </summary>
        /// <param name="mvcBuilder"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseCobMvc(this IApplicationBuilder mvcBuilder, Action <CobMvcOptions> optionsSetup = null)//
        {
            var options = mvcBuilder.ApplicationServices.GetService <IOptions <CobMvcOptions> >().Value;

            optionsSetup?.Invoke(options);

            if (string.IsNullOrWhiteSpace(options.ServiceAddress))
            {
                var addr = mvcBuilder.ServerFeatures.Get <IServerAddressesFeature>();
                if (addr.Addresses.Any())
                {
                    options.ServiceAddress = addr.Addresses.First();
                    Console.WriteLine($"use addr:{options.ServiceAddress} from server feature");
                    if (!options.EnableDevMode)
                    {
                        options.ServiceAddress = NetHelper.ChangeToExternal(options.ServiceAddress);
                        Console.WriteLine($"convert addr to external: {options.ServiceAddress}");
                    }
                }
            }

            if (string.IsNullOrWhiteSpace(options.ServiceAddress))
            {
                options.ServiceAddress = "http://localhost:5000";
            }

            //如果没有设置服务名称,则使用程序集作为服务名
            if (string.IsNullOrWhiteSpace(options.ServiceName))
            {
                options.ServiceName = Assembly.GetEntryAssembly().GetName().Name;
            }

            var uri = new Uri(options.ServiceAddress);

            var svcInfo = new ServiceInfo
            {
                Address = uri.ToString(),
                Name    = options.ServiceName,
                ID      = StringHelper.ToMD5(options.ServiceAddress + options.ServiceName),//
                //Port = uri.Port
            };

            if (!string.IsNullOrWhiteSpace(options.HealthCheck))
            {
                svcInfo.CheckInfoes = new[] {
                    new ServiceCheckInfo {
                        Type     = ServiceCheckInfoType.Http,
                        Target   = new Uri(UriHelper.Combine(options.ServiceAddress, options.HealthCheck)),
                        Interval = TimeSpan.FromSeconds(3),
                        Timeout  = TimeSpan.FromSeconds(60)
                    }
                };
            }

            var logger = mvcBuilder.ApplicationServices.GetRequiredService <ILoggerFactory>().CreateLogger("CobMvcExtensions");

            logger.LogDebug("register service:{0}\t{1}", svcInfo.Name, svcInfo.Address);

            var reg = mvcBuilder.ApplicationServices.GetRequiredService <IServiceRegistration>();

            reg.Register(svcInfo);

            mvcBuilder.UseMiddleware <CobMvcContextMiddleware>();
            //mvcBuilder.UseMiddleware<CobMvcParametersBinder>();

            //todo:deregister

            return(mvcBuilder);
        }