Skip to content

update example to .NET 8 #7

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

Merged
merged 2 commits into from
May 18, 2025
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
60 changes: 21 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ You only need a few lines of code to add and configure it.

| NuGet | | [![TwitchLib.EventSub.Webhooks][1]][2] |
| :--------------- | ----: | :--------------------------------------------------------------------------- |
| Package Manager | `PM>` | `Install-Package TwitchLib.EventSub.Webhooks -Version 2.3.0` |
| .NET CLI | `>` | `dotnet add package TwitchLib.EventSub.Webhooks --version 2.3.0` |
| PackageReference | | `<PackageReference Include="TwitchLib.EventSub.Webhooks" Version="2.3.0" />` |
| Paket CLI | `>` | `paket add TwitchLib.EventSub.Webhooks --version 2.3.0` |
| Package Manager | `PM>` | `Install-Package TwitchLib.EventSub.Webhooks -Version 3.0.0` |
| .NET CLI | `>` | `dotnet add package TwitchLib.EventSub.Webhooks --version 3.0.0` |
| PackageReference | | `<PackageReference Include="TwitchLib.EventSub.Webhooks" Version="3.0.0" />` |
| Paket CLI | `>` | `paket add TwitchLib.EventSub.Webhooks --version 3.0.0` |

[1]: https://img.shields.io/nuget/v/TwitchLib.EventSub.Webhooks.svg?label=TwitchLib.EventSub.Webhooks
[2]: https://www.nuget.org/packages/TwitchLib.EventSub.Webhooks

## Breaking Changes in Version 3.0
Removed deprecated versions of .NET.

## Breaking Changes in Version 2.0

Version 2.0 contains some breaking changes.
Expand All @@ -33,59 +36,38 @@ The usual requirements that Twitch has for EventSub webhooks do still apply!

## Setup

Step 1: Create a new ASP.NET Core project (.NET 5.0 and up)
Step 1: Create a new ASP.NET Core project (.NET 8.0 and up)

Step 2: Install the TwitchLib.EventSub.Webhooks nuget package. (See above on how to do that)

Step 3: Add necessary services and config to the DI Container

```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddTwitchLibEventSubWebhooks(config =>
{
config.CallbackPath = "/webhooks";
config.Secret = "supersecuresecret";
config.EnableLogging = true;
});
var builder = WebApplication.CreateBuilder(args);

services.AddHostedService<EventSubHostedService>();
}
builder.Services.AddTwitchLibEventSubWebhooks(config =>
{
config.CallbackPath = "/eventsub/";
config.Secret = "supersecuresecret";
});
builder.Services.AddHostedService<EventSubHostedService>();
```

!!! If you follow these steps your callback url will https://{your_domain}/webhooks !!!
!!! If you follow these steps your callback url will `https://{your_domain}/eventsub/` !!!

Step 4: Put the TwitchLib.EventSub.Webhooks middleware in the request pipeline

```csharp
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseRouting();
var app = builder.Build();

app.UseAuthorization();
app.UseTwitchLibEventSubWebhooks();

app.UseTwitchLibEventSubWebhooks();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
app.Run();
```

Step 5: Create the HostedService and listen for events

```csharp
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;
using TwitchLib.EventSub.Webhooks.Core;
using TwitchLib.EventSub.Webhooks.Core.EventArgs;
using TwitchLib.EventSub.Webhooks.Core.EventArgs.Channel;
Expand Down Expand Up @@ -117,12 +99,12 @@ namespace TwitchLib.EventSub.Webhooks.Example
return Task.CompletedTask;
}

private void OnChannelFollow(object sender, ChannelFollowArgs e)
private void OnChannelFollow(object? sender, ChannelFollowArgs e)
{
_logger.LogInformation($"{e.Notification.Event.UserName} followed {e.Notification.Event.BroadcasterUserName} at {e.Notification.Event.FollowedAt.ToUniversalTime()}");
}

private void OnError(object sender, OnErrorArgs e)
private void OnError(object? sender, OnErrorArgs e)
{
_logger.LogError($"Reason: {e.Reason} - Message: {e.Message}");
}
Expand Down
12 changes: 4 additions & 8 deletions TwitchLib.EventSub.Webhooks.Example/EventSubHostedService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;
using TwitchLib.EventSub.Webhooks.Core;
using TwitchLib.EventSub.Webhooks.Core;
using TwitchLib.EventSub.Webhooks.Core.EventArgs;
using TwitchLib.EventSub.Webhooks.Core.EventArgs.Channel;

Expand Down Expand Up @@ -33,14 +29,14 @@ public Task StopAsync(CancellationToken cancellationToken)
return Task.CompletedTask;
}

private void OnChannelFollow(object sender, ChannelFollowArgs e)
private void OnChannelFollow(object? sender, ChannelFollowArgs e)
{
_logger.LogInformation($"{e.Notification.Event.UserName} followed {e.Notification.Event.BroadcasterUserName} at {e.Notification.Event.FollowedAt.ToUniversalTime()}");
}

private void OnError(object sender, OnErrorArgs e)
private void OnError(object? sender, OnErrorArgs e)
{
_logger.LogError($"Reason: {e.Reason} - Message: {e.Message}");
}
}
}
}
36 changes: 19 additions & 17 deletions TwitchLib.EventSub.Webhooks.Example/Program.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using TwitchLib.EventSub.Webhooks.Example;
using TwitchLib.EventSub.Webhooks.Extensions;

namespace TwitchLib.EventSub.Webhooks.Example
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddTwitchLibEventSubWebhooks(config =>
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
config.CallbackPath = "/eventsub/";
config.Secret = "supersecuresecret";
config.EnableLogging = true;
});
builder.Services.AddHostedService<EventSubHostedService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseTwitchLibEventSubWebhooks();

app.MapGet("/", () => "Hello World!");

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
app.Run();
23 changes: 15 additions & 8 deletions TwitchLib.EventSub.Webhooks.Example/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,31 @@
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:6905",
"sslPort": 0
"sslPort": 44336
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"launchUrl": "weatherforecast",
"applicationUrl": "http://localhost:5245",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"TwitchLib.EventSub.Webhooks.Test": {
"https": {
"commandName": "Project",
"dotnetRunMessages": "true",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7135;http://localhost:5245",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": false,
"launchUrl": "weatherforecast",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
50 changes: 0 additions & 50 deletions TwitchLib.EventSub.Webhooks.Example/Startup.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\TwitchLib.EventSub.Webhooks\TwitchLib.EventSub.Webhooks.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Controllers\" />
</ItemGroup>

</Project>