예제 #1
0
        public void Prepare(string[] args, PluginHost pluginHost = null)
        {
            _appBaseDir  = AppContext.BaseDirectory;
            _procModFile = Process.GetCurrentProcess().MainModule.FileName;
            _procModDir  = Path.GetDirectoryName(_procModFile);

            var logFilters = LoggingFilters ?? DefaultLoggingFilters;

            var seriLogger = LoggingUtil.ConfigureSerilog(_procModDir).CreateLogger();

            LoggerFactory.AttachSerilog(seriLogger, filters: logFilters);
            _log = LoggerFactory.CreateLogger <TFPluginServer>();
            _log.LogDebug("Logging initialized");
            _log.LogInformation("***********************************************************************************");
            _log.LogInformation("** Plugin Server Preparing...");
            _log.LogInformation("***********************************************************************************");

            //DumpEnv();

            _proto = new TFPluginProtocol();
            if (!_proto.PrepareHandshake())
            {
                if (!_proto.MagicCookieFound)
                {
                    Console.Error.WriteLine(BadInvokeMessage);
                    Environment.Exit(-1);
                    return;
                }
                if (!_proto.ProtocolVersionMatched)
                {
                    throw new Exception("Protocol version mistmatch");
                }
            }

            ResolveListenEndpoint();

            _pluginHost                      = pluginHost ?? NewPluginHost();
            _pluginHost.PKI                  = SimplePKIDetails.GenerateRSA();
            _pluginHost.ListenHost           = _listenHost;
            _pluginHost.ListenPort           = _listenPort;
            _pluginHost.AppProtocolVersion   = _proto.AppProtocolVersion;
            _pluginHost.LoggingBuilderAction = lb =>
            {
                lb.ClearProviders();
                lb.AttachSerilog(seriLogger, filters: logFilters);
            };

            _pluginHost.PrepareHost(args);
            _pluginHost.AddServices(services =>
            {
                services.AddTransient(typeof(Util.Fallback <>));

                services.AddSingleton(this);
                services.AddSingleton <ISchemaResolver, Services.SchemaResolver>();
                services.AddSingleton <Services.PluginService>();
            });
            _pluginHost.BuildHostApp();
            _pluginHost.MapGrpcService <Services.PluginService>();
        }