private void CreateConnectionData(IConfigurationProvider configurationProvider) { string serviceBusConnectionString = configurationProvider.GetValue("serviceBusConnectionString"); if (string.IsNullOrWhiteSpace(serviceBusConnectionString)) { throw new ConfigurationErrorsException( "Configuration parameter 'serviceBusConnectionString' must be set to a valid Service Bus connection string"); } string eventHubName = configurationProvider.GetValue("eventHubName"); if (string.IsNullOrWhiteSpace(eventHubName)) { throw new ConfigurationErrorsException("Configuration parameter 'eventHubName' must not be empty"); } this.connectionData = new EventHubConnectionData(); this.connectionData.EventHubName = eventHubName; ServiceBusConnectionStringBuilder connStringBuilder = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); connStringBuilder.TransportType = TransportType.Amqp; this.connectionData.MessagingFactories = new MessagingFactory[ConcurrentConnections]; for (uint i = 0; i < ConcurrentConnections; i++) { this.connectionData.MessagingFactories[i] = MessagingFactory.CreateFromConnectionString(connStringBuilder.ToString()); } }
static void Main(string[] args) { var builder = new ServiceBusConnectionStringBuilder(""); builder.TransportType = TransportType.Amqp; var factory = MessagingFactory.CreateFromConnectionString(builder.ToString()); var client = factory.CreateEventHubClient("iotsample"); Console.WriteLine("Hit enter to send a batch of messages, x to exit"); var x = Console.ReadLine(); while (x != "x") { var random = new Random(); var messages = new List<EventData>(); for (int i = 0; i < 1000; i++) { var message = new { deviceId = random.Next(0, 1) }; var body = JsonConvert.SerializeObject(message); messages.Add(new EventData(Encoding.UTF8.GetBytes(body))); } client.SendBatch(messages); Console.WriteLine("Batch sent, hit enter to send the next batch, x to exit"); x = Console.ReadLine(); } }
public async Task<string> OpenAsync(CancellationToken cancellationToken) { var builder = new ServiceBusConnectionStringBuilder(_connectionString) { TransportType = TransportType.Amqp }; _messagingFactory = MessagingFactory.CreateFromConnectionString(builder.ToString()); _eventHubClient = _messagingFactory.CreateEventHubClient(_eventHubName); _consumerGroup = !string.IsNullOrEmpty(_consumerGroupName) ? _eventHubClient.GetConsumerGroup(_consumerGroupName) : _eventHubClient.GetDefaultConsumerGroup(); _eventProcessorFactory = new EventProcessorFactory(); _leaseRepository = new ReliableStateLeaseRepository(_reliableStateManager); _checkpointManager = new CheckpointManager(_leaseRepository); var allocatedPartitions = await new EventHubPartitionPartitionAllocationStrategy(_serviceName, _partitionId) .AllocateAsync(_eventHubClient, new FabricClient()); foreach (var partition in allocatedPartitions) { var lease = await _leaseRepository.GetOrCreateAsync(_connectionString, _consumerGroupName, _eventHubName, partition); await _consumerGroup.RegisterProcessorFactoryAsync(lease, _checkpointManager, _eventProcessorFactory); } return string.Concat(_eventHubName, " @ ", _connectionString); }
static void Main(string[] args) { _serverFqdn = System.Net.Dns.GetHostEntry(string.Empty).HostName; var connectionStringBuilder = new ServiceBusConnectionStringBuilder { ManagementPort = HttpPort, RuntimePort = TcpPort }; connectionStringBuilder.Endpoints.Add(new UriBuilder() { Scheme = "sb", Host = _serverFqdn, Path = ServiceNamespace }.Uri); connectionStringBuilder.StsEndpoints.Add(new UriBuilder() { Scheme = "https", Host = _serverFqdn, Port = HttpPort, Path = ServiceNamespace }.Uri); var messageFactory = MessagingFactory.CreateFromConnectionString(connectionStringBuilder.ToString()); var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionStringBuilder.ToString()); if (namespaceManager == null) { Console.WriteLine("\nUnexpected Error"); return; } if (!namespaceManager.QueueExists(QueueName)) { Console.WriteLine(@"Error: TALServiceBusQueue Queue des not Exist... Start your Queue first"); return; } QueueClient queueClient = messageFactory.CreateQueueClient(QueueName); while (true) { try { // Receive the message from the queue BrokeredMessage receivedMessage = queueClient.Receive(); if (receivedMessage != null) { Console.WriteLine(@"Message received: {0}", receivedMessage.GetBody<ServiceBusEventMessage>()); receivedMessage.Complete(); } else { Console.WriteLine(@"No new messages in the queue"); Thread.Sleep(1000); } } catch (Exception e) { Console.WriteLine("Unexpected exception {0}", e); throw; } } if (messageFactory != null) { messageFactory.Close(); } // Console.WriteLine("Press ENTER to clean up and exit."); // Console.ReadLine(); }
public EventPublisher(string connectionString, string eventHubName, string partitionKey, ILogger logger) { var builder = new ServiceBusConnectionStringBuilder(connectionString) { TransportType = TransportType.Amqp }; _partitionKey = partitionKey; _client = EventHubClient.CreateFromConnectionString(builder.ToString(), eventHubName); _logger = logger; }
static void Main(string[] args) { _serverFqdn = System.Net.Dns.GetHostEntry(string.Empty).HostName; var connectionStringBuilder = new ServiceBusConnectionStringBuilder {ManagementPort = HttpPort, RuntimePort = TcpPort}; connectionStringBuilder.Endpoints.Add(new UriBuilder() { Scheme = "sb", Host = _serverFqdn, Path = ServiceNamespace }.Uri); connectionStringBuilder.StsEndpoints.Add(new UriBuilder() { Scheme = "https", Host = _serverFqdn, Port = HttpPort, Path = ServiceNamespace }.Uri); var messageFactory = MessagingFactory.CreateFromConnectionString(connectionStringBuilder.ToString()); var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionStringBuilder.ToString()); if (namespaceManager == null) { Console.WriteLine("\nUnexpected Error"); return; } if (namespaceManager.QueueExists(QueueName)) { namespaceManager.DeleteQueue(QueueName); } namespaceManager.CreateQueue(QueueName); QueueClient queueClient = messageFactory.CreateQueueClient(QueueName); DisplayHeader(); var userInput = string.Empty; while (!ShouldExit(userInput)) { try { userInput = Console.ReadLine(); var numberOfMessages = userInput; var messagesList = GetListOfMessages(numberOfMessages); var startTime = DateTime.Now; SendMessages(messagesList, queueClient); var sendingTime = (DateTime.Now - startTime).TotalSeconds; Console.WriteLine(string.Format("\n{0} Messages Sent.. [Total Time: {1} Secs]", messagesList.Count(), sendingTime)); } catch (Exception e) { Console.WriteLine("Unexpected exception {0}", e.ToString()); throw; } } if (messageFactory != null) { messageFactory.Close(); } }
private static string GetServiceBusConnectionString() { string connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"]; if (string.IsNullOrEmpty(connectionString)) { Console.WriteLine("Did not find Service Bus connections string in appsettings (app.config)"); return string.Empty; } ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString); builder.TransportType = TransportType.Amqp; return builder.ToString(); }
public void SetUp() { var serverFQDN = Environment.MachineName; const int HttpPort = 9355; const int TcpPort = 9354; const string ServiceNamespace = "ServiceBusDefaultNamespace"; var connBuilder = new ServiceBusConnectionStringBuilder { ManagementPort = HttpPort, RuntimePort = TcpPort }; connBuilder.Endpoints.Add(new UriBuilder { Scheme = "sb", Host = serverFQDN, Path = ServiceNamespace }.Uri); connBuilder.StsEndpoints.Add(new UriBuilder { Scheme = "https", Host = serverFQDN, Port = HttpPort, Path = ServiceNamespace }.Uri); _testBus = Bus.CreateBus(cfg => cfg.WithConnectionString(connBuilder.ToString()).WithLogger(new TestLogger())).Result; }
private string GetServiceBusConnectionString() { string connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"]; if (string.IsNullOrEmpty(connectionString)) { return string.Empty; } ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString); builder.TransportType = TransportType.Amqp; return builder.ToString(); }
/// <summary> /// Initializes a new instance of the <see cref="LargePublishPerformance"/> class. /// </summary> public LargePublishPerformance() { var serverFQDN = Environment.MachineName; const int HttpPort = 9355; const int TcpPort = 9354; const string ServiceNamespace = "ServiceBusDefaultNamespace"; // Common.Logging.LogManager.Adapter = new Common.Logging.NLog.NLogLoggerFactoryAdapter(new NameValueCollection { { "configType", "FILE" }, { "configFile", "~/NLog.config" } }); var connBuilder = new ServiceBusConnectionStringBuilder { ManagementPort = HttpPort, RuntimePort = TcpPort }; connBuilder.Endpoints.Add(new UriBuilder { Scheme = "sb", Host = serverFQDN, Path = ServiceNamespace }.Uri); connBuilder.StsEndpoints.Add(new UriBuilder { Scheme = "https", Host = serverFQDN, Port = HttpPort, Path = ServiceNamespace }.Uri); _bus = Bus.CreateBus(cfg => cfg.WithConnectionString(connBuilder.ToString())).Result; }
private static string GetServiceBusConnectionString(string activityType) { string appSettingsKey = string.Format("ServiceBus{0}", activityType); string connectionString = ConfigurationManager.AppSettings[appSettingsKey]; if (string.IsNullOrEmpty(connectionString)) { Console.WriteLine("Did not find Service Bus connections string in appsettings (app.config)"); return string.Empty; } ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString); builder.TransportType = TransportType.Amqp; return builder.ToString(); }
public void SetUp() { var serverFQDN = Environment.MachineName; const int HttpPort = 9355; const int TcpPort = 9354; const string ServiceNamespace = "ServiceBusDefaultNamespace"; var connBuilder = new ServiceBusConnectionStringBuilder { ManagementPort = HttpPort, RuntimePort = TcpPort }; connBuilder.OperationTimeout = TimeSpan.FromSeconds(10); connBuilder.Endpoints.Add(new UriBuilder { Scheme = "sb", Host = serverFQDN, Path = ServiceNamespace }.Uri); connBuilder.StsEndpoints.Add(new UriBuilder { Scheme = "https", Host = serverFQDN, Port = HttpPort, Path = ServiceNamespace }.Uri); _testBus = Bus.CreateBus(cfg => cfg.WithConnectionString(connBuilder.ToString()).WithLogger(new TestLogger())).Result; // _testBus = Bus.CreateBus(cfg => cfg.WithConnectionString(@"Endpoint=sb://funnelfire-test.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=ENUAYOiS9Lt3tDngyClvjzRyls5UkS8ie7aLAyjBV0s=;OperationTimeout=00:00:10").WithLogger(new TestLogger())).Result; }
public void Build() { var connectionBuilder = new ServiceBusConnectionStringBuilder(); connectionBuilder.ManagementPort = HttpPort; connectionBuilder.RuntimePort = TcpPort; // Service Bus end-point connectionBuilder.Endpoints.Add(new Uri(ServiceBusEndPont)); // STS End-point connectionBuilder.StsEndpoints.Add(new Uri(SecureTokenServiceEndPoint)); ConnectionString = connectionBuilder.ToString(); }
public async Task Run(string connectionString) { var connectionStringBuilder = new ServiceBusConnectionStringBuilder(connectionString); var listenAddress = new UriBuilder(connectionStringBuilder.GetAbsoluteRuntimeEndpoints()[0]) { Path = connectionStringBuilder.EntityPath ?? "relay" }.ToString(); TokenProvider tokenProvider = null; if (connectionStringBuilder.SharedAccessKeyName != null && connectionStringBuilder.SharedAccessKey != null) { tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(connectionStringBuilder.SharedAccessKeyName, connectionStringBuilder.SharedAccessKey); } else if (connectionStringBuilder.SharedAccessSignature != null) { tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(connectionStringBuilder.SharedAccessSignature); } else if (connectionStringBuilder.SharedSecretIssuerName != null && connectionStringBuilder.SharedSecretIssuerSecret != null) { tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(connectionStringBuilder.SharedSecretIssuerName, connectionStringBuilder.SharedSecretIssuerSecret); } if (tokenProvider != null) { using (var host = new ServiceHost(this)) { host.AddServiceEndpoint( GetType(), new NetTcpRelayBinding {IsDynamic = true}, listenAddress) .EndpointBehaviors.Add( new TransportClientEndpointBehavior(tokenProvider)); host.Open(); Console.WriteLine("Service listening at address {0}", listenAddress); Console.WriteLine("Press [Enter] to close the listener and exit."); Console.ReadLine(); host.Close(); } } }
static void Main(string[] args) { var connBuilder = new ServiceBusConnectionStringBuilder(); connBuilder.ManagementPort = HttpPort; connBuilder.RuntimePort = TcpPort; connBuilder.Endpoints.Add(new Uri("sb://Niranga/ServiceBusDefaultNamespace")); connBuilder.StsEndpoints.Add(new Uri("https://Niranga:9355/ServiceBusDefaultNamespace")); var messagingFactory = MessagingFactory.CreateFromConnectionString(connBuilder.ToString()); var namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString()); if (namespaceManager == null) { Console.WriteLine("Error"); return; } string queueName = "ServiceBusQueueSample"; if (namespaceManager.QueueExists(queueName)) { namespaceManager.DeleteQueue(queueName); } namespaceManager.CreateQueue(queueName); var queueClient = messagingFactory.CreateQueueClient(queueName); // Send var brokeredMessage = new BrokeredMessage("Hello"); queueClient.Send(brokeredMessage); // Receive var receiveMessage = queueClient.Receive(TimeSpan.FromSeconds(5)); if (receiveMessage != null) { Console.WriteLine("Received '{0}'", receiveMessage.GetBody<string>()); receiveMessage.Complete(); } if (messagingFactory != null) { messagingFactory.Close(); } }
public void InitializeEventHub() { Context.Logger.Info("Initializing EventHubClient and EventHubSender..."); var builder = new ServiceBusConnectionStringBuilder(); builder.Endpoints.Add(new Uri("sb://" + this.appConfig.EventHubNamespace + "." + this.appConfig.EventHubFqnAddress)); builder.EntityPath = this.appConfig.EventHubEntityPath; builder.SharedAccessKeyName = this.appConfig.EventHubSharedAccessKeyName; builder.SharedAccessKey = this.appConfig.EventHubSharedAccessKey; builder.TransportType = TransportType.Amqp; Context.Logger.Info("EventHubWriter: ConnectionString = {0} ParitionId = {1}", builder.ToString(), Context.TopologyContext.GetThisTaskIndex()); eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString()); //TODO: Implement a distribution strategy of partitions in case number of bolt tasks is less than partitions in EventHub eventHubSender = eventHubClient.CreatePartitionedSender(Context.TopologyContext.GetThisTaskIndex().ToString()); }
public async Task Run(string connectionString) { var connectionStringBuilder = new ServiceBusConnectionStringBuilder(connectionString); var sendAddress = new UriBuilder(connectionStringBuilder.GetAbsoluteRuntimeEndpoints()[0]) { Path = connectionStringBuilder.EntityPath ?? "relay" }.ToString(); TokenProvider tokenProvider = null; if (connectionStringBuilder.SharedAccessKeyName != null && connectionStringBuilder.SharedAccessKey != null) { tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(connectionStringBuilder.SharedAccessKeyName, connectionStringBuilder.SharedAccessKey); } else if (connectionStringBuilder.SharedAccessSignature != null) { tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(connectionStringBuilder.SharedAccessSignature); } else if (connectionStringBuilder.SharedSecretIssuerName != null && connectionStringBuilder.SharedSecretIssuerSecret != null) { tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(connectionStringBuilder.SharedSecretIssuerName, connectionStringBuilder.SharedSecretIssuerSecret); } if (tokenProvider != null) { var cf = new ChannelFactory<IClient>( new NetTcpRelayBinding {IsDynamic = true}, sendAddress); cf.Endpoint.EndpointBehaviors.Add(new TransportClientEndpointBehavior(tokenProvider)); using (var client = cf.CreateChannel()) { for (int i = 1; i <= 25; i++) { var result = await client.Echo(DateTime.UtcNow.ToString()); Console.WriteLine("Round {0}, Echo: {1}", i, result); } client.Close(); } } Console.WriteLine("Press [Enter] to exit."); Console.ReadLine(); }
private static async void CreateHashForDevice(string deviceName, string eventHubName, string serviceBus) { string serviceBusConnectionString = Constants.GetBusConnectionString(serviceBus, "configure", Constants.ConsumerHash); var connectionString = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); var namespaceManager = NamespaceManager.CreateFromConnectionString(serviceBusConnectionString); var ehd = await namespaceManager.GetEventHubAsync(eventHubName); // Create a customer SAS rule with Manage permissions ehd.UserMetadata = deviceName; string ruleName = deviceName; string ruleKey = SharedAccessAuthorizationRule.GenerateRandomKey(); ehd.Authorization.Add(new SharedAccessAuthorizationRule(ruleName, ruleKey, new AccessRights[] { AccessRights.Send })); namespaceManager.UpdateEventHubAsync(ehd).Wait(); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(String.Format("{0} => {1}", deviceName, ruleKey)); }
private void button1_Click(object sender, EventArgs e) { string EventHubName = eventhubnametext.Text; string serviceBusConnectionString = sbconnstringtext.Text; ServiceBusConnectionStringBuilder connectionString = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); string ServiceBusNamespace = connectionString.Endpoints.First().Host; string namespaceKeyName = connectionString.SharedAccessKeyName; string namespaceKey = connectionString.SharedAccessKey; string baseAddressHttp = "http://" + ServiceBusNamespace + "/"; string eventHubAddress = baseAddressHttp + EventHubName; var sas = CreateSasToken(ServiceBusNamespace, namespaceKeyName, namespaceKey); sastokentext.Text = sas.ToString(); }
public Task OpenEventHubAsync(string eventHubConnectionString, string hubName) { return Task.Factory.StartNew(async () => { if (string.IsNullOrWhiteSpace(eventHubConnectionString)) throw new ArgumentException("invalid event hub connection string"); if (string.IsNullOrWhiteSpace(hubName)) throw new ArgumentException("invalid hubname"); this.IsOpen = true; var builder = new ServiceBusConnectionStringBuilder(eventHubConnectionString) { TransportType = TransportType.Amqp }; var s = builder.ToString(); _factory = MessagingFactory.CreateFromConnectionString(builder.ToString()); _client = _factory.CreateEventHubClient(hubName); var runtimeInfo = await _client.GetRuntimeInformationAsync(); _partitions.Clear(); foreach (var p in runtimeInfo.PartitionIds) { var partition = await _client.GetPartitionRuntimeInformationAsync(p); var count = partition.LastEnqueuedSequenceNumber - partition.BeginSequenceNumber; if (count != 0) count++; this.MessageCount = count; _partitions.Add(new Partition(p, _messages)); _foundPartitions.OnNext(partition); } _foundPartitions.OnCompleted(); }); }
public void Run(SetEventOnRampMessageReceived setEventOnRampMessageReceived) { try { // Assign the delegate SetEventOnRampMessageReceived = setEventOnRampMessageReceived; // Load vars var eventHubConnectionString = ConfigurationBag.Configuration.AzureNameSpaceConnectionString; var eventHubName = ConfigurationBag.Configuration.GroupEventHubsName; LogEngine.WriteLog( ConfigurationBag.EngineName, $"Event Hubs transfort Type: {ConfigurationBag.Configuration.ServiceBusConnectivityMode}", Constant.LogLevelError, Constant.TaskCategoriesError, null, Constant.LogLevelInformation); var builder = new ServiceBusConnectionStringBuilder(eventHubConnectionString) { TransportType = TransportType.Amqp }; //If not exit it create one, drop brachets because Azure rules var eventHubConsumerGroup = string.Concat(ConfigurationBag.EngineName, "_", ConfigurationBag.Configuration.ChannelId) .Replace("{", "") .Replace("}", "") .Replace("-", ""); var nsManager = NamespaceManager.CreateFromConnectionString(builder.ToString()); Debug.WriteLine($"Initializing Group Name {eventHubConsumerGroup}"); Debug.WriteLine("Start DirectRegisterEventReceiving."); // Create Event Hubs var eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString(), eventHubName); // Create consumer nsManager.CreateConsumerGroupIfNotExists(eventHubName, eventHubConsumerGroup); var namespaceManager = NamespaceManager.CreateFromConnectionString(builder.ToString()); var ehDescription = namespaceManager.GetEventHub(eventHubName); // Use the default consumer group foreach (var partitionId in ehDescription.PartitionIds) { var myNewThread = new Thread(() => ReceiveDirectFromPartition(eventHubClient, partitionId, eventHubConsumerGroup)); myNewThread.Start(); } Debug.WriteLine( "After DirectRegisterEventReceiving Downstream running."); } catch (Exception ex) { LogEngine.WriteLog( ConfigurationBag.EngineName, $"Error in {MethodBase.GetCurrentMethod().Name} - Hint: Check if the firewall outbound port 5671 is opened.", Constant.LogLevelError, Constant.TaskCategoriesEventHubs, ex, Constant.LogLevelError); } }
public IServiceProvider ConfigureServices(IServiceCollection services) { RegisterAppInsights(services); // Add framework services. services.AddMvc(options => { options.Filters.Add(typeof(HttpGlobalExceptionFilter)); }).AddControllersAsServices(); //Injecting Controllers themselves thru DI //For further info see: http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services services.AddTransient <IOrderingIntegrationEventService, OrderingIntegrationEventService>(); services.AddHealthChecks(checks => { var minutes = 1; if (int.TryParse(Configuration["HealthCheck:Timeout"], out var minutesParsed)) { minutes = minutesParsed; } checks.AddSqlCheck("OrderingDb", Configuration["ConnectionString"], TimeSpan.FromMinutes(minutes)); }); services.AddEntityFrameworkSqlServer() .AddDbContext <OrderingContext>(options => { options.UseSqlServer(Configuration["ConnectionString"], sqlServerOptionsAction: sqlOptions => { sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name); sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null); }); }, ServiceLifetime.Scoped //Showing explicitly that the DbContext is shared across the HTTP request scope (graph of objects started in the HTTP request) ); services.AddDbContext <IntegrationEventLogContext>(options => { options.UseSqlServer(Configuration["ConnectionString"], sqlServerOptionsAction: sqlOptions => { sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name); //Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null); }); }); services.Configure <OrderSettings>(Configuration); services.AddSwaggerGen(options => { options.DescribeAllEnumsAsStrings(); options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "Ordering HTTP API", Version = "v1", Description = "The Ordering Service HTTP API", TermsOfService = "Terms Of Service" }); options.AddSecurityDefinition("oauth2", new OAuth2Scheme { Type = "oauth2", Flow = "implicit", AuthorizationUrl = $"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize", TokenUrl = $"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/token", Scopes = new Dictionary <string, string>() { { "orders", "Ordering API" } } }); options.OperationFilter <AuthorizeCheckOperationFilter>(); }); services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); }); // Add application services. services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddTransient <IIdentityService, IdentityService>(); services.AddTransient <Func <DbConnection, IIntegrationEventLogService> >( sp => (DbConnection c) => new IntegrationEventLogService(c)); services.AddTransient <IOrderingIntegrationEventService, OrderingIntegrationEventService>(); if (Configuration.GetValue <bool>("AzureServiceBusEnabled")) { services.AddSingleton <IServiceBusPersisterConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultServiceBusPersisterConnection> >(); var serviceBusConnectionString = Configuration["EventBusConnection"]; var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); return(new DefaultServiceBusPersisterConnection(serviceBusConnection)); }); } else { services.AddSingleton <IRabbitMqPersistentConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultRabbitMqPersistentConnection> >(); var factory = new ConnectionFactory() { HostName = Configuration["EventBusConnection"] }; if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) { factory.UserName = Configuration["EventBusUserName"]; } if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) { factory.Password = Configuration["EventBusPassword"]; } var retryCount = 5; if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) { retryCount = int.Parse(Configuration["EventBusRetryCount"]); } return(new DefaultRabbitMqPersistentConnection(factory, logger, retryCount)); }); } RegisterEventBus(services); ConfigureAuthService(services); services.AddOptions(); //configure autofac var container = new ContainerBuilder(); container.Populate(services); container.RegisterModule(new MediatorModule()); container.RegisterModule(new ApplicationModule(Configuration["ConnectionString"])); return(new AutofacServiceProvider(container.Build())); }
void ConnectionStringBuilderShouldDefaultToAmqp() { var csBuilder = new ServiceBusConnectionStringBuilder("Endpoint=sb://contoso.servicebus.windows.net;SharedAccessKeyName=keyname;SharedAccessKey=key"); Assert.Equal(TransportType.Amqp, csBuilder.TransportType); }
public ServiceBusPersisterConnection(ServiceBusConnectionStringBuilder serviceBusConnectionStringBuilder) { ServiceBusConnectionStringBuilder = serviceBusConnectionStringBuilder; _topicClient = new TopicClient(ServiceBusConnectionStringBuilder, RetryPolicy.Default); }
private async Task StartInternalAsync(OnlineTrainerSettingsInternal settings, OnlineTrainerState state = null, byte[] model = null) { this.LastStartDateTimeUtc = DateTime.UtcNow; this.perfCounters = new PerformanceCounters(settings.Metadata.ApplicationID); // setup trainer this.trainer = new Learner(settings, this.DelayedExampleCallback, this.perfCounters); if (settings.ForceFreshStart || model != null) this.trainer.FreshStart(state, model); else await this.trainer.FindAndResumeFromState(); // setup factory this.trainProcessorFactory = new TrainEventProcessorFactory(settings, this.trainer, this.perfCounters); // setup host var serviceBusConnectionStringBuilder = new ServiceBusConnectionStringBuilder(settings.JoinedEventHubConnectionString); var joinedEventhubName = serviceBusConnectionStringBuilder.EntityPath; serviceBusConnectionStringBuilder.EntityPath = string.Empty; this.eventProcessorHost = new EventProcessorHost(settings.Metadata.ApplicationID, joinedEventhubName, EventHubConsumerGroup.DefaultGroupName, serviceBusConnectionStringBuilder.ToString(), settings.StorageConnectionString); await this.eventProcessorHost.RegisterEventProcessorFactoryAsync( this.trainProcessorFactory, new EventProcessorOptions { InitialOffsetProvider = this.InitialOffsetProvider }); // don't perform too often this.perfUpdater = new SafeTimer( TimeSpan.FromMilliseconds(500), this.UpdatePerformanceCounters); this.telemetry.TrackTrace( "OnlineTrainer started", SeverityLevel.Information, new Dictionary<string, string> { { "CheckpointPolicy", settings.CheckpointPolicy.ToString() }, { "VowpalWabbit", settings.Metadata.TrainArguments }, { "ExampleTracing", settings.EnableExampleTracing.ToString() } }); }
/// <summary> /// Creates a new AMQP MessageSender. /// </summary> /// <param name="connectionStringBuilder">The <see cref="ServiceBusConnectionStringBuilder"/> having entity level connection details.</param> /// <param name="retryPolicy">The <see cref="RetryPolicy"/> that will be used when communicating with Service Bus. Defaults to <see cref="RetryPolicy.Default"/></param> /// <remarks>Creates a new connection to the entity, which is opened during the first operation.</remarks> public MessageSender( ServiceBusConnectionStringBuilder connectionStringBuilder, RetryPolicy retryPolicy = null) : this(connectionStringBuilder?.GetNamespaceConnectionString(), connectionStringBuilder?.EntityPath, retryPolicy) { }
public void InitializeEventHub() { Context.Logger.Info("Current AppConfig File: " + ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)); Context.Logger.Info("Current AppSettings: " + String.Join(Environment.NewLine, ConfigurationManager.AppSettings.AllKeys)); this.EventHubNamespace = ConfigurationManager.AppSettings["EventHubNamespace"]; if (String.IsNullOrWhiteSpace(this.EventHubNamespace)) { throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubNamespace"); } this.EventHubEntityPath = ConfigurationManager.AppSettings["EventHubEntityPath"]; if (String.IsNullOrWhiteSpace(this.EventHubEntityPath)) { throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubEntityPath"); } this.EventHubSharedAccessKeyName = ConfigurationManager.AppSettings["EventHubSharedAccessKeyName"]; if (String.IsNullOrWhiteSpace(this.EventHubSharedAccessKeyName)) { throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubSharedAccessKeyName"); } this.EventHubSharedAccessKey = ConfigurationManager.AppSettings["EventHubSharedAccessKey"]; if (String.IsNullOrWhiteSpace(this.EventHubSharedAccessKey)) { throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubSharedAccessKey"); } this.EventHubPartitions = ConfigurationManager.AppSettings["EventHubPartitions"]; if (String.IsNullOrWhiteSpace(this.EventHubPartitions)) { throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubPartitions"); } var builder = new ServiceBusConnectionStringBuilder(); builder.Endpoints.Add(new Uri("sb://" + this.EventHubNamespace + "." + EventHubFqnAddress)); builder.EntityPath = this.EventHubEntityPath; builder.SharedAccessKeyName = this.EventHubSharedAccessKeyName; builder.SharedAccessKey = this.EventHubSharedAccessKey; builder.TransportType = TransportType.Amqp; var partitionCount = int.Parse(this.EventHubPartitions); TopologyContext topologyContext = Context.TopologyContext; Context.Logger.Info(this.GetType().Name + " TopologyContext info:"); Context.Logger.Info("TaskId: {0}", topologyContext.GetThisTaskId()); var taskIndex = topologyContext.GetThisTaskIndex(); Context.Logger.Info("TaskIndex: {0}", taskIndex); string componentId = topologyContext.GetThisComponentId(); Context.Logger.Info("ComponentId: {0}", componentId); List<int> componentTasks = topologyContext.GetComponentTasks(componentId); Context.Logger.Info("ComponentTasks: {0}", componentTasks.Count); if (partitionCount != componentTasks.Count) { throw new Exception( String.Format("Component task count does not match partition count. Component: {0}, Tasks: {1}, Partition: {2}", componentId, componentTasks.Count, partitionCount)); } partitionId = taskIndex.ToString(); Context.Logger.Info(this.GetType().Name + " ConnectionString = {0}, ParitionId = {1}", builder.ToString(), partitionId); eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString()); eventHubSender = eventHubClient.CreatePartitionedSender(partitionId); }
public virtual IServiceProvider ConfigureServices(IServiceCollection services) { RegisterAppInsights(services); services.AddCustomHealthCheck(Configuration); services.AddControllers(options => { options.Filters.Add(typeof(HttpGlobalExceptionFilter)); }) // Added for functional tests .AddApplicationPart(typeof(LocationsController).Assembly) .AddNewtonsoftJson(); ConfigureAuthService(services); services.Configure <LocationSettings>(Configuration); if (Configuration.GetValue <bool>("AzureServiceBusEnabled")) { services.AddSingleton <IServiceBusPersisterConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultServiceBusPersisterConnection> >(); var serviceBusConnectionString = Configuration["EventBusConnection"]; var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); return(new DefaultServiceBusPersisterConnection(serviceBusConnection, logger)); }); } else { services.AddSingleton <IRabbitMQPersistentConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >(); var factory = new ConnectionFactory() { HostName = Configuration["EventBusConnection"], DispatchConsumersAsync = true }; if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) { factory.UserName = Configuration["EventBusUserName"]; } if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) { factory.Password = Configuration["EventBusPassword"]; } var retryCount = 5; if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) { retryCount = int.Parse(Configuration["EventBusRetryCount"]); } return(new DefaultRabbitMQPersistentConnection(factory, logger, retryCount)); }); } RegisterEventBus(services); // Add framework services. services.AddSwaggerGen(options => { options.DescribeAllEnumsAsStrings(); options.SwaggerDoc("v1", new OpenApiInfo { Title = "eShopOnContainers - Location HTTP API", Version = "v1", Description = "The Location Microservice HTTP API. This is a Data-Driven/CRUD microservice sample", }); options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme { Type = SecuritySchemeType.OAuth2, Flows = new OpenApiOAuthFlows() { Implicit = new OpenApiOAuthFlow() { AuthorizationUrl = new Uri($"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize"), TokenUrl = new Uri($"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/token"), Scopes = new Dictionary <string, string>() { { "locations", "Locations API" } } } } }); options.OperationFilter <AuthorizeCheckOperationFilter>(); }); services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder .SetIsOriginAllowed((host) => true) .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); }); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddTransient <IIdentityService, IdentityService>(); services.AddTransient <ILocationsService, LocationsService>(); services.AddTransient <ILocationsRepository, LocationsRepository>(); //configure autofac var container = new ContainerBuilder(); container.Populate(services); return(new AutofacServiceProvider(container.Build())); }
public IServiceProvider ConfigureServices(IServiceCollection services) { // Add framework services. RegisterAppInsights(services); services.AddHealthChecks(checks => { var minutes = 1; if (int.TryParse(Configuration["HealthCheck:Timeout"], out var minutesParsed)) { minutes = minutesParsed; } checks.AddMySQLCheck("CatalogDb", Configuration["ConnectionString"], TimeSpan.FromMinutes(minutes)); var accountName = Configuration.GetValue <string>("AzureStorageAccountName"); var accountKey = Configuration.GetValue <string>("AzureStorageAccountKey"); if (!string.IsNullOrEmpty(accountName) && !string.IsNullOrEmpty(accountKey)) { checks.AddAzureBlobStorageCheck(accountName, accountKey); } }); services.AddMvc(options => { options.Filters.Add(typeof(HttpGlobalExceptionFilter)); }).AddControllersAsServices(); services.AddDbContext <CatalogContext>(options => { options.UseMySql(Configuration["ConnectionString"], mySqlOptionsAction: sqlOptions => { sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name); //Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null); }); // Changing default behavior when client evaluation occurs to throw. // Default in EF Core would be to log a warning when client evaluation is performed. options.ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning)); //Check Client vs. Server evaluation: https://docs.microsoft.com/en-us/ef/core/querying/client-eval }); services.AddDbContext <IntegrationEventLogContext>(options => { options.UseMySql(Configuration["ConnectionString"], mySqlOptionsAction: sqlOptions => { sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name); //Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null); }); }); services.Configure <ServiceCatalogSettings>(Configuration); // Add framework services. services.AddSwaggerGen(options => { options.DescribeAllEnumsAsStrings(); options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "Book2 - Service Catalog HTTP API", Version = "v1", Description = "The Service Catalog Microservice HTTP API. This is a Data-Driven/CRUD microservice", TermsOfService = "Terms Of Service" }); }); services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); }); services.AddTransient <Func <DbConnection, IIntegrationEventLogService> >( sp => (DbConnection c) => new IntegrationEventLogService(c)); services.AddTransient <ICatalogIntegrationEventService, CatalogIntegrationEventService>(); if (Configuration.GetValue <bool>("AzureServiceBusEnabled")) { services.AddSingleton <IServiceBusPersisterConnection>(sp => { var settings = sp.GetRequiredService <IOptions <ServiceCatalogSettings> >().Value; var logger = sp.GetRequiredService <ILogger <DefaultServiceBusPersisterConnection> >(); var serviceBusConnection = new ServiceBusConnectionStringBuilder(settings.EventBusConnection); return(new DefaultServiceBusPersisterConnection(serviceBusConnection, logger)); }); } else { services.AddSingleton <IRabbitMQPersistentConnection>(sp => { var settings = sp.GetRequiredService <IOptions <ServiceCatalogSettings> >().Value; var logger = sp.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >(); var factory = new ConnectionFactory() { HostName = Configuration["EventBusConnection"] }; if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) { factory.UserName = Configuration["EventBusUserName"]; } if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) { factory.Password = Configuration["EventBusPassword"]; } var retryCount = 5; if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) { retryCount = int.Parse(Configuration["EventBusRetryCount"]); } return(new DefaultRabbitMQPersistentConnection(factory, logger, retryCount)); }); } RegisterEventBus(services); var container = new ContainerBuilder(); container.Populate(services); return(new AutofacServiceProvider(container.Build())); }
static void Run() { var properties = new Dictionary <string, string> { { servicebusNamespace, null }, { servicebusEntityPath, null }, { servicebusFqdnSuffix, null }, { servicebusSendKey, null }, { servicebusListenKey, null }, { servicebusManageKey, null } }; // read the settings file created by the ./setup.ps1 file var settingsFile = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), samplePropertiesFileName); if (File.Exists(settingsFile)) { using (var fs = new StreamReader(settingsFile)) { while (!fs.EndOfStream) { var readLine = fs.ReadLine(); if (readLine != null) { var propl = readLine.Trim(); var cmt = propl.IndexOf('#'); if (cmt > -1) { propl = propl.Substring(0, cmt).Trim(); } if (propl.Length > 0) { var propi = propl.IndexOf('='); if (propi == -1) { continue; } var propKey = propl.Substring(0, propi - 1).Trim(); var propVal = propl.Substring(propi + 1).Trim(); if (properties.ContainsKey(propKey)) { properties[propKey] = propVal; } } } } } } // get overrides from the environment foreach (var prop in properties) { var env = Environment.GetEnvironmentVariable(prop.Key); if (env != null) { properties[prop.Key] = env; } } var hostName = properties[servicebusNamespace] + "." + properties[servicebusFqdnSuffix]; var rootUri = new UriBuilder("http", hostName, -1, "/").ToString(); var netTcpUri = new UriBuilder("sb", hostName, -1, properties[servicebusEntityPath] + "/NetTcp").ToString(); var httpUri = new UriBuilder("https", hostName, -1, properties[servicebusEntityPath] + "/Http").ToString(); var program = Activator.CreateInstance(typeof(Program)); if (program is ITcpListenerSampleUsingKeys) { ((ITcpListenerSampleUsingKeys)program).Run( netTcpUri, "samplelisten", properties[servicebusListenKey]) .GetAwaiter() .GetResult(); } else if (program is ITcpSenderSampleUsingKeys) { ((ITcpSenderSampleUsingKeys)program).Run(netTcpUri, "samplesend", properties[servicebusSendKey]) .GetAwaiter() .GetResult(); } if (program is IHttpListenerSampleUsingKeys) { ((IHttpListenerSampleUsingKeys)program).Run( httpUri, "samplelisten", properties[servicebusListenKey]) .GetAwaiter() .GetResult(); } else if (program is IHttpSenderSampleUsingKeys) { ((IHttpSenderSampleUsingKeys)program).Run(httpUri, "samplesend", properties[servicebusSendKey]) .GetAwaiter() .GetResult(); } if (program is ITcpListenerSample) { var token = TokenProvider.CreateSharedAccessSignatureTokenProvider( "samplelisten", properties[servicebusListenKey]) .GetWebTokenAsync(netTcpUri, string.Empty, true, TimeSpan.FromHours(1)).GetAwaiter().GetResult(); ((ITcpListenerSample)program).Run(netTcpUri, token).GetAwaiter().GetResult(); } else if (program is ITcpSenderSample) { var token = TokenProvider.CreateSharedAccessSignatureTokenProvider( "samplesend", properties[servicebusSendKey]) .GetWebTokenAsync(netTcpUri, string.Empty, true, TimeSpan.FromHours(1)).GetAwaiter().GetResult(); ((ITcpSenderSample)program).Run(netTcpUri, token).GetAwaiter().GetResult(); } if (program is IHttpListenerSample) { var token = TokenProvider.CreateSharedAccessSignatureTokenProvider( "samplelisten", properties[servicebusListenKey]) .GetWebTokenAsync(httpUri, string.Empty, true, TimeSpan.FromHours(1)).GetAwaiter().GetResult(); ((IHttpListenerSample)program).Run(httpUri, token).GetAwaiter().GetResult(); } else if (program is IHttpSenderSample) { var token = TokenProvider.CreateSharedAccessSignatureTokenProvider( "samplesend", properties[servicebusSendKey]) .GetWebTokenAsync(httpUri, string.Empty, true, TimeSpan.FromHours(1)).GetAwaiter().GetResult(); ((IHttpSenderSample)program).Run(httpUri, token).GetAwaiter().GetResult(); } else if (program is IDynamicSenderSample) { var token = TokenProvider.CreateSharedAccessSignatureTokenProvider( "rootsamplesend", properties[servicebusSendKey]) .GetWebTokenAsync(rootUri, string.Empty, true, TimeSpan.FromHours(1)).GetAwaiter().GetResult(); ((IDynamicSenderSample)program).Run(hostName, token).GetAwaiter().GetResult(); } else if (program is IDynamicListenerSample) { var token = TokenProvider.CreateSharedAccessSignatureTokenProvider( "rootsamplelisten", properties[servicebusListenKey]) .GetWebTokenAsync(rootUri, string.Empty, true, TimeSpan.FromHours(1)).GetAwaiter().GetResult(); ((IDynamicListenerSample)program).Run(hostName, token).GetAwaiter().GetResult(); } else if (program is IDynamicSample) { var token = TokenProvider.CreateSharedAccessSignatureTokenProvider( "rootsamplemanage", properties[servicebusManageKey]) .GetWebTokenAsync(rootUri, string.Empty, true, TimeSpan.FromHours(1)).GetAwaiter().GetResult(); ((IDynamicSample)program).Run(hostName, token).GetAwaiter().GetResult(); } else if (program is IConnectionStringSample) { var connectionString = ServiceBusConnectionStringBuilder.CreateUsingSharedAccessKey( new Uri(rootUri), "rootsamplemanage", properties[servicebusManageKey]); ((IConnectionStringSample)program).Run(connectionString).GetAwaiter().GetResult(); } }
private void btnSave_Click(object sender, EventArgs e) { try { if (string.IsNullOrWhiteSpace(txtUri.Text)) { MainForm.StaticWriteToLog("The connection string of the Service Bus namespace cannot be null."); return; } ServiceBusConnectionStringBuilder serviceBusConnectionStringBuilder; try { serviceBusConnectionStringBuilder = new ServiceBusConnectionStringBuilder(txtUri.Text); } catch (Exception) { MainForm.StaticWriteToLog("The format of the connection string is invalid."); return; } if (serviceBusConnectionStringBuilder.Endpoints == null || serviceBusConnectionStringBuilder.Endpoints.Count == 0) { MainForm.StaticWriteToLog("The connection string does not contain any endpoint."); return; } var host = serviceBusConnectionStringBuilder.Endpoints.ToArray()[0].Host; var index = host.IndexOf(".", StringComparison.Ordinal); var key = index > 0 ? CultureInfo.CurrentCulture.TextInfo.ToTitleCase(host.Substring(0, index)) : "MyNamespace"; using (var parameterForm = new ParameterForm("Enter the key for the Service Bus namespace", new List <string> { "Key" }, new List <string> { key }, new List <bool> { false })) { if (parameterForm.ShowDialog() != DialogResult.OK) { return; } key = parameterForm.ParameterValues[0]; if (string.IsNullOrWhiteSpace(key)) { MainForm.StaticWriteToLog("The key of the Service Bus namespace cannot be null."); return; } var value = txtUri.Text; var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var configurationSection = configuration.Sections["serviceBusNamespaces"]; var directory = Path.GetDirectoryName(configuration.FilePath); if (string.IsNullOrEmpty(directory)) { MainForm.StaticWriteToLog("The directory of the configuration file cannot be null."); return; } var appConfig = Path.Combine(directory, "..\\..\\App.config"); configurationSection.SectionInformation.ForceSave = true; var xml = configurationSection.SectionInformation.GetRawXml(); var xmlDocument = new XmlDocument(); xmlDocument.LoadXml(xml); var node = xmlDocument.CreateElement("add"); node.SetAttribute("key", key); node.SetAttribute("value", value); xmlDocument.DocumentElement?.AppendChild(node); configurationSection.SectionInformation.SetRawXml(xmlDocument.OuterXml); configuration.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("serviceBusNamespaces"); if (File.Exists(appConfig)) { var exeConfigurationFileMap = new ExeConfigurationFileMap() { ExeConfigFilename = appConfig }; configuration = ConfigurationManager.OpenMappedExeConfiguration(exeConfigurationFileMap, ConfigurationUserLevel.None); configurationSection = configuration.Sections["serviceBusNamespaces"]; configurationSection.SectionInformation.ForceSave = true; xml = configurationSection.SectionInformation.GetRawXml(); xmlDocument = new XmlDocument(); xmlDocument.LoadXml(xml); node = xmlDocument.CreateElement("add"); node.SetAttribute("key", key); node.SetAttribute("value", value); xmlDocument.DocumentElement?.AppendChild(node); configurationSection.SectionInformation.SetRawXml(xmlDocument.OuterXml); configuration.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("serviceBusNamespaces"); } serviceBusHelper.ServiceBusNamespaces.Add(key, MainForm.GetServiceBusNamespace(key, value)); cboServiceBusNamespace.Items.Clear(); // ReSharper disable once CoVariantArrayConversion cboServiceBusNamespace.Items.AddRange(serviceBusHelper.ServiceBusNamespaces.Keys.OrderBy(s => s).ToArray()); cboServiceBusNamespace.Text = key; } } catch (Exception ex) { HandleException(ex); } }
public static IServiceCollection AddCustomIntegrations(this IServiceCollection services, IConfiguration configuration) { services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddTransient <IIdentityService, IdentityService>(); services.AddTransient <Func <DbConnection, IIntegrationEventLogService> >( sp => (DbConnection c) => new IntegrationEventLogService(c)); services.AddTransient <IOrderingIntegrationEventService, OrderingIntegrationEventService>(); if (configuration.GetValue <bool>("AzureServiceBusEnabled")) { services.AddSingleton <IServiceBusPersisterConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultServiceBusPersisterConnection> >(); var serviceBusConnectionString = configuration["EventBusConnection"]; var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); return(new DefaultServiceBusPersisterConnection(serviceBusConnection, logger)); }); } else if (configuration.GetValue <bool>("RabbitMQBusEnabled")) { services.AddSingleton <IRabbitMQPersistentConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >(); var factory = new ConnectionFactory() { HostName = configuration["EventBusConnection"], DispatchConsumersAsync = true }; if (!string.IsNullOrEmpty(configuration["EventBusUserName"])) { factory.UserName = configuration["EventBusUserName"]; } if (!string.IsNullOrEmpty(configuration["EventBusPassword"])) { factory.Password = configuration["EventBusPassword"]; } var retryCount = 5; if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"])) { retryCount = int.Parse(configuration["EventBusRetryCount"]); } return(new DefaultRabbitMQPersistentConnection(factory, logger, retryCount)); }); } else { services.AddSingleton <INCachePersistantConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultNCachePersistentConnection> >(); var cacheID = "PubSubCache"; if (!string.IsNullOrEmpty(configuration["EventBusNCachID"])) { cacheID = configuration["EventBusNCachID"]; } var topicID = "IntegrationEvents"; if (!string.IsNullOrEmpty(configuration["EventBusNCachTopicID"])) { topicID = configuration["EventBusNCachTopicID"]; } var ipAddresses = configuration.GetValue <string>("PubSubCacheIPAddresses").Split('-'); if (ipAddresses.Length == 0) { throw new ArgumentNullException("No IP addresses given for Pub Sub Cache"); } for (int i = 0; i < ipAddresses.Length; i++) { ipAddresses[i] = ipAddresses[i].Trim(); } var enableNCacheClientLogs = configuration.GetValue <bool>("EnableNCacheClientLogs", false); return(new DefaultNCachePersistentConnection(cacheID, topicID, logger, ipAddresses, enableNCacheClientLogs)); }); } return(services); }
public int Run() { // Obtain management via .publishsettings file from https://manage.windowsazure.com/publishsettings/index?schemaversion=2.0 var creds = new CertificateCloudCredentials(SubscriptionId, ManagementCertificate); // Create Namespace var sbMgmt = new ServiceBusManagementClient(creds); ServiceBusNamespaceResponse nsResponse = null; Console.WriteLine("Creating Service Bus namespace {0} in location {1}", SBNamespace, Location); try { var resultSb = sbMgmt.Namespaces.Create(SBNamespace, Location); if (resultSb.StatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine("Error creating Service Bus namespace {0} in Location {1}: {2}", SBNamespace, Location, resultSb.StatusCode); return(1); } } catch (CloudException) { try { // There is (currently) no clean error code returned when the namespace already exists // Check if it does nsResponse = sbMgmt.Namespaces.Get(SBNamespace); Console.WriteLine("Service Bus namespace {0} already existed.", SBNamespace); } catch (Exception) { nsResponse = null; } if (nsResponse == null) { throw; } } // Wait until the namespace is active while (nsResponse == null || nsResponse.Namespace.Status != "Active") { nsResponse = sbMgmt.Namespaces.Get(SBNamespace); if (nsResponse.Namespace.Status == "Active") { break; } Console.WriteLine("Namespace {0} in state {1}. Waiting...", SBNamespace, nsResponse.Namespace.Status); System.Threading.Thread.Sleep(5000); } // Get the namespace connection string var nsDescription = sbMgmt.Namespaces.GetNamespaceDescription(SBNamespace); var nsConnectionString = nsDescription.NamespaceDescriptions.First( (d) => String.Equals(d.AuthorizationType, "SharedAccessAuthorization") ).ConnectionString; // Create EHs + device keys + consumer keys (WebSite*) var nsManager = NamespaceManager.CreateFromConnectionString(nsConnectionString); var ehDescriptionDevices = new EventHubDescription(EventHubNameDevices) { PartitionCount = 8, }; ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("D1", new List <AccessRights> { AccessRights.Send })); ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("D2", new List <AccessRights> { AccessRights.Send })); ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("D3", new List <AccessRights> { AccessRights.Send })); ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("D4", new List <AccessRights> { AccessRights.Send })); ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("WebSite", new List <AccessRights> { AccessRights.Manage, AccessRights.Listen, AccessRights.Send })); ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("StreamingAnalytics", new List <AccessRights> { AccessRights.Manage, AccessRights.Listen, AccessRights.Send })); Console.WriteLine("Creating Event Hub {0}", EventHubNameDevices); EventHubDescription ehDevices = null; do { try { ehDevices = nsManager.CreateEventHubIfNotExists(ehDescriptionDevices); } catch (System.UnauthorizedAccessException) { Console.WriteLine("Service Bus connection string not valid yet. Waiting..."); System.Threading.Thread.Sleep(5000); } } while (ehDevices == null); var ehDescriptionAlerts = new EventHubDescription(EventHubNameAlerts) { PartitionCount = 8, }; ehDescriptionAlerts.Authorization.Add(new SharedAccessAuthorizationRule("WebSite", new List <AccessRights> { AccessRights.Manage, AccessRights.Listen, AccessRights.Send })); ehDescriptionAlerts.Authorization.Add(new SharedAccessAuthorizationRule("StreamingAnalytics", new List <AccessRights> { AccessRights.Manage, AccessRights.Listen, AccessRights.Send })); Console.WriteLine("Creating Event Hub {0}", EventHubNameAlerts); var ehAlerts = nsManager.CreateEventHubIfNotExists(ehDescriptionAlerts); // Create Storage Account for Event Hub Processor var stgMgmt = new StorageManagementClient(creds); try { Console.WriteLine("Creating Storage Account {0} in location {1}", StorageAccountName, Location); var resultStg = stgMgmt.StorageAccounts.Create( new StorageAccountCreateParameters { Name = StorageAccountName.ToLowerInvariant(), Location = Location, AccountType = "Standard_LRS" }); if (resultStg.StatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine("Error creating storage account {0} in Location {1}: {2}", StorageAccountName, Location, resultStg.StatusCode); return(1); } } catch (CloudException ce) { if (String.Equals(ce.ErrorCode, "ConflictError", StringComparison.InvariantCultureIgnoreCase)) { Console.WriteLine("Storage account {0} already existed.", StorageAccountName); } else { throw; } } var keyResponse = stgMgmt.StorageAccounts.GetKeys(StorageAccountName.ToLowerInvariant()); if (keyResponse.StatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine("Error retrieving access keys for storage account {0} in Location {1}: {2}", StorageAccountName, Location, keyResponse.StatusCode); return(1); } var storageKey = keyResponse.PrimaryKey; string ehDevicesWebSiteConnectionString = new ServiceBusConnectionStringBuilder(nsConnectionString) { SharedAccessKeyName = "WebSite", SharedAccessKey = (ehDevices.Authorization.First((d) => String.Equals(d.KeyName, "WebSite", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey, }.ToString(); string ehAlertsWebSiteConnectionString = new ServiceBusConnectionStringBuilder(nsConnectionString) { SharedAccessKeyName = "WebSite", SharedAccessKey = (ehAlerts.Authorization.First((d) => String.Equals(d.KeyName, "WebSite", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey, }.ToString(); // Write a new web.config template file var doc = new XmlDocument(); doc.PreserveWhitespace = true; var inputFileName = (this.Transform ? "\\web.PublishTemplate.config" : "\\web.config"); var outputFileName = (this.Transform ? String.Format("\\web.{0}.config", NamePrefix) : "\\web.config"); doc.Load(WebSiteDirectory + inputFileName); doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.EventHubDevices']/@value").Value = EventHubNameDevices; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.EventHubAlerts']/@value").Value = EventHubNameAlerts; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionString']/@value").Value = nsConnectionString; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionStringDevices']/@value").Value = ehDevicesWebSiteConnectionString; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionStringAlerts']/@value").Value = ehAlertsWebSiteConnectionString; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.Storage.ConnectionString']/@value").Value = String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", StorageAccountName, storageKey); var outputFile = System.IO.Path.GetFullPath(WebSiteDirectory + outputFileName); doc.Save(outputFile); Console.WriteLine(); Console.WriteLine("Service Bus management connection string (i.e. for use in Service Bus Explorer):"); Console.WriteLine(nsConnectionString); Console.WriteLine(); Console.WriteLine("Device AMQP address strings (for Raspberry PI/devices):"); for (int i = 1; i <= 4; i++) { var deviceKeyName = String.Format("D{0}", i); var deviceKey = (ehDevices.Authorization.First((d) => String.Equals(d.KeyName, deviceKeyName, StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey; Console.WriteLine("amqps://{0}:{1}@{2}.servicebus.windows.net", deviceKeyName, Uri.EscapeDataString(deviceKey), SBNamespace); //Console.WriteLine(new ServiceBusConnectionStringBuilder(nsConnectionString) //{ // SharedAccessKeyName = deviceKeyName, // SharedAccessKey = deviceKey, //}.ToString()); } Console.WriteLine(); Console.WriteLine("Web.Config saved to {0}", outputFile); #if AZURESTREAMANALYTICS // Create StreamAnalyticsJobs + inputs + outputs + enter keys // Untested code. May require AAD authentication, no support for management cert? // Create Resource Group for the Stream Analytics jobs var groupCreateRequest = WebRequest.Create(String.Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}?api-version=2014-04-01-preview", SubscriptionId, StreamAnalyticsGroup)) as HttpWebRequest; groupCreateRequest.ClientCertificates.Add(creds.ManagementCertificate); groupCreateRequest.ContentType = "application/json"; groupCreateRequest.Method = "PUT"; groupCreateRequest.KeepAlive = true; var bytesGroup = Encoding.UTF8.GetBytes("{\"location\":\"Central US\"}"); groupCreateRequest.ContentLength = bytesGroup.Length; groupCreateRequest.GetRequestStream().Write(bytesGroup, 0, bytesGroup.Length); var groupCreateResponse = groupCreateRequest.GetResponse(); //var streamMgmt = new ManagementClient(creds); //, new Uri("https://management.azure.com")); //HttpClient client = streamMgmt.HttpClient; var createJob = new StreamAnalyticsJob() { location = Location, inputs = new List <StreamAnalyticsEntity> { new StreamAnalyticsEntity { name = "devicesInput", properties = new Dictionary <string, object> { { "type", "stream" }, { "serialization", new Dictionary <string, object> { { "type", "JSON" }, { "properties", new Dictionary <string, object> { { "encoding", "UTF8" }, } } } }, { "datasource", new Dictionary <string, object> { { "type", "Microsoft.ServiceBus/EventHub" }, { "properties", new Dictionary <string, object> { { "eventHubNamespace", Namespace }, { "eventHubName", EventHubDevices }, { "sharedAccessPolicyName", "StreamingAnalytics" }, { "sharedAccessPolicyKey", (ehDevices.Authorization.First((d) => String.Equals(d.KeyName, "StreamingAnalytics", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey }, } } } } }, }, }, transformation = new StreamAnalyticsEntity() { name = "Aggregates", properties = new Dictionary <string, object> { { "streamingUnits", 1 }, { "query", "select * from devicesInput" }, } }, outputs = new List <StreamAnalyticsEntity> { new StreamAnalyticsEntity { name = "output", properties = new Dictionary <string, object> { { "datasource", new Dictionary <string, object> { { "type", "Microsoft.ServiceBus/EventHub" }, { "properties", new Dictionary <string, object> { { "eventHubNamespace", Namespace }, { "eventHubName", EventHubAlerts }, { "sharedAccessPolicyName", "StreamingAnalytics" }, { "sharedAccessPolicyKey", (ehAlerts.Authorization.First((d) => String.Equals(d.KeyName, "StreamingAnalytics", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey }, } } } }, { "serialization", new Dictionary <string, object> { { "type", "JSON" }, { "properties", new Dictionary <string, object> { { "encoding", "UTF8" }, } } } }, }, }, } }; var jobCreateRequest = WebRequest.Create(String.Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}/Microsoft.StreamAnalytics/streamingjobs/{2}?api-version=2014-10-01", SubscriptionId, StreamAnalyticsGroup, JobAggregates)) as HttpWebRequest; jobCreateRequest.ClientCertificates.Add(creds.ManagementCertificate); jobCreateRequest.ContentType = "application/json"; jobCreateRequest.Method = "PUT"; jobCreateRequest.KeepAlive = true; var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(createJob)); jobCreateRequest.ContentLength = bytes.Length; jobCreateRequest.GetRequestStream().Write(bytes, 0, bytes.Length); var jobCreateResponse = jobCreateRequest.GetResponse(); //var jobCreateTask = streamMgmt.HttpClient.PutAsync( // String.Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}/Microsoft.StreamAnalytics/streamingjobs/{2}?api-version=2014-10-01", // SubscriptionId, StreamAnalyticsGroup, JobAggregates), // new StringContent(JsonConvert.SerializeObject(createJob))); //jobCreateTask.Wait(); //var jobCreateResponse = jobCreateTask.Result; #endif return(0); }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.Configure <PaymentSettings>(Configuration); RegisterAppInsights(services); if (Configuration.GetValue <bool>("AzureServiceBusEnabled")) { services.AddSingleton <IServiceBusPersisterConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultServiceBusPersisterConnection> >(); var serviceBusConnectionString = Configuration["EventBusConnection"]; var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); return(new DefaultServiceBusPersisterConnection(serviceBusConnection)); }); } else { services.AddSingleton <IRabbitMqPersistentConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultRabbitMqPersistentConnection> >(); var factory = new ConnectionFactory { HostName = Configuration["EventBusConnection"] }; if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) { factory.UserName = Configuration["EventBusUserName"]; } if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) { factory.Password = Configuration["EventBusPassword"]; } var retryCount = 5; if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) { retryCount = int.Parse(Configuration["EventBusRetryCount"]); } return(new DefaultRabbitMqPersistentConnection(factory, logger, retryCount)); }); } services.AddHealthChecks(checks => { checks.AddValueTaskCheck("HTTP Endpoint", () => new ValueTask <IHealthCheckResult>(HealthCheckResult.Healthy("Ok"))); }); RegisterEventBus(services); var container = new ContainerBuilder(); container.Populate(services); return(new AutofacServiceProvider(container.Build())); }
/// <summary> /// Used to register Azure Service Bus as well as add a mapping for messages to topics or queues. This method takes in a connection string builder to configure the Service Bus connection. /// </summary> /// <param name="services"></param> /// <param name="serviceBusName"></param> /// <param name="connectionBuilder"></param> /// <param name="transportType"></param> /// <param name="retryPolicy"></param> public static void AddAzureServiceBusConnectionPool(this IServiceCollection services, ServiceBusConnectionStringBuilder connectionStringBuilder, Action <ServiceBusClientPoolBuilder> connectionBuilder) { services.AddSingleton(provider => { var serviceBusConnection = new ServiceBusConnection(connectionStringBuilder); return(serviceBusConnection); }); services.AddSingleton <IServiceBusClientPool>(provider => { var serviceBusConnection = provider.GetRequiredService <ServiceBusConnection>(); var poolBuilder = new ServiceBusClientPoolBuilder(serviceBusConnection); connectionBuilder(poolBuilder); return(new ServiceBusClientPool(poolBuilder)); }); }
public OrdersQueueProcessor(IShipmentRepository shipmentRepository, ServiceBusConnectionStringBuilder serviceBusConnectionStringBuilder, IConfiguration configuration, ILogger <OrdersQueueProcessor> logger) : base(serviceBusConnectionStringBuilder, configuration, logger) { _shipmentRepository = shipmentRepository; }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { //add health check for this service services.AddCustomHealthCheck(Configuration); //configure settings services.Configure <BackgroundTaskSettings>(Configuration); services.AddOptions(); //configure background task services.AddSingleton <IHostedService, GracePeriodManagerService>(); //configure event bus related services if (Configuration.GetValue <bool>("AzureServiceBusEnabled")) { services.AddSingleton <IServiceBusPersisterConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultServiceBusPersisterConnection> >(); var serviceBusConnectionString = Configuration["EventBusConnection"]; var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); return(new DefaultServiceBusPersisterConnection(serviceBusConnection, logger)); }); } else if (Configuration.GetValue <bool>("RabbitMQBusEnabled")) { services.AddSingleton <IRabbitMQPersistentConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >(); var factory = new ConnectionFactory() { HostName = Configuration["EventBusConnection"], DispatchConsumersAsync = true }; if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) { factory.UserName = Configuration["EventBusUserName"]; } if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) { factory.Password = Configuration["EventBusPassword"]; } var retryCount = 5; if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) { retryCount = int.Parse(Configuration["EventBusRetryCount"]); } return(new DefaultRabbitMQPersistentConnection(factory, logger, retryCount)); }); } else { services.AddSingleton <INCachePersistantConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultNCachePersistentConnection> >(); var cacheID = "PubSubCache"; if (!string.IsNullOrEmpty(Configuration["EventBusNCachID"])) { cacheID = Configuration["EventBusNCachID"]; } var topicID = "IntegrationEvents"; if (!string.IsNullOrEmpty(Configuration["EventBusNCachTopicID"])) { topicID = Configuration["EventBusNCachTopicID"]; } var ipAddresses = Configuration.GetValue <string>("PubSubCacheIPAddresses").Split('-'); if (ipAddresses.Length == 0) { throw new ArgumentNullException("No IP addresses given for Pub Sub Cache"); } for (int i = 0; i < ipAddresses.Length; i++) { ipAddresses[i] = ipAddresses[i].Trim(); } var enableNCacheClientLogs = Configuration.GetValue <bool>("EnableNCacheClientLogs", false); return(new DefaultNCachePersistentConnection(cacheID, topicID, logger, ipAddresses, enableNCacheClientLogs)); }); } RegisterEventBus(services); //create autofac based service provider var container = new ContainerBuilder(); container.Populate(services); return(new AutofacServiceProvider(container.Build())); }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0); /* * Add Authentication. */ services.AddMvcCore() .AddAuthorization() .AddNewtonsoftJson(); services .AddAuthentication("Bearer") .AddIdentityServerAuthentication(options => { options.Authority = Configuration.GetSection("AuthenticationBearer:Authority").Value; options.RequireHttpsMetadata = true; options.ApiName = Configuration.GetSection("AuthenticationBearer:ApiName").Value; options.ApiSecret = Configuration.GetSection("AuthenticationBearer:ApiSecret").Value; }); /* * EventBus Setup. */ if (Configuration["ClusterMode"] == Program.CLUSTER_AZURE) { /* * Azure Service Bus. */ services.AddSingleton <IAzureServiceBusPersisterConnection>(sp => { var logger = sp.GetRequiredService <ILogger <AzureServiceBusPersisterConnection> >(); var serviceBusConnectionString = Configuration.GetSection("Azure:EventBusConnection").Value; var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); if ((Configuration.GetSection("Azure:EventBusEntityPath").Exists()) && (!string.IsNullOrEmpty(Configuration.GetSection("Azure:EventBusEntityPath").Value))) { serviceBusConnection.EntityPath = Configuration.GetSection("Azure:EventBusEntityPath").Value; } return(new AzureServiceBusPersisterConnection(serviceBusConnection, logger)); }); services.AddSingleton <IEventBus, EventBusAzureServiceBus>(sp => { var serviceBusPersisterConnection = sp.GetRequiredService <IAzureServiceBusPersisterConnection>(); var iLifetimeScope = sp.GetRequiredService <ILifetimeScope>(); var logger = sp.GetRequiredService <ILogger <EventBusAzureServiceBus> >(); var eventBusSubcriptionsManager = sp.GetRequiredService <IEventBusSubscriptionsManager>(); var subscriptionClientName = Configuration.GetSection("Azure:SubscriptionClientName").Value; return(new EventBusAzureServiceBus(serviceBusPersisterConnection, logger, eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope)); }); } else { /* * RabbitMq. */ services.AddSingleton <IRabbitMQPersistentConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >(); var factory = new ConnectionFactory() { HostName = Configuration.GetSection("RabbitMqBus:Connection").Value }; factory.UserName = Configuration.GetSection("RabbitMqBus:User").Value; factory.Password = Configuration.GetSection("RabbitMqBus:Password").Value; var retryCount = 5; if ((Configuration.GetSection("RabbitMqBus:RetryCount").Exists()) && (!string.IsNullOrEmpty(Configuration.GetSection("RabbitMqBus:RetryCount").Value))) { retryCount = int.Parse(Configuration.GetSection("RabbitMqBus:RetryCount").Value); } return(new DefaultRabbitMQPersistentConnection(factory, logger, retryCount)); }); services.AddSingleton <IEventBus, EventBusRabbitMQ>(sp => { var rabbitMQConnexion = sp.GetRequiredService <IRabbitMQPersistentConnection>(); var iLifetimeScope = sp.GetRequiredService <ILifetimeScope>(); var logger = sp.GetRequiredService <ILogger <EventBusRabbitMQ> >(); var eventBusAboManager = sp.GetRequiredService <IEventBusSubscriptionsManager>(); var retryCount = 5; if ((Configuration.GetSection("RabbitMqBus:RetryCount").Exists()) && (!string.IsNullOrEmpty(Configuration.GetSection("RabbitMqBus:RetryCount").Value))) { retryCount = int.Parse(Configuration.GetSection("RabbitMqBus:RetryCount").Value); } var queueName = Configuration.GetSection("RabbitMqBus:QueueName").Value; return(new EventBusRabbitMQ(rabbitMQConnexion, logger, iLifetimeScope, eventBusAboManager, queueName, retryCount)); }); } services.AddSingleton <IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); services.AddTransient <CheckOutEventHandler>(); /* * MongoDB Setup. */ services.Configure <MongoDbConfig>(Options => { Options.IsAzure = (Configuration["ClusterMode"] == Program.CLUSTER_AZURE); Options.ConnectionString = Configuration.GetSection("MongoDbConfig:ConnectionString").Value; Options.Datebase = Configuration.GetSection("MongoDbConfig:Datebase").Value; Options.User = Configuration.GetSection("MongoDbConfig:User").Value; Options.Password = Configuration.GetSection("MongoDbConfig:Password").Value; }); services.AddSingleton <IMongoDbClient>(sp => { ILogger <MongoDbClient> logger = sp.GetRequiredService <ILogger <MongoDbClient> >(); IOptions <MongoDbConfig> options = sp.GetRequiredService <IOptions <MongoDbConfig> >(); return(new MongoDbClient(options, logger)); }); services.AddSingleton <ICustomerServices>(sp => { return(new CustomerServices(sp.GetRequiredService <IMongoDbClient>())); }); /* * Custom Validator Setup. */ services.AddSingleton <ICustomerFlowValid>(sp => { return(new CustomerFlowValid()); }); /* * Register the Swagger generator. */ services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Customer.API", Version = "v1" }); c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme { Type = SecuritySchemeType.OAuth2, Flows = new OpenApiOAuthFlows { Password = new OpenApiOAuthFlow { AuthorizationUrl = new Uri($"{Configuration.GetSection("AuthenticationBearer:Authority").Value}/connect/authorize"), TokenUrl = new Uri($"{Configuration.GetSection("AuthenticationBearer:Authority").Value}/connect/token"), Scopes = new Dictionary <string, string> { { "customer", "Access Customer.API" } } } } }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" } }, new[] { "customer" } } }); }); /* * Configuration de AutoFac. * Inversion de contrôle. */ var container = new ContainerBuilder(); container.Populate(services); return(new AutofacServiceProvider(container.Build())); }
public IServiceProvider ConfigureServices(IServiceCollection services) { RegisterAppInsights(services); services.AddMvc(options => { options.Filters.Add(typeof(HttpGlobalExceptionFilter)); }).AddControllersAsServices(); ConfigureAuthService(services); services.Configure <Settings>(Configuration); if (Configuration.GetValue <bool>("AzureServiceBusEnabled")) { services.AddSingleton <IServiceBusPersisterConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultServiceBusPersisterConnection> >(); var serviceBusConnectionString = Configuration["EventBusConnection"]; var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); return(new DefaultServiceBusPersisterConnection(serviceBusConnection, logger)); }); } else { services.AddSingleton <IRabbitMQPersistentConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >(); var factory = new ConnectionFactory() { HostName = Configuration["EventBusConnection"] }; if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) { factory.UserName = Configuration["EventBusUserName"]; } if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) { factory.Password = Configuration["EventBusPassword"]; } var retryCount = 5; if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) { retryCount = int.Parse(Configuration["EventBusRetryCount"]); } return(new DefaultRabbitMQPersistentConnection(factory, logger, retryCount)); }); } services.AddHealthChecks(checks => { checks.AddValueTaskCheck("HTTP Endpoint", () => new ValueTask <IHealthCheckResult>(HealthCheckResult.Healthy("Ok"))); }); RegisterEventBus(services); // Add framework services. services.AddSwaggerGen(options => { options.DescribeAllEnumsAsStrings(); options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "LeadsPlus - CloudMallin webhook", Version = "v1", Description = "The Agent Microservice HTTP API. This is a Data-Driven/CRUD microservice sample", TermsOfService = "Terms Of Service" }); options.AddSecurityDefinition("oauth2", new OAuth2Scheme { Type = "oauth2", Flow = "implicit", AuthorizationUrl = $"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize", TokenUrl = $"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/token", Scopes = new Dictionary <string, string>() { { "CloudmailinWebhook", "Cloudmailin Webhook" } } }); options.OperationFilter <AuthorizeCheckOperationFilter>(); }); services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); }); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); //configure autofac var container = new ContainerBuilder(); container.Populate(services); return(new AutofacServiceProvider(container.Build())); }
/// <summary> /// Returns Service Bus connection string to use. /// </summary> public string BuildConnectionString() { if (OperationTimeout != null) { var connectionStringBuilder = new ServiceBusConnectionStringBuilder(ConnectionString); connectionStringBuilder.OperationTimeout = OperationTimeout.Value; return connectionStringBuilder.ToString(); } return ConnectionString; }
public static IServiceCollection AddCustomIntegrations(this IServiceCollection services, IConfiguration configuration) { services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddTransient <Func <DbConnection, IIntegrationEventLogService> >( sp => (DbConnection c) => new IntegrationEventLogService(c)); if (configuration.GetValue <bool>("AzureServiceBusEnabled")) { services.AddSingleton <IServiceBusPersisterConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultServiceBusPersisterConnection> >(); var serviceBusConnectionString = configuration["EventBusConnection"]; var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); return(new DefaultServiceBusPersisterConnection(serviceBusConnection, logger)); }); } else { services.AddSingleton <IRabbitMQPersistentConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >(); var factory = new ConnectionFactory() { HostName = configuration["EventBusConnection"] }; if (!string.IsNullOrEmpty(configuration["EventBusUserName"])) { factory.UserName = configuration["EventBusUserName"]; } if (!string.IsNullOrEmpty(configuration["EventBusPassword"])) { factory.Password = configuration["EventBusPassword"]; } if (!string.IsNullOrEmpty(configuration["EventBusPort"])) { factory.Port = int.Parse(configuration["EventBusPort"]); } if (!string.IsNullOrEmpty(configuration["EventBusVirtualHost"])) { factory.VirtualHost = configuration["EventBusVirtualHost"]; } var retryCount = 5; if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"])) { retryCount = int.Parse(configuration["EventBusRetryCount"]); } return(new DefaultRabbitMQPersistentConnection(factory, logger, retryCount)); }); } return(services); }
/// <summary> /// Create servicebus connectionstring /// </summary> private void initServiceBusConnectionStringBuilder() { connBuilder = new ServiceBusConnectionStringBuilder(); connBuilder.ManagementPort = HttpPort; connBuilder.RuntimePort = TcpPort; connBuilder.Endpoints.Add(new UriBuilder() { Scheme = "sb", Host = ServerFQDN, Path = ServiceNamespace }.Uri); connBuilder.StsEndpoints.Add(new UriBuilder() { Scheme = "https", Host = ServerFQDN, Port = HttpPort, Path = ServiceNamespace }.Uri); }
public async Task Run(string connectionString) { var sbb = new ServiceBusConnectionStringBuilder(connectionString); try { // Create sender to Sequence Service using (var sendChannelFactory = new ChannelFactory <ISequenceServiceChannel>("sequenceSendClient")) { sendChannelFactory.Endpoint.Address = new EndpointAddress( new Uri(sbb.GetAbsoluteRuntimeEndpoints()[0], SessionQueueName)); sendChannelFactory.Endpoint.EndpointBehaviors.Add( new TransportClientEndpointBehavior(TokenProvider.CreateSharedAccessSignatureTokenProvider(sbb.SharedAccessKeyName, sbb.SharedAccessKey))); using (var clientChannel = sendChannelFactory.CreateChannel()) { for (int j = 0; j < 3; j++) { var contextId = Guid.NewGuid().ToString(); // Send messages var sequenceLength = new Random().Next(5, 10); for (var i = 0; i < sequenceLength; i++) { // Generating a random sequence item var sequenceItem = new SequenceItem( string.Format("{0:00000}", new Random().Next(0, 10000)), new Random().Next(1, 100)); // set the operation context for the subsequent call, this MUST be a new context OperationContext.Current = new OperationContext(clientChannel) { OutgoingMessageProperties = { { BrokeredMessageProperty.Name, new BrokeredMessageProperty { SessionId = contextId, TimeToLive = TimeSpan.FromMinutes(5) } } } }; // Correlating ServiceBus SessionId to ContextId await clientChannel.SubmitSequenceItemAsync(sequenceItem); Console.WriteLine("Sequence: {0} [{1}] - ContextId {2}.", sequenceItem.ItemId, sequenceItem.Quantity, contextId); } // set the operation context for the subsequent call, this MUST be a new context OperationContext.Current = new OperationContext(clientChannel) { OutgoingMessageProperties = { { BrokeredMessageProperty.Name, new BrokeredMessageProperty { SessionId = contextId, TimeToLive = TimeSpan.FromMinutes(5) } } } }; await clientChannel.TerminateSequenceAsync(); } clientChannel.Close(); } // Close sender sendChannelFactory.Close(); } } catch (Exception exception) { Console.WriteLine("Exception occurred: {0}", exception); } }
private static void Main(string[] args) { ServerFQDN = System.Net.Dns.GetHostEntry(string.Empty).HostName; ServiceBusConnectionStringBuilder connBuilder = new ServiceBusConnectionStringBuilder { ManagementPort = HttpPort, RuntimePort = TcpPort }; connBuilder.Endpoints.Add(new UriBuilder { Scheme = "sb", Host = ServerFQDN, Path = ServiceNamespace }.Uri); connBuilder.StsEndpoints.Add(new UriBuilder { Scheme = "https", Host = ServerFQDN, Port = HttpPort, Path = ServiceNamespace }.Uri); MessagingFactory messageFactory = MessagingFactory.CreateFromConnectionString(connBuilder.ToString()); NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString()); if (namespaceManager == null) { Console.WriteLine("\nUnepected Error"); return; } string queueName = "ServiceBusQueueSample"; if (namespaceManager.QueueExists(queueName)) { namespaceManager.DeleteQueue(queueName); } namespaceManager.CreateQueue(queueName); QueueClient myQueueClient = messageFactory.CreateQueueClient(queueName); try { BrokeredMessage sendMessage = new BrokeredMessage("Hello World!"); myQueueClient.Send(sendMessage); //Receive the message from the queue BrokeredMessage receivedMessage = myQueueClient.Receive(TimeSpan.FromSeconds(5)); if (receivedMessage != null) { Console.WriteLine(string.Format($"Message received:{receivedMessage.GetBody<string>()}")); receivedMessage.Complete(); } } catch (Exception e) { Console.WriteLine($"Unexpected exception {e}"); throw; } finally { if (messageFactory != null) { messageFactory.Close(); } Console.WriteLine("Press ENTER to clean up and exit."); Console.ReadLine(); } }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { //add health check for this service services.AddHealthChecks(checks => { var minutes = 1; if (int.TryParse(Configuration["HealthCheck:Timeout"], out var minutesParsed)) { minutes = minutesParsed; } checks.AddSqlCheck("OrderingDb", Configuration["ConnectionString"], TimeSpan.FromMinutes(minutes)); }); //configure settings services.Configure <BackgroundTaskSettings>(Configuration); services.AddOptions(); //configure background task services.AddSingleton <IHostedService, GracePeriodManagerService>(); //configure event bus related services if (Configuration.GetValue <bool>("AzureServiceBusEnabled")) { services.AddSingleton <IServiceBusPersisterConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultServiceBusPersisterConnection> >(); var serviceBusConnectionString = Configuration["EventBusConnection"]; var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); return(new DefaultServiceBusPersisterConnection(serviceBusConnection)); }); } else { services.AddSingleton <IRabbitMqPersistentConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultRabbitMqPersistentConnection> >(); var factory = new ConnectionFactory() { HostName = Configuration["EventBusConnection"] }; if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) { factory.UserName = Configuration["EventBusUserName"]; } if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) { factory.Password = Configuration["EventBusPassword"]; } var retryCount = 5; if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) { retryCount = int.Parse(Configuration["EventBusRetryCount"]); } return(new DefaultRabbitMqPersistentConnection(factory, logger, retryCount)); }); } RegisterEventBus(services); //create autofac based service provider var container = new ContainerBuilder(); container.Populate(services); return(new AutofacServiceProvider(container.Build())); }
/// <summary> /// Create a topic client from a connection string /// </summary> /// <param name="connectionString">Connection string</param> /// <returns>Instace of TopicClient class</returns> public static TopicClient CreateFromConnectionString(string connectionString) { ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString); return CreateFromConnectionString(connectionString, builder.EntityPath); }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { RegisterAppInsights(services); // Add framework services. services.AddMvc(options => { options.Filters.Add(typeof(HttpGlobalExceptionFilter)); options.Filters.Add(typeof(ValidateModelStateFilter)); }).AddControllersAsServices(); ConfigureAuthService(services); services.AddHealthChecks(checks => { checks.AddValueTaskCheck("HTTP Endpoint", () => new ValueTask <IHealthCheckResult>(HealthCheckResult.Healthy("Ok")), TimeSpan.Zero //No cache for this HealthCheck, better just for demos ); }); services.Configure <BasketSettings>(Configuration); //By connecting here we are making sure that our service //cannot start until redis is ready. This might slow down startup, //but given that there is a delay on resolving the ip address //and then creating the connection it seems reasonable to move //that cost to startup instead of having the first request pay the //penalty. services.AddSingleton <ConnectionMultiplexer>(sp => { var settings = sp.GetRequiredService <IOptions <BasketSettings> >().Value; var configuration = ConfigurationOptions.Parse(settings.ConnectionString, true); configuration.ResolveDns = true; return(ConnectionMultiplexer.Connect(configuration)); }); if (Configuration.GetValue <bool>("AzureServiceBusEnabled")) { services.AddSingleton <IServiceBusPersisterConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultServiceBusPersisterConnection> >(); var serviceBusConnectionString = Configuration["EventBusConnection"]; var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); return(new DefaultServiceBusPersisterConnection(serviceBusConnection, logger)); }); } else { services.AddSingleton <IRabbitMQPersistentConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >(); var factory = new ConnectionFactory() { HostName = Configuration["EventBusConnection"] }; if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) { factory.UserName = Configuration["EventBusUserName"]; } if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) { factory.Password = Configuration["EventBusPassword"]; } var retryCount = 5; if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) { retryCount = int.Parse(Configuration["EventBusRetryCount"]); } return(new DefaultRabbitMQPersistentConnection(factory, logger, retryCount)); }); } RegisterEventBus(services); services.AddSwaggerGen(options => { options.DescribeAllEnumsAsStrings(); options.SwaggerDoc("v1", new Info { Title = "Basket HTTP API", Version = "v1", Description = "The Basket Service HTTP API", TermsOfService = "Terms Of Service" }); options.AddSecurityDefinition("oauth2", new OAuth2Scheme { Type = "oauth2", Flow = "implicit", AuthorizationUrl = $"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize", TokenUrl = $"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/token", Scopes = new Dictionary <string, string>() { { "basket", "Basket API" } } }); options.OperationFilter <AuthorizeCheckOperationFilter>(); }); services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); }); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddTransient <IBasketRepository, RedisBasketRepository>(); services.AddTransient <IIdentityService, IdentityService>(); services.AddOptions(); var container = new ContainerBuilder(); container.Populate(services); return(new AutofacServiceProvider(container.Build())); }
void ConnectionStringBuilderShouldParseTransportTypeIfWebSocket() { var csBuilder = new ServiceBusConnectionStringBuilder("Endpoint=sb://contoso.servicebus.windows.net;SharedAccessKeyName=keyname;SharedAccessKey=key;TransportType=AmqpWebSockets"); Assert.Equal(TransportType.AmqpWebSockets, csBuilder.TransportType); }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { RegisterAppInsights(services); // Add framework services. services .AddCustomHealthCheck(Configuration) .AddMvc(options => { options.Filters.Add(typeof(HttpGlobalExceptionFilter)); }) .SetCompatibilityVersion(CompatibilityVersion.Version_2_2) .AddControllersAsServices(); //Injecting Controllers themselves thru DIFor further info see: http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services services.Configure <MarketingSettings>(Configuration); ConfigureAuthService(services); services.AddDbContext <MarketingContext>(options => { options.UseSqlServer(Configuration["ConnectionString"], sqlServerOptionsAction: sqlOptions => { sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name); //Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null); }); // Changing default behavior when client evaluation occurs to throw. // Default in EF Core would be to log a warning when client evaluation is performed. options.ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning)); //Check Client vs. Server evaluation: https://docs.microsoft.com/en-us/ef/core/querying/client-eval }); if (Configuration.GetValue <bool>("AzureServiceBusEnabled")) { services.AddSingleton <IServiceBusPersisterConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultServiceBusPersisterConnection> >(); var serviceBusConnectionString = Configuration["EventBusConnection"]; var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); return(new DefaultServiceBusPersisterConnection(serviceBusConnection, logger)); }); } else { services.AddSingleton <IRabbitMQPersistentConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >(); var factory = new ConnectionFactory() { HostName = Configuration["EventBusConnection"] }; if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) { factory.UserName = Configuration["EventBusUserName"]; } if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) { factory.Password = Configuration["EventBusPassword"]; } var retryCount = 5; if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) { retryCount = int.Parse(Configuration["EventBusRetryCount"]); } return(new DefaultRabbitMQPersistentConnection(factory, logger, retryCount)); }); } // Add framework services. services.AddSwaggerGen(options => { options.DescribeAllEnumsAsStrings(); options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "Marketing HTTP API", Version = "v1", Description = "The Marketing Service HTTP API", TermsOfService = "Terms Of Service" }); options.AddSecurityDefinition("oauth2", new OAuth2Scheme { Type = "oauth2", Flow = "implicit", AuthorizationUrl = $"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize", TokenUrl = $"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/token", Scopes = new Dictionary <string, string>() { { "marketing", "Marketing API" } } }); options.OperationFilter <AuthorizeCheckOperationFilter>(); }); services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder .SetIsOriginAllowed((host) => true) .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); }); RegisterEventBus(services); services.AddTransient <IMarketingDataRepository, MarketingDataRepository>(); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddTransient <IIdentityService, IdentityService>(); services.AddOptions(); //configure autofac var container = new ContainerBuilder(); container.Populate(services); return(new AutofacServiceProvider(container.Build())); }
private static string GetAmqpConnectionString(string connectionString) { if (string.IsNullOrEmpty(connectionString)) { throw new ApplicationException(ConnectionStringCannotBeNull); } var builder = new ServiceBusConnectionStringBuilder(connectionString) { TransportType = TransportType.Amqp }; return builder.ToString(); }
public int Run() { // Obtain management via .publishsettings file from https://manage.windowsazure.com/publishsettings/index?schemaversion=2.0 var creds = new CertificateCloudCredentials(SubscriptionId, ManagementCertificate); // Create Namespace var sbMgmt = new ServiceBusManagementClient(creds); ServiceBusNamespaceResponse nsResponse = null; Console.WriteLine("Creating Service Bus namespace {0} in location {1}", SBNamespace, Location); try { var resultSb = sbMgmt.Namespaces.Create(SBNamespace, Location); if (resultSb.StatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine("Error creating Service Bus namespace {0} in Location {1}: {2}", SBNamespace, Location, resultSb.StatusCode); return 1; } } catch (CloudException) { try { // There is (currently) no clean error code returned when the namespace already exists // Check if it does nsResponse = sbMgmt.Namespaces.Get(SBNamespace); Console.WriteLine("Service Bus namespace {0} already existed.", SBNamespace); } catch (Exception) { nsResponse = null; } if (nsResponse == null) { throw; } } // Wait until the namespace is active while (nsResponse == null || nsResponse.Namespace.Status != "Active") { nsResponse = sbMgmt.Namespaces.Get(SBNamespace); if (nsResponse.Namespace.Status == "Active") { break; } Console.WriteLine("Namespace {0} in state {1}. Waiting...", SBNamespace, nsResponse.Namespace.Status); System.Threading.Thread.Sleep(5000); } // Get the namespace connection string var nsDescription = sbMgmt.Namespaces.GetNamespaceDescription(SBNamespace); var nsConnectionString = nsDescription.NamespaceDescriptions.First( (d) => String.Equals(d.AuthorizationType, "SharedAccessAuthorization") ).ConnectionString; // Create EHs + device keys + consumer keys (WebSite*) var nsManager = NamespaceManager.CreateFromConnectionString(nsConnectionString); var ehDescriptionDevices = new EventHubDescription(EventHubNameDevices) { PartitionCount = 8, }; ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("D1", new List<AccessRights> { AccessRights.Send })); ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("D2", new List<AccessRights> { AccessRights.Send })); ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("D3", new List<AccessRights> { AccessRights.Send })); ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("D4", new List<AccessRights> { AccessRights.Send })); ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("WebSite", new List<AccessRights> { AccessRights.Manage, AccessRights.Listen, AccessRights.Send })); ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("StreamingAnalytics", new List<AccessRights> { AccessRights.Manage, AccessRights.Listen, AccessRights.Send })); Console.WriteLine("Creating Event Hub {0}", EventHubNameDevices); EventHubDescription ehDevices = null; do { try { ehDevices = nsManager.CreateEventHubIfNotExists(ehDescriptionDevices); } catch (System.UnauthorizedAccessException) { Console.WriteLine("Service Bus connection string not valid yet. Waiting..."); System.Threading.Thread.Sleep(5000); } } while (ehDevices == null); var ehDescriptionAlerts = new EventHubDescription(EventHubNameAlerts) { PartitionCount = 8, }; ehDescriptionAlerts.Authorization.Add(new SharedAccessAuthorizationRule("WebSite", new List<AccessRights> { AccessRights.Manage, AccessRights.Listen, AccessRights.Send })); ehDescriptionAlerts.Authorization.Add(new SharedAccessAuthorizationRule("StreamingAnalytics", new List<AccessRights> { AccessRights.Manage, AccessRights.Listen, AccessRights.Send })); Console.WriteLine("Creating Event Hub {0}", EventHubNameAlerts); var ehAlerts = nsManager.CreateEventHubIfNotExists(ehDescriptionAlerts); // Create Storage Account for Event Hub Processor var stgMgmt = new StorageManagementClient(creds); try { Console.WriteLine("Creating Storage Account {0} in location {1}", StorageAccountName, Location); var resultStg = stgMgmt.StorageAccounts.Create( new StorageAccountCreateParameters { Name = StorageAccountName.ToLowerInvariant(), Location = Location, AccountType = "Standard_LRS" }); if (resultStg.StatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine("Error creating storage account {0} in Location {1}: {2}", StorageAccountName, Location, resultStg.StatusCode); return 1; } } catch (CloudException ce) { if (String.Equals(ce.ErrorCode, "ConflictError", StringComparison.InvariantCultureIgnoreCase)) { Console.WriteLine("Storage account {0} already existed.", StorageAccountName); } else { throw; } } var keyResponse = stgMgmt.StorageAccounts.GetKeys(StorageAccountName.ToLowerInvariant()); if (keyResponse.StatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine("Error retrieving access keys for storage account {0} in Location {1}: {2}", StorageAccountName, Location, keyResponse.StatusCode); return 1; } var storageKey = keyResponse.PrimaryKey; string ehDevicesWebSiteConnectionString = new ServiceBusConnectionStringBuilder(nsConnectionString) { SharedAccessKeyName = "WebSite", SharedAccessKey = (ehDevices.Authorization.First((d) => String.Equals(d.KeyName, "WebSite", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey, }.ToString(); string ehAlertsWebSiteConnectionString = new ServiceBusConnectionStringBuilder(nsConnectionString) { SharedAccessKeyName = "WebSite", SharedAccessKey = (ehAlerts.Authorization.First((d) => String.Equals(d.KeyName, "WebSite", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey, }.ToString(); // Write a new web.config template file var doc = new XmlDocument(); doc.PreserveWhitespace = true; var inputFileName = (this.Transform ? "\\web.PublishTemplate.config" : "\\web.config"); var outputFileName = (this.Transform ? String.Format("\\web.{0}.config", NamePrefix) : "\\web.config"); doc.Load(WebSiteDirectory + inputFileName); doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.EventHubDevices']/@value").Value = EventHubNameDevices; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.EventHubAlerts']/@value").Value = EventHubNameAlerts; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionString']/@value").Value = nsConnectionString; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionStringDevices']/@value").Value = ehDevicesWebSiteConnectionString; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionStringAlerts']/@value").Value = ehAlertsWebSiteConnectionString; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.Storage.ConnectionString']/@value").Value = String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", StorageAccountName, storageKey); var outputFile = System.IO.Path.GetFullPath(WebSiteDirectory + outputFileName); doc.Save(outputFile); Console.WriteLine(); Console.WriteLine("Service Bus management connection string (i.e. for use in Service Bus Explorer):"); Console.WriteLine(nsConnectionString); Console.WriteLine(); Console.WriteLine("Device AMQP address strings (for Raspberry PI/devices):"); for (int i = 1; i <= 4; i++) { var deviceKeyName = String.Format("D{0}", i); var deviceKey = (ehDevices.Authorization.First((d) => String.Equals(d.KeyName, deviceKeyName, StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey; Console.WriteLine("amqps://{0}:{1}@{2}.servicebus.windows.net", deviceKeyName, Uri.EscapeDataString(deviceKey), SBNamespace); //Console.WriteLine(new ServiceBusConnectionStringBuilder(nsConnectionString) //{ // SharedAccessKeyName = deviceKeyName, // SharedAccessKey = deviceKey, //}.ToString()); } Console.WriteLine(); Console.WriteLine("Web.Config saved to {0}", outputFile); #if AZURESTREAMANALYTICS // Create StreamAnalyticsJobs + inputs + outputs + enter keys // Untested code. May require AAD authentication, no support for management cert? // Create Resource Group for the Stream Analytics jobs var groupCreateRequest = WebRequest.Create(String.Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}?api-version=2014-04-01-preview", SubscriptionId, StreamAnalyticsGroup)) as HttpWebRequest; groupCreateRequest.ClientCertificates.Add(creds.ManagementCertificate); groupCreateRequest.ContentType = "application/json"; groupCreateRequest.Method = "PUT"; groupCreateRequest.KeepAlive = true; var bytesGroup = Encoding.UTF8.GetBytes("{\"location\":\"Central US\"}"); groupCreateRequest.ContentLength = bytesGroup.Length; groupCreateRequest.GetRequestStream().Write(bytesGroup, 0, bytesGroup.Length); var groupCreateResponse = groupCreateRequest.GetResponse(); //var streamMgmt = new ManagementClient(creds); //, new Uri("https://management.azure.com")); //HttpClient client = streamMgmt.HttpClient; var createJob = new StreamAnalyticsJob() { location = Location, inputs = new List<StreamAnalyticsEntity> { new StreamAnalyticsEntity { name = "devicesInput", properties = new Dictionary<string,object> { { "type" , "stream" }, { "serialization" , new Dictionary<string,object> { { "type", "JSON"}, { "properties", new Dictionary<string, object> { { "encoding", "UTF8"}, } } } }, { "datasource", new Dictionary<string,object> { { "type", "Microsoft.ServiceBus/EventHub" }, { "properties", new Dictionary<string,object> { { "eventHubNamespace", Namespace }, { "eventHubName", EventHubDevices }, { "sharedAccessPolicyName", "StreamingAnalytics" }, { "sharedAccessPolicyKey", (ehDevices.Authorization.First( (d) => String.Equals(d.KeyName, "StreamingAnalytics", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey }, } } } } }, }, }, transformation = new StreamAnalyticsEntity() { name = "Aggregates", properties = new Dictionary<string,object> { { "streamingUnits", 1 }, { "query" , "select * from devicesInput" }, } }, outputs = new List<StreamAnalyticsEntity> { new StreamAnalyticsEntity { name = "output", properties = new Dictionary<string,object> { { "datasource", new Dictionary<string,object> { { "type", "Microsoft.ServiceBus/EventHub" }, { "properties", new Dictionary<string,object> { { "eventHubNamespace", Namespace }, { "eventHubName", EventHubAlerts }, { "sharedAccessPolicyName", "StreamingAnalytics" }, { "sharedAccessPolicyKey", (ehAlerts.Authorization.First( (d) => String.Equals(d.KeyName, "StreamingAnalytics", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey }, } } } }, { "serialization" , new Dictionary<string,object> { { "type", "JSON"}, { "properties", new Dictionary<string, object> { { "encoding", "UTF8"}, } } } }, }, }, } }; var jobCreateRequest = WebRequest.Create(String.Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}/Microsoft.StreamAnalytics/streamingjobs/{2}?api-version=2014-10-01", SubscriptionId, StreamAnalyticsGroup, JobAggregates)) as HttpWebRequest; jobCreateRequest.ClientCertificates.Add(creds.ManagementCertificate); jobCreateRequest.ContentType = "application/json"; jobCreateRequest.Method = "PUT"; jobCreateRequest.KeepAlive = true; var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(createJob)); jobCreateRequest.ContentLength = bytes.Length; jobCreateRequest.GetRequestStream().Write(bytes, 0, bytes.Length); var jobCreateResponse = jobCreateRequest.GetResponse(); //var jobCreateTask = streamMgmt.HttpClient.PutAsync( // String.Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}/Microsoft.StreamAnalytics/streamingjobs/{2}?api-version=2014-10-01", // SubscriptionId, StreamAnalyticsGroup, JobAggregates), // new StringContent(JsonConvert.SerializeObject(createJob))); //jobCreateTask.Wait(); //var jobCreateResponse = jobCreateTask.Result; #endif return 0; }
private static WcfCommunicationListener <TServiceContract> ResolveCommunicationListener <TServiceContract>( StatelessServiceContext context, TServiceContract wcfServiceObject, NetMessagingBinding netMessagingBinding, Func <string> queueNameProvider, Func <ServiceContext, string> connectionStringResolver, IEndpointBehavior[] behaviors) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (wcfServiceObject == null) { throw new ArgumentNullException(nameof(wcfServiceObject)); } if (netMessagingBinding == null) { throw new ArgumentNullException(nameof(netMessagingBinding)); } if (connectionStringResolver == null) { throw new ArgumentNullException(nameof(connectionStringResolver)); } if (behaviors == null) { throw new ArgumentNullException(nameof(behaviors)); } string listenConnectionString = connectionStringResolver(context); ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(listenConnectionString); Uri endpointUri; try { endpointUri = builder.Endpoints.SingleOrDefault(); } catch (InvalidOperationException) { throw new InvalidOperationException("More than one endpoint was detected in connection string"); } if (endpointUri == null) { throw new InvalidOperationException("No endpoint was detected in connection string"); } UriBuilder uriBuilder = new UriBuilder(endpointUri); var listener = new WcfCommunicationListener <TServiceContract>( wcfServiceObject: wcfServiceObject, serviceContext: context, // // The name of the endpoint configured in the ServiceManifest under the Endpoints section // that identifies the endpoint that the WCF ServiceHost should listen on. // address: new EndpointAddress(ServiceBusEnvironment.CreateServiceUri("sb", uriBuilder.Host.Split('.').First(), queueNameProvider == null ? typeof(TServiceContract).Name : queueNameProvider.Invoke())), // // Populate the binding information that you want the service to use. // listenerBinding: netMessagingBinding ); ServiceEndpoint serviceEndpoint = listener.ServiceHost.Description.Endpoints.Last(); serviceEndpoint.Behaviors.Add(new TransportClientEndpointBehavior { TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider( keyName: builder.SharedAccessKeyName, sharedAccessKey: builder.SharedAccessKey ) }); foreach (IEndpointBehavior behavior in behaviors) { serviceEndpoint.Behaviors.Add(behavior); } return(listener); }
/// <summary> /// Create a messaging factory from a connection string /// </summary> /// <param name="connectionString">Connection string</param> /// <returns>Messaging factory</returns> public static MessagingFactory CreateFromConnectionString(string connectionString) { ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString); Uri endpointAddress = builder.Endpoint; string sharedAccessKeyName = builder.SharedAccessKeyName; string sharedAccessKey = builder.SharedAccessKey; string sharedAccessSignature = builder.SharedAccessSignature; TokenProvider tokenProvider = null; if ((sharedAccessKeyName != null) && (sharedAccessKeyName != string.Empty)) { tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(sharedAccessKeyName, sharedAccessKey); } if ((sharedAccessSignature != null) && (sharedAccessSignature != string.Empty)) { tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(sharedAccessSignature); } return Create(endpointAddress, tokenProvider); }
private void AddEventing(IServiceCollection services) { services.AddSingleton <IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); var connection = Configuration.GetValue <string>("ServiceBusConnection"); var connectionBlob = Configuration.GetValue <string>("BlobConnection"); if (string.IsNullOrWhiteSpace(connection)) { // _logger.LogError("Error configuring eventing. Service Bus connection settings are missing"); } else { // _logger.LogInformation($"Retrieved Service Bus connection settings"); } services.AddSingleton <IBlobConnection>(sp => { var logger = sp.GetRequiredService <ILogger <BlobConnection> >(); return(new BlobConnection(connectionBlob)); }); services.AddSingleton <IEventStorage, EventsBlobStorage>(sp => { var blobStoragePersisterConnection = sp.GetRequiredService <IBlobConnection>(); var logger = sp.GetRequiredService <ILogger <EventsBlobStorage> >(); return(new EventsBlobStorage(blobStoragePersisterConnection, logger)); }); // Configure Service Bus Provider - RabbitMq or Azure Service Bus based on environment variables if (Configuration.GetValue <bool>("UseAzureServiceBus")) { // _logger.LogInformation($"Configuring Azure service bus"); services.AddSingleton <IServiceBusPersisterConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultServiceBusPersisterConnection> >(); var serviceBusConnection = new ServiceBusConnectionStringBuilder(connection); return(new DefaultServiceBusPersisterConnection(serviceBusConnection, logger)); }); services.AddSingleton <IEventBus, EventBusServiceBus>(sp => { var serviceBusPersisterConnection = sp.GetRequiredService <IServiceBusPersisterConnection>(); var logger = sp.GetRequiredService <ILogger <EventBusServiceBus> >(); var eventBusSubcriptionsManager = sp.GetRequiredService <IEventBusSubscriptionsManager>(); var subscriptionClientName = Configuration["SubscriptionClientName"]; return(new EventBusServiceBus(serviceBusPersisterConnection, logger, eventBusSubcriptionsManager, subscriptionClientName, sp)); }); } else { // _logger.LogInformation($"Configuring Rabbitmq"); services.AddSingleton <IRabbitMQPersistentConnection>(sp => { var settings = sp.GetRequiredService <IOptions <AuthorConfigurationSettings> >().Value; var logger = sp.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >(); var factory = new ConnectionFactory() { HostName = settings.ServiceBusConnection //"my-rabbit" }; //if (!string.IsNullOrEmpty(settings.ServiceBusUserName)) //{ // factory.UserName = settings.ServiceBusUserName; //} //if (!string.IsNullOrEmpty(settings.ServiceBusUserPassword)) //{ // factory.Password = settings.ServiceBusUserPassword; //} var retryCount = 5; return(new DefaultRabbitMQPersistentConnection(factory, logger, retryCount)); }); services.AddSingleton <IEventBus, EventBusRabbitMQ>(sp => { var rabbitMQPersistentConnection = sp.GetRequiredService <IRabbitMQPersistentConnection>(); var logger = sp.GetRequiredService <ILogger <EventBusRabbitMQ> >(); var eventBusSubcriptionsManager = sp.GetRequiredService <IEventBusSubscriptionsManager>(); var retryCount = 5; return(new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, eventBusSubcriptionsManager, sp, retryCount)); }); } // _logger.LogInformation($"Finished configuring service bus"); // Register Event Handlers //EX: }
private bool CreateWeb( CloudWebDeployInputs inputs ) { Console.WriteLine( "Retrieving namespace metadata..." ); // Create Namespace ServiceBusManagementClient sbMgmt = new ServiceBusManagementClient( inputs.Credentials ); var nsDescription = sbMgmt.Namespaces.GetNamespaceDescription( inputs.SBNamespace ); string nsConnectionString = nsDescription.NamespaceDescriptions.First( ( d ) => String.Equals( d.AuthorizationType, "SharedAccessAuthorization" ) ).ConnectionString; NamespaceManager nsManager = NamespaceManager.CreateFromConnectionString( nsConnectionString ); StorageManagementClient stgMgmt = new StorageManagementClient( inputs.Credentials ); var keyResponse = stgMgmt.StorageAccounts.GetKeys( inputs.StorageAccountName.ToLowerInvariant( ) ); if( keyResponse.StatusCode != System.Net.HttpStatusCode.OK ) { Console.WriteLine( "Error retrieving access keys for storage account {0} in Location {1}: {2}", inputs.StorageAccountName, inputs.Location, keyResponse.StatusCode ); return false; } var storageKey = keyResponse.PrimaryKey; EventHubDescription ehDevices = nsManager.GetEventHub( inputs.EventHubNameDevices ); string ehDevicesWebSiteConnectionString = new ServiceBusConnectionStringBuilder( nsConnectionString ) { SharedAccessKeyName = "WebSite", SharedAccessKey = ( ehDevices.Authorization.First( ( d ) => String.Equals( d.KeyName, "WebSite", StringComparison.InvariantCultureIgnoreCase) ) as SharedAccessAuthorizationRule ).PrimaryKey, }.ToString( ); string ehAlertsWebSiteConnectionString = string.Empty; try { EventHubDescription ehAlerts = nsManager.GetEventHub( inputs.EventHubNameAlerts ); ehAlertsWebSiteConnectionString = new ServiceBusConnectionStringBuilder( nsConnectionString ) { SharedAccessKeyName = "WebSite", SharedAccessKey = ( ehAlerts.Authorization.First( ( d ) => String.Equals( d.KeyName, "WebSite", StringComparison.InvariantCultureIgnoreCase ) ) as SharedAccessAuthorizationRule ).PrimaryKey, }.ToString( ); } catch { } Console.WriteLine( "Started processing..." ); // Write a new web.config template file var doc = new XmlDocument { PreserveWhitespace = true }; //var inputFileName = ( inputs.Transform ? "\\web.PublishTemplate.config" : "\\web.config" ); string inputFileName = "web.PublishTemplate.config"; //var outputFileName = ( inputs.Transform ? String.Format("\\web.{0}.config", inputs.NamePrefix) : "\\web.config" ); string outputFileName = "web.config"; //doc.Load( inputs.WebSiteDirectory + inputFileName ); string inputFilePath = Environment.CurrentDirectory +@"\"; Console.WriteLine("Opening and updating " + inputFilePath + inputFileName); doc.Load( inputFilePath + inputFileName ); doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.EventHubDevices']/@value" ).Value = inputs.EventHubNameDevices; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.EventHubAlerts']/@value" ) .Value = inputs.EventHubNameAlerts; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionString']/@value" ).Value = nsConnectionString; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionStringDevices']/@value" ).Value = ehDevicesWebSiteConnectionString; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionStringAlerts']/@value" ).Value = ehAlertsWebSiteConnectionString; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.Storage.ConnectionString']/@value" ) .Value = String.Format( "DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", inputs.StorageAccountName, storageKey ); //var outputFile = System.IO.Path.GetFullPath( inputs.WebSiteDirectory + outputFileName ); string outputFilePath = Environment.GetFolderPath( Environment.SpecialFolder.Desktop ); //Console.WriteLine(outputFilePath); var outputFile = outputFilePath + @"\" + outputFileName; Console.WriteLine( "Writing updates to " + outputFile ); doc.Save( outputFile ); Console.WriteLine( " " ); Console.WriteLine( "Web config saved to {0}", outputFile ); Console.WriteLine( " " ); return true; }
public async Task RunTest() { var senderCxn = new ServiceBusConnectionStringBuilder(targetNamespaceConnectionString) { EntityPath = targetQueue }; var sendSideClient = new Microsoft.Azure.ServiceBus.Core.MessageSender(senderCxn); var receiverCxn = new ServiceBusConnectionStringBuilder(sourceNamespaceConnectionString) { EntityPath = sourceQueue }; var receiveSideClient = new Microsoft.Azure.ServiceBus.Core.MessageReceiver(receiverCxn); var start = DateTime.UtcNow; var sw = new Stopwatch(); sw.Start(); var tracker = new ConcurrentDictionary <string, long>(); Console.WriteLine("sending"); List <Task> sendTasks = new List <Task>(); for (int j = 0; j < 1000; j++) { string msgid = Guid.NewGuid().ToString(); tracker[msgid] = sw.ElapsedTicks; var message = new Message(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }); message.MessageId = msgid; sendTasks.Add(sendSideClient.SendAsync(message)); } await Task.WhenAll(sendTasks); ConcurrentBag <long> durations = new ConcurrentBag <long>(); Console.Write("receiving: "); var receiveTask = Task.Run(async() => { while (!tracker.IsEmpty) { var message = await receiveSideClient.ReceiveAsync(100, TimeSpan.FromSeconds(30)); if (message != null) { foreach (var msg in message) { string msgid = msg.MessageId; if (tracker.TryRemove(msgid, out var swval)) { durations.Add(sw.ElapsedTicks - swval); } await receiveSideClient.CompleteAsync(msg.SystemProperties.LockToken); } } else { break; } } }); await receiveTask; Console.WriteLine(); Assert.True(tracker.IsEmpty, $"tracker is not empty: {tracker.Count}"); Console.WriteLine(((double)durations.Sum() / (double)durations.Count) / TimeSpan.TicksPerMillisecond); }
public async Task EventHubConnectSend(int sendCount) { var cs = @"Endpoint=sb://yournamespace.servicebus.windows.net/;SharedAccessKeyName=YourPublisher;SharedAccessKey=12EHa5NNllnZBz6gksogvNfhketIokdTdjx4ZrTfu9U="; var builder = new ServiceBusConnectionStringBuilder(cs) { TransportType = TransportType.Amqp }; var client = EventHubClient.CreateFromConnectionString(builder.ToString(), "swiftsoftware-eh"); int sentCount = sendCount; string infoText; for (int i = 0; i < sendCount; i++) { try { if (MessageType.Text == "Sensor") { infoText = "Operating Within Expected Parameters"; } else { infoText = "Temperature Sensor Requires Calibration"; } Random rnd = new Random(); int intTemp = rnd.Next(15, 25); int intHum = rnd.Next(50, 80); var e = new Event { MessageType = MessageType.Text, Temp = intTemp, Humidity = intHum, Location = Location.Text, Room = Room.Text, Info = infoText }; var serializedString = JsonConvert.SerializeObject(e); var data = new EventData(Encoding.UTF8.GetBytes(serializedString)) { PartitionKey = rnd.Next(6).ToString() }; // Set user properties if needed data.Properties.Add("Type", "Telemetry_" + DateTime.Now.ToLongTimeString()); await client.SendAsync(data).ConfigureAwait(false); } catch (Exception exp) { Console.WriteLine("Error on send: " + exp.Message); sentCount--; } await Task.Delay(Convert.ToInt32(DelayTime.Text)); } MessageResult.Text = " " + sentCount.ToString() + " " + MessageType.Text + " Messages were successfully sent to Event Hub"; return; }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); }); if (Configuration.GetValue <string>("IsClusterEnv") == bool.TrueString) { services .AddSignalR() .AddRedis(Configuration["SignalrStoreConnectionString"]); } else { services.AddSignalR(); } if (Configuration.GetValue <bool>("AzureServiceBusEnabled")) { services.AddSingleton <IServiceBusPersisterConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultServiceBusPersisterConnection> >(); var serviceBusConnectionString = Configuration["EventBusConnection"]; var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); return(new DefaultServiceBusPersisterConnection(serviceBusConnection, logger)); }); } else { services.AddSingleton <IRabbitMQPersistentConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >(); var factory = new ConnectionFactory() { HostName = Configuration["EventBusConnection"] }; if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) { factory.UserName = Configuration["EventBusUserName"]; } if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) { factory.Password = Configuration["EventBusPassword"]; } var retryCount = 5; if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) { retryCount = int.Parse(Configuration["EventBusRetryCount"]); } return(new DefaultRabbitMQPersistentConnection(factory, logger, retryCount)); }); } ConfigureAuthService(services); RegisterEventBus(services); services.AddOptions(); //configure autofac var container = new ContainerBuilder(); container.RegisterModule(new ApplicationModule()); container.Populate(services); return(new AutofacServiceProvider(container.Build())); }
public DefaultServiceBusPersisterConnection(ServiceBusConnectionStringBuilder serviceBusConnectionStringBuilder) { _serviceBusConnectionStringBuilder = serviceBusConnectionStringBuilder ?? throw new ArgumentNullException(nameof(serviceBusConnectionStringBuilder)); _topicClient = new TopicClient(_serviceBusConnectionStringBuilder, RetryPolicy.Default); }
public static IServiceCollection AddCustomEventBus(this IServiceCollection services, IConfiguration Configuration) { var subscriptionClientName = Configuration["EventBus:SubscriptionClientName"]; if (Configuration.GetValue <bool>("AzureServiceBusEnabled")) { services.AddSingleton <IServiceBusPersisterConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultServiceBusPersisterConnection> >(); var serviceBusConnectionString = Configuration["EventBus:Connection"]; var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); return(new DefaultServiceBusPersisterConnection(serviceBusConnection, logger)); }); services.AddSingleton <IEventBus, EventBusServiceBus>(sp => { var serviceBusPersisterConnection = sp.GetRequiredService <IServiceBusPersisterConnection>(); var iLifetimeScope = sp.GetRequiredService <ILifetimeScope>(); var logger = sp.GetRequiredService <ILogger <EventBusServiceBus> >(); var eventBusSubcriptionsManager = sp.GetRequiredService <IEventBusSubscriptionsManager>(); return(new EventBusServiceBus(serviceBusPersisterConnection, logger, eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope)); }); } else { services.AddSingleton <IRabbitMQPersistentConnection>(sp => { var logger = sp.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >(); var factory = new ConnectionFactory() { HostName = Configuration["EventBus:Connection"], DispatchConsumersAsync = true }; if (!string.IsNullOrEmpty(Configuration["EventBus:UserName"])) { factory.UserName = Configuration["EventBus:UserName"]; } if (!string.IsNullOrEmpty(Configuration["EventBus:Password"])) { factory.Password = Configuration["EventBus:Password"]; } var retryCount = 5; if (!string.IsNullOrEmpty(Configuration["EventBus:RetryCount"])) { retryCount = int.Parse(Configuration["EventBus:RetryCount"]); } return(new DefaultRabbitMQPersistentConnection(factory, logger, retryCount)); }); services.AddSingleton <IEventBus, EventBusRabbitMQ>(sp => { var rabbitMQPersistentConnection = sp.GetRequiredService <IRabbitMQPersistentConnection>(); var iLifetimeScope = sp.GetRequiredService <ILifetimeScope>(); var logger = sp.GetRequiredService <ILogger <EventBusRabbitMQ> >(); var eventBusSubcriptionsManager = sp.GetRequiredService <IEventBusSubscriptionsManager>(); var retryCount = 5; if (!string.IsNullOrEmpty(Configuration["EventBus:RetryCount"])) { retryCount = int.Parse(Configuration["EventBus:RetryCount"]); } return(new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount)); }); } services.AddSingleton <IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); return(services); }