Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Examples that show subscribing to a Static Event in a component to show unsubscribing from the event in Terminate() #2692

Merged
merged 3 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Extending/Section-Trees/trees.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion Getting-Started/Code/Subscribing-To-Events/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Getting-Started/Code/Umbraco-Services/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ namespace Umbraco8.Components

public void Terminate()
{
// Nothing to terminate
//unsubscribe during shutdown
ContentService.Saved -= ContentService_Saved;
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Implementation/Composing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
17 changes: 11 additions & 6 deletions Implementation/Custom-Routing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
```
4 changes: 3 additions & 1 deletion Implementation/Services/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -205,6 +206,7 @@ namespace Umbraco8.Components
public void Terminate()
{
// called when the Umbraco application shuts down.
ContentService.Unpublishing -= ContentService_Unpublishing;
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Reference/Cache/examples/tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ namespace Doccers.Core
}
}

public void Terminate() { }
public void Terminate() {
//unsubscribe during shutdown
ContentService.Published -= ContentService_Published;
}
}
}
```
Expand Down
3 changes: 2 additions & 1 deletion Reference/Events/ContentService-Events.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ namespace Umbraco8.Components
}
public void Terminate()
{
// Nothing to terminate
//unsubscribe during shutdown
ContentService.Publishing -= ContentService_Publishing;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions Reference/Events/EditorModel-Events/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Umbraco.Web.Models.ContentEditing.ContentItemDisplay> e)
Expand Down
3 changes: 2 additions & 1 deletion Reference/Events/MediaService-Events.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ namespace Umbraco8.Components

public void Terminate()
{
// Nothing to terminate
//unsubscribe during shutdown
MediaService.Saved -= MediaService_Saved;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Reference/Events/MemberService-Events/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ namespace Umbraco8.Components
}
public void Terminate()
{
// Nothing to terminate
//unsubscribe during shutdown
MemberService.Saved -= MemberService_Saved;
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Reference/Management/Services/RelationService/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,10 @@ namespace Doccers.Core.Components
}
}

public void Terminate() { }
public void Terminate() {
//unsubscribe during shutdown
ContentService.Published -= ContentService_Published;
}
}
}
```
Expand Down
17 changes: 11 additions & 6 deletions Reference/Routing/Request-Pipeline/inbound-pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
```
8 changes: 8 additions & 0 deletions Reference/Scheduling/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
14 changes: 8 additions & 6 deletions Reference/Searching/Examine/examine-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -66,13 +66,15 @@ public class ExamineEvents : IComponent

public void Terminate()
{
//unsubscribe during shutdown
indexProvider.TransformingIndexValues -= IndexProviderTransformingIndexValues;
}
}
```

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();
Expand Down Expand Up @@ -104,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))
Expand All @@ -129,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<ExamineEvents>, IUserComposer
{
Expand All @@ -143,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();
```
2 changes: 2 additions & 0 deletions Tutorials/Porting-Packages-V8/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ namespace MyProject.Components

public void Terminate()
{
//unsubscribe during shutdown
ContentService.Saving -= this.ContentService_Saving;
}

/// <summary>
Expand Down