using BBWY.Client.Models; using BBWY.Client.ViewModels; using BBWY.Common.Extensions; using BBWY.Common.Http; using BBWY.Common.Models; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; using System.IO; using System.IO.MemoryMappedFiles; using System.Linq; using System.Reflection; using System.Threading.Tasks; using System.Windows; namespace BBWY.Client { /// /// Interaction logic for App.xaml /// public partial class App : Application { public IServiceProvider ServiceProvider { get; private set; } public IConfiguration Configuration { get; private set; } protected override void OnStartup(StartupEventArgs e) { var gl = new GlobalContext(); string userToken = string.Empty; //#if DEBUG userToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxNDA1MTUxNjE5NTk0NTg4MTYwIiwidGVhbUlkIjoiMTQzOTg5OTEyMzk1NTI3MzcyOCIsImV4cCI6MTY3MTkwMTU1NH0.UaUubqP442qxVc6ppQt7FO0jcFs3w6KR6q1OeBuL1i8"; //齐越小一 //#else // var uid = e.Args.Count() > 0 ? e.Args.LastOrDefault(args => args.StartsWith("uid:")) : string.Empty; // if (string.IsNullOrEmpty(uid)) // { // MessageBox.Show("缺少启动参数", "提示"); // Environment.Exit(0); // } // var tokenResult = ReadMMF(uid); // if (tokenResult.isOk) // userToken = tokenResult.content; // else // { // MessageBox.Show($"读取内存数据失败\r\n{tokenResult.content}", "提示"); // Environment.Exit(0); // } //#endif gl.UserToken = userToken; #region 注册全局异常 this.DispatcherUnhandledException += App_DispatcherUnhandledException; ; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; ; TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; ; #endregion var applicationPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var builder = new ConfigurationBuilder().SetBasePath(applicationPath).AddJsonFile("BBWYAppSettings.json", false, true); Configuration = builder.Build(); gl.BBYWApiHost = Configuration.GetSection("BBWYApiHost").Value; gl.MDSApiHost = Configuration.GetSection("MDSApiHost").Value; gl.JOSApiHost = Configuration.GetSection("JOSApiHost").Value; gl._1688ApiHost = Configuration.GetSection("1688ApiHost").Value; IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddHttpClient(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(gl); serviceCollection.BatchRegisterServices(new Assembly[] { Assembly.Load("BBWY.Client") }, typeof(IDenpendency)); serviceCollection.AddMapper(new MappingProfile()); ServiceProvider = serviceCollection.BuildServiceProvider(); base.OnStartup(e); } private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) { Console.WriteLine(e.Exception); e.SetObserved(); } private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { throw new NotImplementedException(); } private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { Console.WriteLine(e.Exception); } public (bool isOk, string content) ReadMMF(string mapname) { try { using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting(mapname)) { using (var mmfStream = mmf.CreateViewStream(0, 1000, MemoryMappedFileAccess.ReadWrite)) { StreamReader reader = new StreamReader(mmfStream); string jwt = reader.ReadToEnd().Replace("\0", "").TrimEnd(); return (true, jwt); } } } catch (Exception ex) { return (false, ex.Message); } } } }