Tutorials SignalR & Real-Time .NET Applications

Strongly Typed Hubs: Enforcing the contract

On this page

Contract-First Communication

Stop using magic strings like "ReceiveMessage". In enterprise .NET, we use **Strongly Typed Hubs** to prevent bugs and enable IntelliSense.

1. The Hub Interface

Define an interface (e.g., IChatClient) with all the methods the client can handle. Then, derive your Hub from Hub<IChatClient>. Now, Clients.All.ReceiveMessage(msg) is compile-time checked. If you rename the method in the interface, the compiler will force you to update it everywhere.

2. Code Sharing

Place this interface in a **Shared Class Library**. Your C# Client (like a Blazor WASM or a worker service) can also use this interface to register its handlers. This ensures that the server and client are always speaking the same 'Language'.

3. Architect Insight

Q: "Should I use strongly typed hubs for JS clients?"

Architect Answer: "YES. Even though Javascript isn't compiled, using the interface on the server ensures consistency. For TypeScript clients, you can even use tools to generate the TS interfaces from your C# DLL, giving you full end-to-end type safety for your real-time messages."

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

SignalR & Real-Time .NET Applications
Course syllabus
1. SignalR Core
2. Managing Users & Groups
3. Scaling SignalR
4. Advanced Communication
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details