Skip to content

Commit

Permalink
Finish sample
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmarbach committed Feb 14, 2025
1 parent 0dea8f3 commit 700c416
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@

builder.Services.AddAzureServiceBusTopology(builder.Configuration);

var section = builder.Configuration.GetSection("AzureServiceBus");

var endpointConfiguration = new EndpointConfiguration("Samples.AzureServiceBus.Options.Publisher");

#region OptionsLoading
var section = builder.Configuration.GetSection("AzureServiceBus");
var topologyOptions = section.GetSection("Topology").Get<TopologyOptions>()!;
var topology = TopicTopology.FromOptions(topologyOptions);
endpointConfiguration.UseTransport(new AzureServiceBusTransport(section["ConnectionString"]!, topology));
#endregion

endpointConfiguration.UseSerialization<SystemJsonSerializer>();
endpointConfiguration.DefineCriticalErrorAction(OnCriticalError);
endpointConfiguration.EnableInstallers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public static void AddAzureServiceBusTopology(
this IServiceCollection services,
IConfiguration configuration)
{
#region OptionsValidation
services.AddSingleton<IValidateOptions<TopologyOptions>, TopologyOptionsValidator>();
services.AddOptions<TopologyOptions>().Bind(configuration.GetSection("AzureServiceBus:Topology")).ValidateOnStart();
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
}
}
},
// startcode PublisherAppsettings
"AzureServiceBus": {
"ConnectionString": "Endpoint=sb://...;SharedAccessKeyName=...;SharedAccessKey=...",
"Topology": {
Expand All @@ -20,4 +21,5 @@
}
}
}
// endcode
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
}
}
},
// startcode SubscriberAppsettings
"AzureServiceBus": {
"ConnectionString": "Endpoint=sb://...;SharedAccessKeyName=...;SharedAccessKey=...",
"Topology": {
Expand All @@ -20,4 +21,5 @@
}
}
}
// endcode
}
26 changes: 22 additions & 4 deletions samples/azure-service-bus-netstandard/options/sample.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,32 @@ include: asb-connectionstring-xplat

The sample contains three executable projects:

* `Publisher`: an NServiceBus endpoint that publishes `EventOne` to topic `event-one` and `EventTwo` to `event-two`.
* `Subscriber`: an NServiceBus endpoint subscribing to the `EventOne` and `EventTwo` event published by the `Publisher`.
* `Publisher`: an NServiceBus endpoint that publishes `EventOne` to topic `event-one` and `EventTwo` to `event-two`.
* `Subscriber`: an NServiceBus endpoint subscribing to the `EventOne` and `EventTwo` event published by the `Publisher`.

### Configuration from options

With the generic hosts ability to load configuration sections it is a matter of loading the topology options from the section in the application settings as shown below:

Snippet: OptionsLoading

In this example the publisher overrides the default topic destination to a custom conventions instead of using the default fullname of the event type:

Snippet: PublisherAppsettings

The subscriber needs to override the subscription mapping accordingly:

Snippet: SubscriberAppsettings

### Validation

The transport provides integration with Microsoft.Extensions.Options and has a built-in options validator. With the generic host it is possible to register the validator to make sure the configuration loaded fulfills the requirements of the broker (e.g. topic name length) and is self-consistent.

Snippet: OptionsValidation

## Running the sample

1. First, run the `Subscriber` project by itself. This will create all the necessary publish/subscribe infrastructure in Azure Service Bus.
2. Run the projects normally so that all endpoints start.
3. The `Publisher` endpoint continuously published two events with a short pause in between.
* The endpoint in the `Subscriber` window will receive both `EventOne` and `EventTwo`.

TBD

0 comments on commit 700c416

Please sign in to comment.