|
| 1 | +# Microsoft Orleans Streaming for ADO.NET |
| 2 | + |
| 3 | +## Introduction |
| 4 | +Microsoft Orleans Streaming for ADO.NET provides a stream provider implementation for Orleans using ADO.NET-compatible databases (SQL Server, MySQL, PostgreSQL, etc.). This allows for publishing and subscribing to streams of events with relational databases as the underlying infrastructure. |
| 5 | + |
| 6 | +## Getting Started |
| 7 | +To use this package, install it via NuGet: |
| 8 | + |
| 9 | +```shell |
| 10 | +dotnet add package Microsoft.Orleans.Streaming.AdoNet |
| 11 | +``` |
| 12 | + |
| 13 | +You will also need to install the appropriate ADO.NET provider for your database: |
| 14 | + |
| 15 | +```shell |
| 16 | +# For SQL Server |
| 17 | +dotnet add package System.Data.SqlClient |
| 18 | + |
| 19 | +# For MySQL |
| 20 | +dotnet add package MySql.Data |
| 21 | + |
| 22 | +# For PostgreSQL |
| 23 | +dotnet add package Npgsql |
| 24 | +``` |
| 25 | + |
| 26 | +## Example - Configuring ADO.NET Streaming |
| 27 | +```csharp |
| 28 | +using Microsoft.Extensions.Hosting; |
| 29 | +using Orleans.Hosting; |
| 30 | +using Orleans.Streams; |
| 31 | + |
| 32 | +var builder = new HostBuilder() |
| 33 | + .UseOrleans(siloBuilder => |
| 34 | + { |
| 35 | + siloBuilder |
| 36 | + .UseLocalhostClustering() |
| 37 | + // Configure ADO.NET as a stream provider |
| 38 | + .AddAdoNetStreams( |
| 39 | + name: "AdoNetStreamProvider", |
| 40 | + configureOptions: options => |
| 41 | + { |
| 42 | + options.Invariant = "System.Data.SqlClient"; // For SQL Server |
| 43 | + options.ConnectionString = "Server=localhost;Database=OrleansStreaming;User ID=orleans;******;"; |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | +// Run the host |
| 48 | +await builder.RunConsoleAsync(); |
| 49 | +``` |
| 50 | + |
| 51 | +## Example - Using ADO.NET Streams in a Grain |
| 52 | +```csharp |
| 53 | +// Producer grain |
| 54 | +public class ProducerGrain : Grain, IProducerGrain |
| 55 | +{ |
| 56 | + private IAsyncStream<string> _stream; |
| 57 | + |
| 58 | + public override Task OnActivateAsync(CancellationToken cancellationToken) |
| 59 | + { |
| 60 | + // Get a reference to a stream |
| 61 | + var streamProvider = GetStreamProvider("AdoNetStreamProvider"); |
| 62 | + _stream = streamProvider.GetStream<string>(Guid.NewGuid(), "MyStreamNamespace"); |
| 63 | + |
| 64 | + return base.OnActivateAsync(cancellationToken); |
| 65 | + } |
| 66 | + |
| 67 | + public async Task SendMessage(string message) |
| 68 | + { |
| 69 | + // Send a message to the stream |
| 70 | + await _stream.OnNextAsync(message); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// Consumer grain |
| 75 | +public class ConsumerGrain : Grain, IConsumerGrain, IAsyncObserver<string> |
| 76 | +{ |
| 77 | + private StreamSubscriptionHandle<string> _subscription; |
| 78 | + |
| 79 | + public override async Task OnActivateAsync(CancellationToken cancellationToken) |
| 80 | + { |
| 81 | + // Get a reference to a stream |
| 82 | + var streamProvider = GetStreamProvider("AdoNetStreamProvider"); |
| 83 | + var stream = streamProvider.GetStream<string>(this.GetPrimaryKey(), "MyStreamNamespace"); |
| 84 | + |
| 85 | + // Subscribe to the stream |
| 86 | + _subscription = await stream.SubscribeAsync(this); |
| 87 | + |
| 88 | + await base.OnActivateAsync(cancellationToken); |
| 89 | + } |
| 90 | + |
| 91 | + public Task OnNextAsync(string item, StreamSequenceToken token = null) |
| 92 | + { |
| 93 | + Console.WriteLine($"Received message: {item}"); |
| 94 | + return Task.CompletedTask; |
| 95 | + } |
| 96 | + |
| 97 | + public Task OnCompletedAsync() |
| 98 | + { |
| 99 | + Console.WriteLine("Stream completed"); |
| 100 | + return Task.CompletedTask; |
| 101 | + } |
| 102 | + |
| 103 | + public Task OnErrorAsync(Exception ex) |
| 104 | + { |
| 105 | + Console.WriteLine($"Stream error: {ex.Message}"); |
| 106 | + return Task.CompletedTask; |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## Documentation |
| 112 | +For more comprehensive documentation, please refer to: |
| 113 | +- [Microsoft Orleans Documentation](https://docs.microsoft.com/dotnet/orleans/) |
| 114 | +- [Orleans Streams](https://learn.microsoft.com/en-us/dotnet/orleans/streaming/index) |
| 115 | +- [Stream Providers](https://learn.microsoft.com/en-us/dotnet/orleans/streaming/stream-providers) |
| 116 | +- [ADO.NET Database Setup](https://learn.microsoft.com/en-us/dotnet/orleans/host/configuration-guide/adonet-configuration) |
| 117 | + |
| 118 | +## Feedback & Contributing |
| 119 | +- If you have any issues or would like to provide feedback, please [open an issue on GitHub](https://github.com/dotnet/orleans/issues) |
| 120 | +- Join our community on [Discord](https://aka.ms/orleans-discord) |
| 121 | +- Follow the [@msftorleans](https://twitter.com/msftorleans) Twitter account for Orleans announcements |
| 122 | +- Contributions are welcome! Please review our [contribution guidelines](https://github.com/dotnet/orleans/blob/main/CONTRIBUTING.md) |
| 123 | +- This project is licensed under the [MIT license](https://github.com/dotnet/orleans/blob/main/LICENSE) |
0 commit comments