Lädt...

🔧 Brighter and SQL Server: How to setup and use Brighter with MS SQL


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

In this article, we’ll focus on integrating Brighter with Microsoft SQL Server.

Introduction to Microsoft SQL Server

Microsoft SQL Server is a robust relational database management system (RDBMS) widely adopted in .NET ecosystems. While traditionally used for transactional data storage, SQL Server can also act as a message broker in distributed systems. Brighter—a command-processing library—supports SQL Server as a messaging backend, offering a pragmatic solution for scenarios where deploying dedicated brokers like RabbitMQ or Kafka is impractical (e.g., small projects or legacy environments).

Requirement

Brighter Recap

Before continuing about Microsoft SQL Server configuration, let's recap what we already know about Brighter.

Request (Command/Event)

Define messages using IRequest:

public class Greeting() : Event(Guid.NewGuid())
{
    public string Name { get; set; } = string.Empty;
}
  • Commands: Single-recipient operations (e.g., SendEmail).
  • Events: Broadcast notifications (e.g., OrderShipped).

Message Mapper

Translates between Brighter messages and your app objects:

public class GreetingMapper : IAmAMessageMapper<Greeting>
{
    public Message MapToMessage(Greeting request)
    {
        var header = new MessageHeader();
        header.Id = request.Id; 
        header.TimeStamp = DateTime.UtcNow;
        header.Topic = "greeting.topic"; // The target topic to be publish
        header.MessageType = MessageType.MT_EVENT;

        var body = new MessageBody(JsonSerializer.Serialize(request));
        return new Message(header, body);
    }

    public Greeting MapToRequest(Message message)
    {
        return JsonSerializer.Deserialize<Greeting>(message.Body.Bytes)!;
    }
}

Request Handler

Processes incoming messages:

public class GreetingHandler(ILogger<GreetingHandler> logger) : RequestHandler<Greeting>
{
    public override Greeting Handle(Greeting command)
    {
        logger.LogInformation("Hello {Name}", command.Name);
        return base.Handle(command);
    }
}

Configuring Brighter with SQL Server

Step 1: Database Setup

Create a table to store messages (e.g., QueueData):

CREATE TABLE [dbo].[QueueData](
    [Id] BIGINT IDENTITY(1,1) PRIMARY KEY,
    [Topic] NVARCHAR(255) NOT NULL,
    [MessageType] NVARCHAR(1024) NOT NULL,
    [Payload] NVARCHAR(MAX) NOT NULL
);
CREATE NONCLUSTERED INDEX [IX_Topic] ON [dbo].[QueueData] ([Topic]);

Step 2: Configure SQL Server Connection

Define connection settings in C#:

var sqlConfig = new MsSqlConfiguration(
    connectionString: "<Your_Connection_String>",
    queueStoreTable: "QueueData" // Table created above
);

Step 3: Consumer Configuration

Subscribe to an SQL Server queue and:

.AddServiceActivator(opt =>
{
    opt.Subscriptions = [
        new MsSqlSubscription<Greeting>(
            subscriptionName: new SubscriptionName("greeting-subscription"),
            channelName: new ChannelName("greeting.topic"), // Maps to the Topic column
            isAsync: false // Set to true if using async handlers
        )
    ];
    opt.ChannelFactory = new ChannelFactory(new MsSqlMessageConsumerFactory(sqlConfig));
});

Step 4: Producer Configuration

Publish events to SQL Server:

.UseExternalBus(new MsSqlProducerRegistryFactory(sqlConfig, new[]
{
    new Publication
    {
        Topic = new RoutingKey("greeting.topic")
    }
}).Create());

Key Considerations

  • Schema Ownership: You must manually create the QueueData table and index. Brighter does not auto-generate schemas.
  • Performance: SQL Server is not optimized for high-throughput messaging. Use it for lightweight scenarios or prototyping.

Conclusion

Conclusion Brighter's SQL Server integration provides a pragmatic messaging solution for .NET applications where dedicated brokers are unavailable. By leveraging SQL Server’s transactional guarantees, this approach ensures reliable message delivery with minimal infrastructure overhead. For production workloads, pair it with monitoring (e.g., SQL Server Profiler) and error-handling strategies.

Reference

GitHub Repository

...

🔧 Brighter and SQL Server: How to setup and use Brighter with MS SQL


📈 58.64 Punkte
🔧 Programmierung

🔧 Brighter and Redis/Valkey: How to setup and use Brighter with Redis/Valkey


📈 50.41 Punkte
🔧 Programmierung

🔧 Brighter and RabbitMQ: How to setup and use Brighter with RabbitMQ


📈 50.41 Punkte
🔧 Programmierung

🔧 Brighter and Azure: How to setup and use Brighter with Azure Service Bus


📈 50.41 Punkte
🔧 Programmierung

🔧 Brighter and AWS: How to setup and use Brighter with AWS SNS/SQS


📈 50.41 Punkte
🔧 Programmierung

🔧 Brighter and Kafka: How to setup and use Brighter with Apache Kafka


📈 50.41 Punkte
🔧 Programmierung

🔧 Brighter + Redis/Valkey: Como configurar o Brighter com Redis/Valkey


📈 36.25 Punkte
🔧 Programmierung

🔧 Brighter + RabbitMQ: Como configurar Brighter com RabbitMQ


📈 36.25 Punkte
🔧 Programmierung

🔧 Brighter e Azure: Como configurar e usar o Brighter com o Azure Service Bus


📈 36.25 Punkte
🔧 Programmierung

🔧 Integração do Brighter com Microsoft SQL Server


📈 23.82 Punkte
🔧 Programmierung

🔧 Demystifying Brighter: Commands, Events, and the Send-Publish-Post Trifecta


📈 18.87 Punkte
🔧 Programmierung

🍏 Budget Apple Vision Pro could be slimmer and brighter


📈 18.87 Punkte
🍏 iOS / Mac OS

🍏 iPhone 16 Pro will have brighter and longer-lasting display


📈 18.87 Punkte
🍏 iOS / Mac OS

🍏 Bigger and brighter: iPhone 16 &amp; iPhone 16 Pro rumored screen changes


📈 18.87 Punkte
🍏 iOS / Mac OS

🍏 Nomad’s new Sport Band and iPhone case glow even brighter in the dark


📈 18.87 Punkte
🍏 iOS / Mac OS

🪟 Microsoft and Qualcomm’s future gets brighter. Don’t miss OnPodcast this Sunday!


📈 18.87 Punkte
🪟 Windows Tipps

🪟 Razer Firefly V2 is a brighter and thinner RGB surface for your mouse


📈 18.87 Punkte
🪟 Windows Tipps

🎥 Pixel Watch 3 hands-on | Bigger, Brighter, Better (45mm vs 41)


📈 18.12 Punkte
🎥 Video | Youtube

📰 The Fabulous Five: How CyberHoot Makes Managed Service Providers Shine Brighter


📈 18.12 Punkte
📰 IT Security Nachrichten

🔧 Brighter: Handler Assíncronos


📈 18.12 Punkte
🔧 Programmierung

📰 Fahrgastlenkung, Anonymisierung: Deutsche Bahn investiert in Brighter AI


📈 18.12 Punkte
📰 IT Nachrichten

📰 Database Admins See Brighter Job Prospects Amid IT Challenges


📈 18.12 Punkte
📰 IT Nachrichten

🔧 Asynchronous Handlers in Brighter


📈 18.12 Punkte
🔧 Programmierung

🐧 Steam Deck - The future of Linux gaming has never looked brighter


📈 18.12 Punkte
🐧 Linux Tipps

📰 Database Admins See Brighter Job Prospects Amid IT Challenges


📈 18.12 Punkte
📰 IT Nachrichten

🔧 Integração do Brighter com AWS SNS/SQS: Guia Prático


📈 18.12 Punkte
🔧 Programmierung

📰 Sony TVs Get Brighter OLED, Cognitive Processing, Google TV Streaming in 2021


📈 18.12 Punkte
📰 IT Security Nachrichten

📰 SpaceX's New Direct-To-Cell Starlink Satellites Are Way Brighter Than the Originals


📈 18.12 Punkte
📰 IT Security Nachrichten

📰 KI-Update kompakt: OpenAIs EU-Blueprint, Brighter AI, Klimaschutz, GPT-4o


📈 18.12 Punkte
📰 IT Nachrichten

🪟 The future of Xbox gaming looks brighter than Craig's smile


📈 18.12 Punkte
🪟 Windows Tipps

🍏 How To Make the Time Brighter on iPhone Lock Screen in iOS 17


📈 18.12 Punkte
🍏 iOS / Mac OS