diff --git a/src/c#/GeneralUpdate.Client/MySample.cs b/src/c#/GeneralUpdate.Client/MySample.cs index 26f4fde71c15baef5b22622faa4377729d57c449..d45be1740944a788d8c4148a76ddb8fd6c6a4afd 100644 --- a/src/c#/GeneralUpdate.Client/MySample.cs +++ b/src/c#/GeneralUpdate.Client/MySample.cs @@ -39,7 +39,7 @@ namespace GeneralUpdate.Client #region 常规更新 - public void Upgrade() + public async Task Upgrade() { //Task.Run(async () => //{ @@ -51,42 +51,39 @@ namespace GeneralUpdate.Client // await GeneralClientOSS.Start(@params); //}); - Task.Run(async () => - { - //ClientStrategy该更新策略将完成1.自动升级组件自更新 2.启动更新组件 3.配置好ClientParameter无需再像之前的版本写args数组进程通讯了。 - //generalClientBootstrap.Config(baseUrl, "B8A7FADD-386C-46B0-B283-C9F963420C7C"). - var configinfo = GetWindowsConfigInfo(); - var generalClientBootstrap = await new GeneralClientBootstrap() - //单个或多个更新包下载通知事件 - .AddListenerMultiDownloadProgress(OnMultiDownloadProgressChanged) - //单个或多个更新包下载速度、剩余下载事件、当前下载版本信息通知事件 - .AddListenerMultiDownloadStatistics(OnMultiDownloadStatistics) - //单个或多个更新包下载完成 - .AddListenerMultiDownloadCompleted(OnMultiDownloadCompleted) - //完成所有的下载任务通知 - .AddListenerMultiAllDownloadCompleted(OnMultiAllDownloadCompleted) - //下载过程出现的异常通知 - .AddListenerMultiDownloadError(OnMultiDownloadError) - //整个更新过程出现的任何问题都会通过这个事件通知 - .AddListenerException(OnException) - .Config(configinfo) - .Option(UpdateOption.DownloadTimeOut, 60) - .Option(UpdateOption.Encoding, Encoding.Default) - .Option(UpdateOption.Format, Format.ZIP) - //开启驱动更新 - .Option(UpdateOption.Drive, true) - //开启遗言功能,需要部署GeneralUpdate.SystemService Windows服务。 - //.Option(UpdateOption.WillMessage, true) - .Strategy() - //注入一个func让用户决定是否跳过本次更新,如果是强制更新则不生效 - //.SetCustomSkipOption(ShowCustomOption) - //注入一个自定义方法集合,该集合会在更新启动前执行。执行自定义方法列表如果出现任何异常,将通过异常订阅通知。(推荐在更新之前检查当前软件环境) - //.AddCustomOption(new List>() { () => Check1(), () => Check2() }) - //默认黑名单文件: { "Newtonsoft.Json.dll" } 默认黑名单文件扩展名: { ".patch", ".7z", ".zip", ".rar", ".tar" , ".json" } - //如果不需要扩展,需要重新传入黑名单集合来覆盖。 - //.SetBlacklist(GetBlackFiles(), GetBlackFormats()) - .LaunchTaskAsync(); - }); + //ClientStrategy该更新策略将完成1.自动升级组件自更新 2.启动更新组件 3.配置好ClientParameter无需再像之前的版本写args数组进程通讯了。 + //generalClientBootstrap.Config(baseUrl, "B8A7FADD-386C-46B0-B283-C9F963420C7C"). + var configinfo = GetWindowsConfigInfo(); + var generalClientBootstrap = await new GeneralClientBootstrap() + //单个或多个更新包下载通知事件 + .AddListenerMultiDownloadProgress(OnMultiDownloadProgressChanged) + //单个或多个更新包下载速度、剩余下载事件、当前下载版本信息通知事件 + .AddListenerMultiDownloadStatistics(OnMultiDownloadStatistics) + //单个或多个更新包下载完成 + .AddListenerMultiDownloadCompleted(OnMultiDownloadCompleted) + //完成所有的下载任务通知 + .AddListenerMultiAllDownloadCompleted(OnMultiAllDownloadCompleted) + //下载过程出现的异常通知 + .AddListenerMultiDownloadError(OnMultiDownloadError) + //整个更新过程出现的任何问题都会通过这个事件通知 + .AddListenerException(OnException) + .Config(configinfo) + .Option(UpdateOption.DownloadTimeOut, 60) + .Option(UpdateOption.Encoding, Encoding.Default) + .Option(UpdateOption.Format, Format.ZIP) + //开启驱动更新 + .Option(UpdateOption.Drive, true) + //开启遗言功能,需要部署GeneralUpdate.SystemService Windows服务。 + .Option(UpdateOption.WillMessage, true) + .Strategy() + //注入一个func让用户决定是否跳过本次更新,如果是强制更新则不生效 + //.SetCustomSkipOption(ShowCustomOption) + //注入一个自定义方法集合,该集合会在更新启动前执行。执行自定义方法列表如果出现任何异常,将通过异常订阅通知。(推荐在更新之前检查当前软件环境) + //.AddCustomOption(new List>() { () => Check1(), () => Check2() }) + //默认黑名单文件: { "Newtonsoft.Json.dll" } 默认黑名单文件扩展名: { ".patch", ".7z", ".zip", ".rar", ".tar" , ".json" } + //如果不需要扩展,需要重新传入黑名单集合来覆盖。 + //.SetBlacklist(GetBlackFiles(), GetBlackFormats()) + .LaunchTaskAsync(); } private bool Check1() => true; diff --git a/src/c#/GeneralUpdate.Client/Program.cs b/src/c#/GeneralUpdate.Client/Program.cs index 3e37065a5e3de04d36fad74784c8b43f663c25d2..d2b508002b83f039405c8ee7431fc128eea881a3 100644 --- a/src/c#/GeneralUpdate.Client/Program.cs +++ b/src/c#/GeneralUpdate.Client/Program.cs @@ -9,6 +9,7 @@ MySample sample = new MySample(); //await sample.TestDifferentialClean(); //await sample.TestDifferentialDirty(); + await sample.Upgrade(); }); Console.Read(); } diff --git a/src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs b/src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs new file mode 100644 index 0000000000000000000000000000000000000000..013a2534fde7badaffdc09875c1bd259a437e0a2 --- /dev/null +++ b/src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs @@ -0,0 +1,48 @@ +using GeneralUpdate.Core.Exceptions.CustomArgs; +using System; + +namespace GeneralUpdate.Core.Exceptions +{ + internal sealed class ThrowExceptionUtility + { + public static void ThrowGeneralUpdateException(ExceptionArgs args) + => Throw(args.ToString(), args); + + #region Common + + /// + /// Checks if an object is empty and throws an exception if it is + /// + /// + /// + /// + public static void ThrowIfNull(object obj, string paramName) + { + if (obj == null) + Throw(paramName); + } + + /// + /// Checks if the string is empty or blank, and throws an exception if it is. + /// + /// + /// + /// + public static void ThrowIfNullOrWhiteSpace(string str, string paramName) + { + if (string.IsNullOrWhiteSpace(str)) + Throw("Parameter cannot be null or whitespace", paramName); + } + + /// + /// Basic method of exception declaration. + /// + /// + /// + /// + public static void Throw(string message, params object[] args) where T : Exception, new() + => throw (T)Activator.CreateInstance(typeof(T), message, args); + + #endregion + } +} diff --git a/src/c#/GeneralUpdate.Upgrad/Program.cs b/src/c#/GeneralUpdate.Upgrad/Program.cs index 6189220b0fd1794b255db98235af6d403a8b1d18..0a9e88af83c568c01d740fc87e3d3b33a2f8fd62 100644 --- a/src/c#/GeneralUpdate.Upgrad/Program.cs +++ b/src/c#/GeneralUpdate.Upgrad/Program.cs @@ -50,11 +50,11 @@ namespace GeneralUpdate.Upgrad .AddListenerMultiDownloadError(OnMultiDownloadError) //整个更新过程出现的任何问题都会通过这个事件通知 .AddListenerException(OnException) - .Strategy(). - Option(UpdateOption.Encoding, Encoding.Default). - Option(UpdateOption.DownloadTimeOut, 60). - Option(UpdateOption.Format, Format.ZIP). - LaunchTaskAsync(); + .Strategy() + .Option(UpdateOption.Encoding, Encoding.Default) + .Option(UpdateOption.DownloadTimeOut, 60) + .Option(UpdateOption.Format, Format.ZIP) + .LaunchTaskAsync(); }); Console.Read(); }