/// <summary> /// This is the entry point of the service host process. /// </summary> private static void Main() { try { var locator = new BrokerServiceLocator(); var publisherActorHelper = new PublisherActorHelper(locator); ActorRuntime.RegisterActorAsync <PublishingActor>( (context, actorType) => new ActorService(context, actorType, (svc, id) => new PublishingActor(svc, id, publisherActorHelper))).GetAwaiter().GetResult(); Thread.Sleep(Timeout.Infinite); // Prevents this host process from terminating so services keeps running. } catch (Exception e) { ActorEventSource.Current.ActorHostInitializationFailed(e); throw; } }
/// <summary> /// Publish the configured amount of messages. /// </summary> /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service instance.</param> protected override async Task RunAsync(CancellationToken cancellationToken) { int delay; string setting = GetConfigurationValue(Context, "MessageSettings", "InitialDelaySec"); if (!string.IsNullOrWhiteSpace(setting) && int.TryParse(setting, out delay)) { ServiceEventSource.Current.ServiceMessage(this, $"Sleeping for {delay} seconds."); await Task.Delay(TimeSpan.FromSeconds(delay), cancellationToken); } int ammount; setting = GetConfigurationValue(Context, "MessageSettings", "Amount"); if (string.IsNullOrWhiteSpace(setting) || !int.TryParse(setting, out ammount)) { return; } int messageTypeCount; setting = GetConfigurationValue(Context, "MessageSettings", "MessageTypeCount"); if (string.IsNullOrWhiteSpace(setting) || !int.TryParse(setting, out messageTypeCount)) { return; } AssemblyName asmName = new AssemblyName(); asmName.Name = "Dynamic"; AssemblyBuilder asmBuild = Thread.GetDomain().DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave); ModuleBuilder modBuild = asmBuild.DefineDynamicModule("Module", "Dynamic.dll"); List <Type> types = new List <Type>(messageTypeCount); for (int i = 0; i < messageTypeCount; i++) { string messageTypeName = "DataContract" + i; TypeBuilder tb = modBuild.DefineType(messageTypeName, TypeAttributes.Public, typeof(Common.DataContracts.DataContract)); tb.DefineDefaultConstructor(MethodAttributes.Public); types.Add(tb.CreateType()); ServiceEventSource.Current.ServiceMessage(this, $"Created Message Type {messageTypeName} ({i} of {messageTypeCount}."); } Dictionary <Type, List <object> > messagesPerType = new Dictionary <Type, List <object> >(); int basketCount = ammount / messageTypeCount; for (int i = 0; i < messageTypeCount; i++) { var messages = new List <object>(); messagesPerType.Add(types[i], messages); for (int j = 0; j < basketCount; j++) { var message = Activator.CreateInstance(types[i]); messages.Add(message); } } var allMessages = messagesPerType.Values.SelectMany(l => l).ToArray(); var options = new ParallelOptions { MaxDegreeOfParallelism = -1 }; var helper = new PublisherServiceHelper(); var brokerSvcLocator = new BrokerServiceLocator(); var brokerSvcLocation = await brokerSvcLocator.LocateAsync(); //sending starts here: Stopwatch sw = Stopwatch.StartNew(); Parallel.For(0, allMessages.Length, options, i => { var message = allMessages[i]; helper.PublishMessageAsync(this, message, brokerSvcLocation).ConfigureAwait(false).GetAwaiter().GetResult(); }); sw.Stop(); ServiceEventSource.Current.ServiceMessage(this, $"In {sw.ElapsedMilliseconds}ms - Published {ammount} instances of Message Types {string.Join(", ", types.Select(t => t.FullName))}."); while (!cancellationToken.IsCancellationRequested) { await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken); } }