From 0714718b95e9a0fe02e539577ef10f5e14188f97 Mon Sep 17 00:00:00 2001 From: Marc Moriyamas Date: Fri, 4 Sep 2020 13:37:36 +0100 Subject: [PATCH 1/3] Update Examples that show subscribing to a Static Event in a component to show unsubscribing from the event in Terminate() --- Extending/Section-Trees/trees.md | 15 +++++++++++++++ .../Code/Subscribing-To-Events/index.md | 11 ++++++++++- Getting-Started/Code/Umbraco-Services/index.md | 3 ++- Implementation/Composing/index.md | 5 ++++- Implementation/Custom-Routing/index.md | 17 +++++++++++------ Implementation/Services/index.md | 4 +++- Reference/Cache/examples/tags.md | 5 ++++- Reference/Events/ContentService-Events.md | 3 ++- Reference/Events/EditorModel-Events/index.md | 2 ++ Reference/Events/MediaService-Events.md | 3 ++- Reference/Events/MemberService-Events/index.md | 3 ++- .../Services/RelationService/index.md | 5 ++++- .../Request-Pipeline/inbound-pipeline.md | 17 +++++++++++------ Reference/Scheduling/index.md | 8 ++++++++ Reference/Searching/Examine/examine-events.md | 1 + Tutorials/Porting-Packages-V8/index.md | 2 ++ 16 files changed, 83 insertions(+), 21 deletions(-) diff --git a/Extending/Section-Trees/trees.md b/Extending/Section-Trees/trees.md index cbc465c09d9..9bc1821b2dc 100644 --- a/Extending/Section-Trees/trees.md +++ b/Extending/Section-Trees/trees.md @@ -263,6 +263,11 @@ void TreeControllerBase_RootNodeRendering(TreeControllerBase sender, TreeNodeRen e.Node.Title = "My new title"; } } +public void Terminate() +{ + // unsubscribe on shutdown + TreeControllerBase.RootNodeRendering -= TreeControllerBase_RootNodeRendering; +} ``` ### TreeNodesRendering @@ -296,6 +301,11 @@ void TreeControllerBase_TreeNodesRendering(TreeControllerBase sender, TreeNodesR e.Nodes.RemoveAll(node => node.Name.StartsWith("Private")); } } +public void Terminate() +{ + // unsubscribe on shutdown + TreeControllerBase.TreeNodesRendering -= TreeControllerBase_TreeNodesRendering; +} ``` ### MenuRendering @@ -343,6 +353,11 @@ void TreeControllerBase_MenuRendering(TreeControllerBase sender, MenuRenderingEv e.Menu.Items.Insert(5, i); } } +public void Terminate() +{ + // unsubscribe on shutdown + TreeControllerBase.MenuRendering -= TreeControllerBase_MenuRendering; +} ``` ## Tree Actions and User Permissions diff --git a/Getting-Started/Code/Subscribing-To-Events/index.md b/Getting-Started/Code/Subscribing-To-Events/index.md index c9a55447417..269504b3483 100644 --- a/Getting-Started/Code/Subscribing-To-Events/index.md +++ b/Getting-Started/Code/Subscribing-To-Events/index.md @@ -83,7 +83,15 @@ private void ContentService_Published(Umbraco.Core.Services.IContentService send { // the custom code to fire everytime content is published goes here! } +public void Terminate() +{ + // unsubscribe on shutdown + ContentService.Published -= ContentService_Published; +} ``` +:::note +When you subscribe to static events you should also unsubscribe from them when Umbraco shuts down, see the Terminate() method in the example using the -+ syntax to achieve the unsubscribing. +::: Let's check if this works by adding a message to the log every time the publish event occurs. @@ -186,7 +194,8 @@ namespace MyProjectName.Web.Components // terminate: runs once when Umbraco stops public void Terminate() { - // do something when Umbraco terminates + // unsubscribe on shutdown + ContentService.Published -= ContentService_Published; } } } diff --git a/Getting-Started/Code/Umbraco-Services/index.md b/Getting-Started/Code/Umbraco-Services/index.md index 3f94a655420..4a465d5e073 100644 --- a/Getting-Started/Code/Umbraco-Services/index.md +++ b/Getting-Started/Code/Umbraco-Services/index.md @@ -101,7 +101,8 @@ namespace Umbraco8.Components public void Terminate() { - // Nothing to terminate + //unsubscribe during shutdown + ContentService.Saved -= ContentService_Saved; } } } diff --git a/Implementation/Composing/index.md b/Implementation/Composing/index.md index fe5b1f5e8c6..f81d16d0ea6 100644 --- a/Implementation/Composing/index.md +++ b/Implementation/Composing/index.md @@ -122,7 +122,10 @@ namespace My.Website // terminate: runs once when Umbraco stops public void Terminate() - { } + { + //unsubscribe during shutdown + ContentService.Saving -= ContentService_Saving; + } private void ContentService_Saving(IContentService sender, ContentSavingEventArgs e) { diff --git a/Implementation/Custom-Routing/index.md b/Implementation/Custom-Routing/index.md index f7dc4dbb9b2..2656d254d74 100644 --- a/Implementation/Custom-Routing/index.md +++ b/Implementation/Custom-Routing/index.md @@ -117,14 +117,19 @@ namespace Umbraco8.Components { public void Initialize() { - PublishedRequest.Prepared += (sender, args) => - { - var request = sender as PublishedRequest; - // do something… - }; + PublishedRequest.Prepared += PublishedRequest_Prepared; } - public void Terminate() {} + private void PublishedRequest_Prepared(object sender, EventArgs e) + { + var request = sender as PublishedRequest; + // do something… + } + + public void Terminate() { + //unsubscribe during shutdown + PublishedRequest.Prepared -= PublishedRequest_Prepared; +} } } ``` diff --git a/Implementation/Services/index.md b/Implementation/Services/index.md index c059b473c1c..6aa41ecfa4f 100644 --- a/Implementation/Services/index.md +++ b/Implementation/Services/index.md @@ -130,7 +130,8 @@ namespace Umbraco8.Components public void Terminate() { - // called when the Umbraco application shuts down. + // called when the Umbraco application shuts down. + ContentService.Saved -= ContentService_Saved; } } } @@ -205,6 +206,7 @@ namespace Umbraco8.Components public void Terminate() { // called when the Umbraco application shuts down. + ContentService.Unpublishing -= ContentService_Unpublishing; } } } diff --git a/Reference/Cache/examples/tags.md b/Reference/Cache/examples/tags.md index 424f75c2614..58e66fc7aab 100644 --- a/Reference/Cache/examples/tags.md +++ b/Reference/Cache/examples/tags.md @@ -203,7 +203,10 @@ namespace Doccers.Core } } - public void Terminate() { } + public void Terminate() { + //unsubscribe during shutdown + ContentService.Published -= ContentService_Published; + } } } ``` diff --git a/Reference/Events/ContentService-Events.md b/Reference/Events/ContentService-Events.md index 5ace9d01e21..3ea32c19141 100644 --- a/Reference/Events/ContentService-Events.md +++ b/Reference/Events/ContentService-Events.md @@ -51,7 +51,8 @@ namespace Umbraco8.Components } public void Terminate() { - // Nothing to terminate + //unsubscribe during shutdown + ContentService.Publishing -= ContentService_Publishing; } } } diff --git a/Reference/Events/EditorModel-Events/index.md b/Reference/Events/EditorModel-Events/index.md index 93aeaeac2ab..67c680ff871 100644 --- a/Reference/Events/EditorModel-Events/index.md +++ b/Reference/Events/EditorModel-Events/index.md @@ -39,6 +39,8 @@ namespace My.Website // terminate: runs once when Umbraco stops public void Terminate() { + //unsubscribe during shutdown + EditorModelEventManager.SendingContentModel -= EditorModelEventManager_SendingContentModel; } private void EditorModelEventManager_SendingContentModel(System.Web.Http.Filters.HttpActionExecutedContext sender, EditorModelEventArgs e) diff --git a/Reference/Events/MediaService-Events.md b/Reference/Events/MediaService-Events.md index bf614c502b3..2867f0cf5c0 100644 --- a/Reference/Events/MediaService-Events.md +++ b/Reference/Events/MediaService-Events.md @@ -46,7 +46,8 @@ namespace Umbraco8.Components public void Terminate() { - // Nothing to terminate + //unsubscribe during shutdown + MediaService.Saved -= MediaService_Saved; } } } diff --git a/Reference/Events/MemberService-Events/index.md b/Reference/Events/MemberService-Events/index.md index 02cb9796e38..258457941ec 100644 --- a/Reference/Events/MemberService-Events/index.md +++ b/Reference/Events/MemberService-Events/index.md @@ -50,7 +50,8 @@ namespace Umbraco8.Components } public void Terminate() { - // Nothing to terminate + //unsubscribe during shutdown + MemberService.Saved -= MemberService_Saved; } } } diff --git a/Reference/Management/Services/RelationService/index.md b/Reference/Management/Services/RelationService/index.md index 46e32e542ac..f056007926d 100644 --- a/Reference/Management/Services/RelationService/index.md +++ b/Reference/Management/Services/RelationService/index.md @@ -358,7 +358,10 @@ namespace Doccers.Core.Components } } - public void Terminate() { } + public void Terminate() { + //unsubscribe during shutdown + ContentService.Published -= ContentService_Published; + } } } ``` diff --git a/Reference/Routing/Request-Pipeline/inbound-pipeline.md b/Reference/Routing/Request-Pipeline/inbound-pipeline.md index d3ec8bd7890..3eb750d53f5 100644 --- a/Reference/Routing/Request-Pipeline/inbound-pipeline.md +++ b/Reference/Routing/Request-Pipeline/inbound-pipeline.md @@ -58,14 +58,19 @@ namespace Umbraco8.Components { public void Initialize() { - PublishedRequest.Prepared += (sender, args) => - { - var request = sender as PublishedRequest; - // do something… - }; + PublishedRequest.Prepared += PublishedRequest_Prepared; } - public void Terminate() {} + private void PublishedRequest_Prepared(object sender, EventArgs e) + { + var request = sender as PublishedRequest; + // do something… + } + + public void Terminate() { + //unsubscribe during shutdown + PublishedRequest.Prepared -= PublishedRequest_Prepared; + } } } ``` diff --git a/Reference/Scheduling/index.md b/Reference/Scheduling/index.md index 311cfb58256..3b1124175ec 100644 --- a/Reference/Scheduling/index.md +++ b/Reference/Scheduling/index.md @@ -165,6 +165,14 @@ public class CleanUpYourRoomComponent : IComponent public void Terminate() { + //unsubscribe during shutdown + _cleanUpYourRoomRunner.TaskCompleted -= Task_Completed; + + _cleanUpYourRoomRunner.TaskStarting -= this.Task_Starting; + + _cleanUpYourRoomRunner.TaskCancelled -= this.Task_Cancelled; + + _cleanUpYourRoomRunner.TaskError -= this.Task_Error; } } diff --git a/Reference/Searching/Examine/examine-events.md b/Reference/Searching/Examine/examine-events.md index 53e819a44eb..b5f157ded08 100644 --- a/Reference/Searching/Examine/examine-events.md +++ b/Reference/Searching/Examine/examine-events.md @@ -66,6 +66,7 @@ public class ExamineEvents : IComponent public void Terminate() { + indexProvider.TransformingIndexValues -= IndexProviderTransformingIndexValues; } } ``` diff --git a/Tutorials/Porting-Packages-V8/index.md b/Tutorials/Porting-Packages-V8/index.md index 35dc9c07530..42b2b3a1e41 100644 --- a/Tutorials/Porting-Packages-V8/index.md +++ b/Tutorials/Porting-Packages-V8/index.md @@ -136,6 +136,8 @@ namespace MyProject.Components public void Terminate() { + //unsubscribe during shutdown + ContentService.Saving -= this.ContentService_Saving; } /// From 664f8d2e7912021b826a74443d830f01ff4cc43a Mon Sep 17 00:00:00 2001 From: Marc Goodson Date: Mon, 7 Sep 2020 08:47:34 +0100 Subject: [PATCH 2/3] Update Reference/Searching/Examine/examine-events.md Yes looks good Co-authored-by: sofietoft --- Reference/Searching/Examine/examine-events.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Reference/Searching/Examine/examine-events.md b/Reference/Searching/Examine/examine-events.md index b5f157ded08..5036c96fde1 100644 --- a/Reference/Searching/Examine/examine-events.md +++ b/Reference/Searching/Examine/examine-events.md @@ -66,7 +66,8 @@ public class ExamineEvents : IComponent public void Terminate() { - indexProvider.TransformingIndexValues -= IndexProviderTransformingIndexValues; + //unsubscribe during shutdown + indexProvider.TransformingIndexValues -= IndexProviderTransformingIndexValues; } } ``` From 71cefdca2422887e8fd985be2db91b31af87749e Mon Sep 17 00:00:00 2001 From: sofietoft Date: Mon, 7 Sep 2020 10:13:59 +0200 Subject: [PATCH 3/3] Update syntax highlighting to csharp --- Reference/Searching/Examine/examine-events.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Reference/Searching/Examine/examine-events.md b/Reference/Searching/Examine/examine-events.md index 5036c96fde1..91dd8a09277 100644 --- a/Reference/Searching/Examine/examine-events.md +++ b/Reference/Searching/Examine/examine-events.md @@ -16,7 +16,7 @@ In the [Quick Start](Quick-Start/index.md) documentation you can see how to perf However, what if you want to search through several different node types and search across many different fields, you will typically need to have a query that looks like this: -```c# +```csharp var textFields = new[] { "title", "description", "content", .... }; var results = searcher.CreateQuery("content") .GroupedOr(textFields, searchTerm) @@ -33,7 +33,7 @@ This example will build upon the Umbraco Starterkit as it is a good starting poi So to add a TransformingIndexValues event we will add a controller that inherits from `IComponent`. Something like this: -```c# +```csharp public class ExamineEvents : IComponent { private readonly IExamineManager _examineManager; @@ -74,7 +74,7 @@ public class ExamineEvents : IComponent You can read more about this [syntax and Components here](../../../Implementation/Composing/index.md). We can now add the logic to combine fields in the `IndexProviderTransformingIndexValues` method: -```c# +```csharp if (e.ValueSet.Category == IndexTypes.Content) { var combinedFields = new StringBuilder(); @@ -106,7 +106,7 @@ So at this point we have done something along the lines of: Next is an optional step to highlight how you can access the Umbraco published cache during an indexing event. In this example, we will be adding the breadcrumb values to our combined field. -```cs +```csharp private string GetBreadcrumb(string id) { if (int.TryParse(idString, out var id)) @@ -131,7 +131,7 @@ This example is here to highlight the user of the Scope Provider. A Scope is req Before this works the component will have to be registered in a composer. If you already have a composer you can add it to that one, but for this example we will make a new composer: -```c# +```csharp //This is a composer which automatically appends the ExamineEvents component public class ExamineComposer : ComponentComposer, IUserComposer { @@ -145,6 +145,6 @@ We append it so it runs as the last one. Now if you start up your website and [r At this point you can create a query for only that field: -```c# +```csharp var results = searcher.CreateQuery("content").Field("combinedField", searchTerm).Execute(); ```