public static IServiceCollection AddRuntime(this IServiceCollection services) { services.AddSingleton(typeof(CPlatformContainer), new CPlatformContainer(ServiceLocator.Current)); //注册服务token生成接口 ServiceTokenGenerator serviceTokenGenerator = new ServiceTokenGenerator(); serviceTokenGenerator.GeneratorToken("True"); services.AddSingleton(typeof(IServiceTokenGenerator), serviceTokenGenerator); //注册服务器路由接口 services.AddSingleton <IServiceRouteProvider, DefaultServiceRouteProvider>(); //注册服务ID生成实例 services.AddSingleton <IServiceIdGenerator, DefaultServiceIdGenerator>(); services.AddSingleton <ITypeConvertibleService, DefaultTypeConvertibleService>(); services.AddSingleton <IValidationProcessor, DefaultValidationProcessor>(); services.AddSingleton <IClrServiceEntryFactory, ClrServiceEntryFactory>(); var provider = services.BuildServiceProvider(); services.AddSingleton <IClrServiceEntryFactory, ClrServiceEntryFactory>(); var assemblys = GetReferenceAssembly(); var types = assemblys.SelectMany(i => i.ExportedTypes).ToArray(); var attributeServiceEntryProvider = new AttributeServiceEntryProvider(types, provider.GetService <IClrServiceEntryFactory>()); services.AddSingleton(typeof(IServiceEntryProvider), attributeServiceEntryProvider); services.AddSingleton <IServiceEntryManager, DefaultServiceEntryManager>(); services.AddSingleton <IServiceEntryLocate, DefaultServiceEntryLocate>(); services.AddSingleton <IAuthorizationFilter, AuthorizationAttribute>(); services.AddSingleton <IFilter, AuthorizationAttribute>(); services.AddSingleton <IServiceExecutor, HttpExecutor>(); services.AddSingleton <IMessageListener, HttpMessageListener>(); return(services); }
private static void Main() { //相关服务初始化。 ISerializer <string> serializer = new JsonSerializer(); ISerializer <byte[]> byteArraySerializer = new StringByteArraySerializer(serializer); ISerializer <object> objectSerializer = new StringObjectSerializer(serializer); IServiceIdGenerator serviceIdGenerator = new DefaultServiceIdGenerator(new ConsoleLogger <DefaultServiceIdGenerator>()); IServiceInstanceFactory serviceInstanceFactory = new DefaultServiceInstanceFactory(new ConsoleLogger <DefaultServiceInstanceFactory>()); ITypeConvertibleService typeConvertibleService = new DefaultTypeConvertibleService(new[] { new DefaultTypeConvertibleProvider(objectSerializer) }, new NullLogger <DefaultTypeConvertibleService>()); IClrServiceEntryFactory clrServiceEntryFactory = new ClrServiceEntryFactory(serviceInstanceFactory, serviceIdGenerator, typeConvertibleService); var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetExportedTypes()); var serviceEntryProvider = new AttributeServiceEntryProvider(types, clrServiceEntryFactory, new ConsoleLogger <AttributeServiceEntryProvider>()); IServiceEntryManager serviceEntryManager = new DefaultServiceEntryManager(new IServiceEntryProvider[] { serviceEntryProvider }); IServiceEntryLocate serviceEntryLocate = new DefaultServiceEntryLocate(serviceEntryManager); //自动生成服务路由(这边的文件与Echo.Client为强制约束) { var addressDescriptors = serviceEntryManager.GetEntries().Select(i => new ServiceRoute { Address = new[] { new IpAddressModel { Ip = "127.0.0.1", Port = 9981 } }, ServiceDescriptor = i.Descriptor }); var serviceRouteManager = new SharedFileServiceRouteManager("d:\\routes.txt", serializer, new ConsoleLogger <SharedFileServiceRouteManager>()); //zookeeper服务路由管理者。 // var serviceRouteManager = new ZooKeeperServiceRouteManager(new ZooKeeperServiceRouteManager.ZookeeperConfigInfo("172.18.20.132:2181"), serializer, new ConsoleLogger<ZooKeeperServiceRouteManager>()); serviceRouteManager.AddRoutesAsync(addressDescriptors).Wait(); } IServiceHost serviceHost = new DotNettyServiceHost(new DefaultServiceExecutor(serviceEntryLocate, byteArraySerializer, new ConsoleLogger <DefaultServiceExecutor>()), new ConsoleLogger <DotNettyServiceHost>(), byteArraySerializer); Task.Factory.StartNew(async() => { //启动主机 await serviceHost.StartAsync(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9981)); Console.WriteLine($"服务端启动成功,{DateTime.Now}。"); }); Console.ReadLine(); }