public static IGMethod MCreateExecuteAsyncMethod(string gAccessModifier = "")
        {
            var gMethodDeclaration = new GMethodDeclaration(gName: "ExecuteAsync", gType: "Task",
                                                            gVisibility: "protected", gAccessModifier: gAccessModifier, isConstructor: false,
                                                            gArguments: new Dictionary <IPhilote <IGArgument>, IGArgument>());

            foreach (var kvp in new Dictionary <string, string>()
            {
                { "genericHostsCancellationToken", "CancellationTokenFromCaller " }
            })
            {
                var gMethodArgument = new GArgument(kvp.Key, kvp.Value);
                gMethodDeclaration.GArguments[gMethodArgument.Philote] = gMethodArgument;
            }

            var gBody = new GBody(gStatements: new List <string>()
            {
                "#region Create linkedCancellationSource and linkedCancellationToken",
                "  //// Combine the cancellation tokens,so that either can stop this HostedService",
                "  //linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(internalCancellationToken, externalCancellationToken);",
                "  //var linkedCancellationToken = linkedCancellationTokenSource.Token;",
                "#endregion",
                "#region Register actions with the CancellationTokenFromCaller (s)",
                "  //externalCancellationToken.Register(() => Logger.LogDebug(DebugLocalizer[\"{0} {1} externalCancellationToken has signalled stopping.\"], \"ConsoleMonitorBackgroundService\", \"externalCancellationToken\"));",
                "  //internalCancellationToken.Register(() => Logger.LogDebug(DebugLocalizer[\"{0} {1} internalCancellationToken has signalled stopping.\"], \"ConsoleMonitorBackgroundService\", \"internalCancellationToken\"));",
                "  //linkedCancellationToken.Register(() => Logger.LogDebug(DebugLocalizer[\"{0} {1} linkedCancellationToken has signalled stopping.\"], \"ConsoleMonitorBackgroundService\", \"linkedCancellationToken\"));",
                "#endregion",
                "#region Instantiate this service's Data structure",
                "  //DataInitializationInStartAsyncReplacementPattern",
                "#endregion",
                "// Wait for the conjoined cancellation token (or individually if the hosted service does not define its own internal cts)",
                "// WaitHandle.WaitAny(new[] { linkedCancellationToken.WaitHandle });",
                "Logger.LogDebug(DebugLocalizer[\"{0} {1} ConsoleMonitorBackgroundService is stopping due to \"], \"ConsoleMonitorBackgroundService\", \"ExecuteAsync\"); // add third parameter for internal or external",
                "AssemblyUnitNameReplacementPatternBaseData.Dispose();",
            });
            GComment gComment = new GComment(new List <string>()
            {
                "/// <summary>",
                "/// Called by the genericHost to start the Backgroundservice.",
                "/// Sets up data structures",
                "/// </summary>",
                "/// <param name=\"externalCancellationToken\"></param>",
                "/// <returns></returns>",
            });

            return(new GMethod(gMethodDeclaration, gBody, gComment));
        }
Ejemplo n.º 2
0
        public static IGClass AddTConstructorAutoPropertyGroup(this IGClass gClass, IPhilote <IGMethod> gMethodId, string gAutoPropertyName, string gType, IPhilote <IGPropertyGroup> gPropertyGroupId = default, string?gAccessors = "{ get;}", string?gVisibility = default)
        {
            var gProperty = new GProperty(gAutoPropertyName.ToUpperFirstChar(), gType, gAccessors, gVisibility);

            if (gClass.GPropertyGroups != null && gClass.GPropertyGroups.ContainsKey(gPropertyGroupId))
            {
                gClass.GPropertyGroups[gPropertyGroupId].GPropertys[gProperty.Philote] = gProperty;
            }
            else
            {
                throw new Exception(string.Format("{0} not found in the PropertyGroups of {1}", gPropertyGroupId.ID.ToString(), gClass.GName));
            }
            IGMethod gMethod = default;

            if (gClass.GMethods != null && gClass.GMethods.ContainsKey(gMethodId))
            {
                gMethod = gClass.GMethods[gMethodId];
            }
            else if (gClass.GMethodGroups != null)
            {
                foreach (var kvp in gClass.GMethodGroups)
                {
                    if (kvp.Value.GMethods.ContainsKey(gMethodId))
                    {
                        var gMethodGroup = kvp.Value;
                        gMethod = gMethodGroup.GMethods[gMethodId];
                    }
                }
            }

            if (gMethod == null)
            {
                throw new Exception(string.Format("{0} not found in the Methods or MethodGroups of {1}", gMethodId.ID.ToString(), gClass.GName));
            }

            GArgument gArgument = new GArgument(gAutoPropertyName.ToLowerFirstChar(), gType);

            gMethod.GDeclaration.GArguments[gArgument.Philote] = gArgument;

            gMethod.GBody.GStatements.Add($"{gAutoPropertyName.ToUpperFirstChar()} = {gAutoPropertyName.ToLowerFirstChar()} ?? throw new ArgumentNullException(nameof({gAutoPropertyName.ToLowerFirstChar()}));");
            return(gClass);
        }
        public static IGMethod MCreateStopAsyncMethod(string gAccessModifier = "")
        {
            var gMethodDeclaration = new GMethodDeclaration(gName: "StopAsync", gType: "Task",
                                                            gVisibility: "public", gAccessModifier: gAccessModifier, isConstructor: false,
                                                            gArguments: new Dictionary <IPhilote <IGArgument>, IGArgument>());

            foreach (var kvp in new Dictionary <string, string>()
            {
                { "genericHostsCancellationToken", "CancellationTokenFromCaller " }
            })
            {
                var gMethodArgument = new GArgument(kvp.Key, kvp.Value);
                gMethodDeclaration.GArguments[gMethodArgument.Philote] = gMethodArgument;
            }

            var gBody = new GBody(gStatements: new List <string>()
            {
                "// StopAsync issued in both IHostedService and IHostLifetime interfaces",
                "// This IS called when the user closes the ConsoleWindow with the windows top right pane \"x (close)\" icon",
                "// This IS called when the user hits ctrl-C in the console window",
                "//  After Ctrl-C and after this method exits, the Debugger",
                "//   shows an unhandled Exception: System.OperationCanceledException: 'The operation was canceled.'",
                "// See also discussion of Stop async in the following attributions.",
                "// Attribution to  https://stackoverflow.com/questions/51044781/graceful-shutdown-with-generic-host-in-net-core-2-1",
                "// Attribution to https://stackoverflow.com/questions/52915015/how-to-apply-hostoptions-shutdowntimeout-when-configuring-net-core-generic-host for OperationCanceledException notes",
                //Not sure if this is the right place for the dispose
                "// DataDisposalInStopAsyncReplacementPattern",
                "//InternalCancellationTokenSource.Cancel();",
                "// Defer completion promise, until our application has reported it is done.",
                "// return TaskCompletionSource.Task;",
                "//Stop(); // would call the servicebase stop if this was a generic hosted service ??",
                "//return Task.CompletedTask;"
            });
            GComment gComment = new GComment(new List <string>()
            {
            });

            return(new GMethod(gMethodDeclaration, gBody, gComment));
        }