|
| 1 | +using Messages; |
| 2 | +using Microsoft.Extensions.Configuration; |
| 3 | +using Microsoft.Extensions.Hosting; |
| 4 | +using NServiceBus; |
| 5 | +using System; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +public class Configuration |
| 9 | +{ |
| 10 | + public async Task GenericHost() |
| 11 | + { |
| 12 | + #region generic-host |
| 13 | + |
| 14 | + await Host.CreateDefaultBuilder() |
| 15 | + .UseNServiceBusBridge(bridgeConfiguration => |
| 16 | + { |
| 17 | + // Configure the bridge |
| 18 | + }) |
| 19 | + .Build() |
| 20 | + .RunAsync(); |
| 21 | + |
| 22 | + #endregion |
| 23 | + } |
| 24 | + |
| 25 | + public async Task GenericHostBuilderContext() |
| 26 | + { |
| 27 | + #region generic-host-builder-context |
| 28 | + |
| 29 | + await Host.CreateDefaultBuilder() |
| 30 | + .UseNServiceBusBridge((hostBuilderContext, bridgeConfiguration) => |
| 31 | + { |
| 32 | + var connectionString = hostBuilderContext.Configuration.GetValue<string>("MyBridge:AzureServiceBusConnectionString"); |
| 33 | + var concurrency = hostBuilderContext.Configuration.GetValue<int>("MyBridge:Concurrency"); |
| 34 | + |
| 35 | + var transport = new BridgeTransport(new AzureServiceBusTransport(connectionString)) |
| 36 | + { |
| 37 | + Concurrency = concurrency |
| 38 | + }; |
| 39 | + |
| 40 | + bridgeConfiguration.AddTransport(transport); |
| 41 | + |
| 42 | + // more configuration... |
| 43 | + }) |
| 44 | + .Build() |
| 45 | + .RunAsync(); |
| 46 | + |
| 47 | + #endregion |
| 48 | + } |
| 49 | + |
| 50 | + public async Task EndpointRegistration() |
| 51 | + { |
| 52 | + #region endpoint-registration |
| 53 | + |
| 54 | + await Host.CreateDefaultBuilder() |
| 55 | + .UseNServiceBusBridge((ctx, bridgeConfiguration) => |
| 56 | + { |
| 57 | + var msmq = new BridgeTransport(new MsmqTransport()); |
| 58 | + msmq.HasEndpoint("Sales"); |
| 59 | + msmq.HasEndpoint("Shipping"); |
| 60 | + |
| 61 | + var asb = new BridgeTransport(new AzureServiceBusTransport(connectionString)); |
| 62 | + asb.HasEndpoint("Finance.Invoicing"); |
| 63 | + asb.HasEndpoint("Finance.Billing"); |
| 64 | + |
| 65 | + bridgeConfiguration.AddTransport(msmq); |
| 66 | + bridgeConfiguration.AddTransport(asb); |
| 67 | + }) |
| 68 | + .Build() |
| 69 | + .RunAsync(); |
| 70 | + |
| 71 | + #endregion |
| 72 | + } |
| 73 | + |
| 74 | + public void RegisterPublishers() |
| 75 | + { |
| 76 | + #region register-publisher |
| 77 | + |
| 78 | + var msmq = new BridgeTransport(new MsmqTransport()); |
| 79 | + msmq.HasEndpoint("Sales"); |
| 80 | + msmq.HasEndpoint("Finance.Billing"); |
| 81 | + |
| 82 | + var asb = new BridgeTransport(new AzureServiceBusTransport(connectionString)); |
| 83 | + asb.HasEndpoint("Shipping"); |
| 84 | + |
| 85 | + var invoicing = new BridgeEndpoint("Finance.Invoicing"); |
| 86 | + invoicing.RegisterPublisher(typeof(OrderBilled), "Finance.Billing"); |
| 87 | + invoicing.RegisterPublisher<OrderShipped>("Shipping"); |
| 88 | + invoicing.RegisterPublisher("Messages.OrderPlaced", "Sales"); |
| 89 | + |
| 90 | + asb.HasEndpoint(invoicing); |
| 91 | + |
| 92 | + #endregion |
| 93 | + |
| 94 | + #region register-publisher-legacy |
| 95 | + |
| 96 | + // Type.AssemblyQualifiedName Property value |
| 97 | + invoicing.RegisterPublisher("CreditApproved, CreditScoring.Messages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "Sales"); |
| 98 | + // Type.AssemblyQualifiedName Property but trimmed without Culture and PublicKeyToken as these are ignored by the message driven pub/sub feature |
| 99 | + invoicing.RegisterPublisher("CreditApproved, CreditScoring.Messages, Version=1.0.0.0", "Sales"); |
| 100 | + |
| 101 | + |
| 102 | + #endregion |
| 103 | + } |
| 104 | + |
| 105 | + public void AutoCreateQueues() |
| 106 | + { |
| 107 | + #region auto-create-queues |
| 108 | + |
| 109 | + var msmq = new BridgeTransport(new MsmqTransport()) |
| 110 | + { |
| 111 | + AutoCreateQueues = true |
| 112 | + }; |
| 113 | + |
| 114 | + var azureServiceBus = new BridgeTransport(new AzureServiceBusTransport(connectionString)) |
| 115 | + { |
| 116 | + AutoCreateQueues = true |
| 117 | + }; |
| 118 | + |
| 119 | + #endregion |
| 120 | + |
| 121 | + #region auto-create-queues-proxies |
| 122 | + |
| 123 | + msmq.HasEndpoint("Sales"); |
| 124 | + azureServiceBus.HasEndpoint("Billing"); |
| 125 | + |
| 126 | + #endregion |
| 127 | + } |
| 128 | + |
| 129 | + public void CustomConcurrency() |
| 130 | + { |
| 131 | + #region custom-concurrency |
| 132 | + |
| 133 | + var msmq = new BridgeTransport(new MsmqTransport()) |
| 134 | + { |
| 135 | + Concurrency = 10 |
| 136 | + }; |
| 137 | + |
| 138 | + var azureServiceBus = new BridgeTransport(new AzureServiceBusTransport(connectionString)) |
| 139 | + { |
| 140 | + Concurrency = 5 |
| 141 | + }; |
| 142 | + |
| 143 | + #endregion |
| 144 | + } |
| 145 | + |
| 146 | + public void CustomErrorQueue() |
| 147 | + { |
| 148 | + #region custom-error-queue |
| 149 | + |
| 150 | + var msmq = new BridgeTransport(new MsmqTransport()) |
| 151 | + { |
| 152 | + ErrorQueue = "my-msmq-bridge-error-queue" |
| 153 | + }; |
| 154 | + |
| 155 | + var azureServiceBus = new BridgeTransport(new AzureServiceBusTransport(connectionString)) |
| 156 | + { |
| 157 | + ErrorQueue = "my-asb-bridge-error-queue" |
| 158 | + }; |
| 159 | + |
| 160 | + #endregion |
| 161 | + } |
| 162 | + |
| 163 | + public void CustomTransportName() |
| 164 | + { |
| 165 | + #region custom-transport-name |
| 166 | + |
| 167 | + var azureServiceBus1 = new BridgeTransport(new AzureServiceBusTransport(connectionStringNamepace1)) |
| 168 | + { |
| 169 | + Name = "asb-namespace-1" |
| 170 | + }; |
| 171 | + |
| 172 | + var azureServiceBus2 = new BridgeTransport(new AzureServiceBusTransport(connectionStringNamepace2)) |
| 173 | + { |
| 174 | + Name = "asb-namespace-2" |
| 175 | + }; |
| 176 | + |
| 177 | + #endregion |
| 178 | + } |
| 179 | + |
| 180 | + public void PlatformBridging() |
| 181 | + { |
| 182 | + #region platform-bridging |
| 183 | + |
| 184 | + var transportWhereServiceControlIsInstalled = new BridgeTransport(new MsmqTransport()); |
| 185 | + |
| 186 | + transportWhereServiceControlIsInstalled.HasEndpoint("Particular.ServiceControl"); |
| 187 | + transportWhereServiceControlIsInstalled.HasEndpoint("Particular.Monitoring"); |
| 188 | + transportWhereServiceControlIsInstalled.HasEndpoint("error"); |
| 189 | + transportWhereServiceControlIsInstalled.HasEndpoint("audit"); |
| 190 | + |
| 191 | + #endregion |
| 192 | + } |
| 193 | + |
| 194 | + public void QueueName() |
| 195 | + { |
| 196 | + #region custom-address |
| 197 | + |
| 198 | + var transport = new BridgeTransport(new MsmqTransport()); |
| 199 | + transport.HasEndpoint("Finance", "finance@machinename"); |
| 200 | + |
| 201 | + var endpoint = new BridgeEndpoint("Sales", "sales@another-machine"); |
| 202 | + transport.HasEndpoint(endpoint); |
| 203 | + |
| 204 | + #endregion |
| 205 | + } |
| 206 | + |
| 207 | + public void DoNotEnforceBestPractices() |
| 208 | + { |
| 209 | + var bridgeConfiguration = new BridgeConfiguration(); |
| 210 | + |
| 211 | + #region do-not-enforce-best-practices |
| 212 | + |
| 213 | + bridgeConfiguration.DoNotEnforceBestPractices(); |
| 214 | + |
| 215 | + #endregion |
| 216 | + } |
| 217 | + |
| 218 | + public void ConfigureHeartbeats() |
| 219 | + { |
| 220 | + var bridgeTransport = new BridgeTransport(new AzureServiceBusTransport(connectionString)); |
| 221 | + |
| 222 | + #region configure-heartbeats |
| 223 | + |
| 224 | + bridgeTransport.SendHeartbeatTo( |
| 225 | + serviceControlQueue: "ServiceControl_Queue", |
| 226 | + frequency: TimeSpan.FromSeconds(15), |
| 227 | + timeToLive: TimeSpan.FromSeconds(30)); |
| 228 | + |
| 229 | + #endregion |
| 230 | + } |
| 231 | + |
| 232 | + public void ConfigureCustomChecks() |
| 233 | + { |
| 234 | + var bridgeTransport = new BridgeTransport(new AzureServiceBusTransport(connectionString)); |
| 235 | + |
| 236 | + #region configure-custom-checks |
| 237 | + |
| 238 | + bridgeTransport.ReportCustomChecksTo( |
| 239 | + serviceControlQueue: "ServiceControl_Queue", |
| 240 | + timeToLive: TimeSpan.FromSeconds(30)); |
| 241 | + |
| 242 | + #endregion |
| 243 | + } |
| 244 | + |
| 245 | + public void DoNotTranslateReplyToAddressForFailedMessages() |
| 246 | + { |
| 247 | + var bridgeConfiguration = new BridgeConfiguration(); |
| 248 | + |
| 249 | + #region do-not-translate-reply-to-address-for-failed-messages |
| 250 | + |
| 251 | + bridgeConfiguration.DoNotTranslateReplyToAddressForFailedMessages(); |
| 252 | + |
| 253 | + #endregion |
| 254 | + } |
| 255 | + |
| 256 | + string connectionString = string.Empty; |
| 257 | + string connectionStringNamepace1 = string.Empty; |
| 258 | + string connectionStringNamepace2 = string.Empty; |
| 259 | +} |
0 commit comments