public static void Run([ServiceBusTrigger("scmtopic", "scmcontactsearch", Connection = "ServiceBusConnectionString", IsSessionsEnabled = true)] ContactMessage msg, ILogger log, ExecutionContext context) { _context = context; ServiceProvider.GetRequiredService <ContactIndexerProcessor>().Process(msg).GetAwaiter().GetResult(); log.LogInformation($"C# ServiceBus topic trigger function processed message: {msg.EventType}"); }
public static bool IsAddedMessage(this ContactMessage msg) { return(msg.EventType == "ContactAddedEvent"); }
public async Task Process(ContactMessage msg) { var client = new SearchServiceClient(_options.ServiceName, new SearchCredentials(_options.AdminApiKey)); // try to create index once if (!_indexExists) { lock (_syncRoot) { if (!_indexExists) { if (!client.Indexes.ExistsAsync(_options.IndexName).Result) { var definition = new Index() { Name = _options.IndexName, Fields = FieldBuilder.BuildForType <Contact>(new DefaultContractResolver() { NamingStrategy = new JsonLowercaseNamingStrategy() }) }; client.Indexes.CreateAsync(definition).Wait(); } _indexExists = true; } } } var action = default(IndexAction <Contact>); if (msg.IsAddedMessage()) { action = IndexAction.Upload(_mapper.Value.Map <Contact>(msg)); } else if (msg.IsChangedMessage()) { action = IndexAction.MergeOrUpload(_mapper.Value.Map <Contact>(msg)); } else if (msg.IsDeletedMessage()) { action = IndexAction.Delete(_mapper.Value.Map <Contact>(msg)); } else { throw new InvalidOperationException($"Meesage type {msg.EventType} not supported."); } try { var indexClient = client.Indexes.GetClient(_options.IndexName); indexClient.DeserializationSettings.ContractResolver = new DefaultContractResolver() { NamingStrategy = new JsonLowercaseNamingStrategy() }; await indexClient.Documents.IndexAsync(IndexBatch.New(new[] { action })); } catch (IndexBatchException) { } await Task.Delay(0); }