private static async Task <Tuple <IMessagingHubClient, string> > GetClientForApplicationAsync(Func <Message, CancellationToken, Task> onMessageReceived, Func <Notification, CancellationToken, Task> onNotificationReceived, Func <Message, CancellationToken, Task> secondOnMessageReceived = null) { var appShortName = await CreateAndRegisterApplicationAsync(); var appAccessKey = await GetApplicationAccessKeyAsync(appShortName); var client = new MessagingHubClientBuilder() .UsingHostName("hmg.msging.net") .UsingPort(55321) .UsingAccessKey(appShortName, appAccessKey) .WithMaxConnectionRetries(1) .WithSendTimeout(Timeout) .Build(); if (onMessageReceived != null) { client.AddMessageReceiver(onMessageReceived); } if (secondOnMessageReceived != null) { client.AddMessageReceiver(secondOnMessageReceived); } client.AddNotificationReceiver(onNotificationReceived); return(new Tuple <IMessagingHubClient, string>(client, appShortName)); }
/// <summary> /// Initializes a new instance of the <see cref="TextcMessageReceiverBuilder"/> class. /// </summary> /// <param name="clientBuilder">The client builder.</param> /// <param name="outputProcessor">The output processor.</param> /// <param name="syntaxParser">The syntax parser.</param> /// <param name="expressionScorer">The expression scorer.</param> /// <param name="cultureProvider">The culture provider.</param> public TextcMessageReceiverBuilder( MessagingHubClientBuilder clientBuilder, IOutputProcessor outputProcessor = null, ISyntaxParser syntaxParser = null, IExpressionScorer expressionScorer = null, ICultureProvider cultureProvider = null) : this(clientBuilder.Build(), outputProcessor, syntaxParser, expressionScorer, cultureProvider) { }
public void TryToStartConnectionWithInvalidServer() { var client = new MessagingHubClientBuilder() .WithMaxConnectionRetries(1) .UsingHostName("invalid.iris.io") .UsingGuest() .WithSendTimeout(TimeSpan.FromSeconds(1)) .Build(); Should.ThrowAsync <TaskCanceledException>(client.StartAsync()); }
public async Task StartSuccessfully() { var client = new MessagingHubClientBuilder() .WithMaxConnectionRetries(1) .UsingHostName("hmg.msging.net") .UsingPort(55321) .UsingGuest() .Build(); await client.StartAsync().ConfigureAwait(false); }
/// <summary> /// Creates ans starts an application with the given settings. /// </summary> /// <param name="application">The application instance. If not defined, the class will look for an application.json file in the current directory.</param> /// <param name="loadAssembliesFromWorkingDirectory">if set to <c>true</c> indicates to the bootstrapper to load all assemblies from the current working directory.</param> /// <param name="path">Assembly path to load</param> /// <param name="builder">The builder instance to be used.</param> /// <param name="typeResolver">The type provider.</param> /// <returns></returns> /// <exception cref="System.IO.FileNotFoundException">Could not find the '{DefaultApplicationFileName}'</exception> /// <exception cref="System.ArgumentException">At least an access key or password must be defined</exception> /// <exception cref="FileNotFoundException"></exception> /// <exception cref="ArgumentException">At least an access key or password must be defined</exception> /// <exception cref="FileNotFoundException"></exception> /// <exception cref="ArgumentException">At least an access key or password must be defined</exception> public static async Task <IStoppable> StartAsync( Application application = null, bool loadAssembliesFromWorkingDirectory = true, string path = ".", MessagingHubClientBuilder builder = null, ITypeResolver typeResolver = null) { if (application == null) { if (!File.Exists(DefaultApplicationFileName)) { throw new FileNotFoundException($"Could not find the '{DefaultApplicationFileName}' file", DefaultApplicationFileName); } application = Application.ParseFromJsonFile(DefaultApplicationFileName); } if (loadAssembliesFromWorkingDirectory) { ReferencesUtil.LoadAssembliesAndReferences(path, assemblyFilter: ReferencesUtil.IgnoreSystemAndMicrosoftAssembliesFilter, ignoreExceptionLoadingReferencedAssembly: true); } if (builder == null) { builder = new MessagingHubClientBuilder(); } if (application.Identifier != null) { if (application.Password != null) { builder = builder.UsingPassword(application.Identifier, application.Password); } else if (application.AccessKey != null) { builder = builder.UsingAccessKey(application.Identifier, application.AccessKey); } else { throw new ArgumentException("At least an access key or password must be defined", nameof(application)); } } else { builder = builder.UsingGuest(); } if (application.Instance != null) { builder = builder.UsingInstance(application.Instance); } if (application.RoutingRule != null) { builder = builder.UsingRoutingRule(application.RoutingRule.Value); } if (application.Domain != null) { builder = builder.UsingDomain(application.Domain); } if (application.Scheme != null) { builder = builder.UsingScheme(application.Scheme); } if (application.HostName != null) { builder = builder.UsingHostName(application.HostName); } if (application.Port != null) { builder = builder.UsingPort(application.Port.Value); } if (application.SendTimeout != 0) { builder = builder.WithSendTimeout(TimeSpan.FromMilliseconds(application.SendTimeout)); } if (application.SessionEncryption.HasValue) { builder = builder.UsingEncryption(application.SessionEncryption.Value); } if (application.SessionCompression.HasValue) { builder = builder.UsingCompression(application.SessionCompression.Value); } if (application.Throughput != 0) { builder = builder.WithThroughput(application.Throughput); } if (application.ChannelBuffer != 0) { builder = builder.WithChannelBuffer(application.ChannelBuffer); } if (application.DisableNotify) { builder = builder.WithAutoNotify(false); } if (application.ChannelCount.HasValue) { builder = builder.WithChannelCount(application.ChannelCount.Value); } if (application.ReceiptEvents != null && application.ReceiptEvents.Any()) { builder = builder.WithReceiptEvents(application.ReceiptEvents); } else if (application.ReceiptEvents != null) { builder = builder.WithReceiptEvents(new[] { Event.Failed }); } if (typeResolver == null) { typeResolver = TypeResolver.Instance; } var localServiceProvider = BuildServiceProvider(application, typeResolver); localServiceProvider.RegisterService(typeof(MessagingHubClientBuilder), builder); var client = await BuildMessagingHubClientAsync(application, builder.Build, localServiceProvider, typeResolver); await client.StartAsync().ConfigureAwait(false); var stoppables = new IStoppable[2]; stoppables[0] = client; var startable = await BuildStartupAsync(application, localServiceProvider, typeResolver); if (startable != null) { stoppables[1] = startable as IStoppable; } return(new StoppableWrapper(stoppables)); }