Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Twitch MediatR with ASP.NET Core

Twitch MediatR with ASP.NET Core
April 26, 2019 3 min read BEN ABT
Twitch TwitchLib ASP.NET Core

This article was written in 2019. Versions, tools and recommendations may have changed since then. Newer posts are collected under ASP.NET Core.

Twitch

Twitch is the leading streaming platform for video games and Co.

Starting small, Twitch hosts monthly over 3.5 million streamers that can be watched over the shoulder while playing games, cooking or driving a car. This platform is therefore very well known, especially with the younger generations. More than 70% of the viewers, whose average age is 21 years, belong to the millennials. The average Twitch user already spends almost 2 hours a day on the platform to watch videos.

Many analysts say that Twitch and similar streaming platforms will completely replace television, especially among young people. Amazon acquired Twitch in 2014 for nearly $1 billion.

From the developer’s point of view Twitch is very attractive, because Twitch has a very powerful and extensible API.

TwitchLib

TwitchLib is a very extensive library for .NET that allows you to access the Twitch API. This includes all kinds of events like streaming chat messages, new subscriber notifications, user content information and much much more. A separate connection is created for each channel that you want to monitor, which then makes .NET events available.

The disadvantage of this way is that this is rather unpleasant to implement with ASP.NET Core.

MediatR

MediatR is a very simplified Mediator implementation in .NET that allows us to easily implement the CQRS pattern through in-process messaging, which otherwise requires an extensive infrastructure. Instead of working with standard .NET Event Delegate Signatures, CQRS works with messages, to put it simply.

In order to subscribe to a message, the source - the sender - does not have to be directly known and thus enables an increased decoupling of the code. Instead, one or more handlers are registered that can process a certain message type.

Messages Types

MediatR uses two types of message types.

  • Requests: Requests usually have a return value, for example, the result of a database query. Usually there is only one request handler for a request message.
  • Notifications: this is a kind of fire-and-forget message and does not allow return value by the handler. Many different notification handlers can be registered to a notification message.

Register Handlers

MediatR is implemented in such a way that all handlers in assemblies are automatically recognized and not each handler has to be registered individually. One or more assemblies can be specified. If no assembly is specified, the current assembly is used automatically.

C#
1Assembly[] assemblies = <assemblies here>
2services.AddMediatR(mediatrAssemblies);

Twitch Bot

I myself have been working with bots on different platforms like Skype, Teams - and Twitch for some time now.

Unfortunately the implementation of TwitchLib was always quite cumbersome and not very flexible, especially if you want to monitor multiple channels with ASP.NET Core.

Therefore I implemented my ASP.NET Core Bots for Twitch with MediatR some time ago to be able to offer more flexible solutions. I have now published this in the form of NuGet packages and on GitHub called Twitch.MediatR .

Twitch.MediatR

Twitch.MediatR is a wrapper for TwitchLib to make Twitch events available as MediatR notifications. Each notification contains the Twitch Channel link from TwitchLib in order to be able to react to a notification, e.g. to send a reply message to a chat message.

C#
1    private async void Client_OnMessageReceived(object sender, OnMessageReceivedArgs e)
2    {
3        await _eventProxy.PublishAsync(new TwitchChannelMessageNotification(this, e.ChatMessage)).ConfigureAwait(false);
4    }

If a certain Notification Type is to be processed - in this example listening to chat messages - just register a NotificationHandler.

C#
 1    public class TwitchChannelMessageNotificationHandler : INotificationHandler<TwitchChannelMessageNotification>
 2    {
 3        public async Task Handle(TwitchChannelMessageNotification request, CancellationToken cancellationToken = default)
 4        {
 5            string channel = request.ChatMessage.Channel;
 6            string response = $"Echo: {request.ChatMessage.Message}";
 7
 8            await request.ChannelLink.SendMessageAsync(channel, response).ConfigureAwait(false);
 9        }
10    }

This handler accepts the chatmessage notification and responds with a simple echo response.

Related articles

Problem Details in ASP.NET Core and .NET 11

Oct 5, 2026 - 12 min read

Problem Details in ASP.NET Core and .NET 11

builder.Services.AddProblemDetails() registers infrastructure. It does not create an error contract. An ASP.NET Core request can fail during …

Resilience in .NET with Microsoft.Extensions.Resilience

Sep 21, 2026 - 11 min read

Resilience in .NET with Microsoft.Extensions.Resilience

Retries are the most visible resilience strategy and the easiest one to misuse. Repeating a failing call can recover from a short network …

What Matters in ASP.NET Core 11

Sep 14, 2026 - 10 min read

What Matters in ASP.NET Core 11

ASP.NET Core 11 has no single feature that fundamentally changes how web applications are built. That is not a criticism. Mature frameworks …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026
Problem Details in ASP.NET Core and .NET 11

Problem Details in ASP.NET Core and .NET 11

Oct 5, 2026
Resilience in .NET with Microsoft.Extensions.Resilience

Resilience in .NET with Microsoft.Extensions.Resilience

Sep 21, 2026
What Matters in ASP.NET Core 11

What Matters in ASP.NET Core 11

Sep 14, 2026

Tags

.NET (192) Csharp (96) ASP.NET Core (48) Azure (41) DevOps (17) Docker (14) Microsoft (14) EF Core (11) Microsoft MVP (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Security (9) Synology (9) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7) Bicep (6)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact