in src/WebJobs.Extensions.CosmosDB/Trigger/CosmosDBTriggerAttributeBindingProvider.cs [40:128]
public async Task<ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context)
{
// Tries to parse the context parameters and see if it belongs to this [CosmosDBTrigger] binder
if (context == null)
{
throw new ArgumentNullException("context");
}
ParameterInfo parameter = context.Parameter;
CosmosDBTriggerAttribute attribute = parameter.GetCustomAttribute<CosmosDBTriggerAttribute>(inherit: false);
if (attribute == null)
{
return null;
}
Container monitoredContainer;
Container leasesContainer;
string monitoredDatabaseName = ResolveAttributeValue(attribute.DatabaseName);
string monitoredCollectionName = ResolveAttributeValue(attribute.ContainerName);
string leasesDatabaseName = ResolveAttributeValue(attribute.LeaseDatabaseName);
string leasesCollectionName = ResolveAttributeValue(attribute.LeaseContainerName);
string processorName = ResolveAttributeValue(attribute.LeaseContainerPrefix) ?? string.Empty;
string preferredLocations = ResolveAttributeValue(attribute.PreferredLocations);
try
{
string triggerConnection = ResolveAttributeConnection(attribute);
if (string.IsNullOrEmpty(triggerConnection))
{
throw new InvalidOperationException($"The attribute {nameof(attribute.Connection)} for the monitored container is in an invalid format, please use AccountEndpoint=XXXXXX;AccountKey=XXXXXX; or a node representing token authentication information.");
}
string leasesConnection = ResolveAttributeLeasesConnection(attribute);
if (string.IsNullOrEmpty(leasesConnection))
{
throw new InvalidOperationException($"The attribute {nameof(attribute.LeaseConnection)} for the leases container is in an invalid format, please use AccountEndpoint=XXXXXX;AccountKey=XXXXXX;. or a node representing token authentication information.");
}
if (string.IsNullOrEmpty(monitoredDatabaseName)
|| string.IsNullOrEmpty(monitoredCollectionName)
|| string.IsNullOrEmpty(leasesDatabaseName)
|| string.IsNullOrEmpty(leasesCollectionName))
{
throw new InvalidOperationException("Cannot establish database and container values. If you are using environment and configuration values, please ensure these are correctly set.");
}
if (triggerConnection.Equals(leasesConnection, StringComparison.InvariantCultureIgnoreCase)
&& monitoredDatabaseName.Equals(leasesDatabaseName, StringComparison.InvariantCultureIgnoreCase)
&& monitoredCollectionName.Equals(leasesCollectionName, StringComparison.InvariantCultureIgnoreCase))
{
throw new InvalidOperationException("The monitored container cannot be the same as the container storing the leases.");
}
CosmosClient monitoredCosmosDBService = _configProvider.GetService(
connection: triggerConnection,
preferredLocations: preferredLocations,
userAgent: CosmosDBTriggerUserAgentSuffix);
CosmosClient leaseCosmosDBService = _configProvider.GetService(
connection: leasesConnection,
preferredLocations: preferredLocations,
userAgent: CosmosDBTriggerUserAgentSuffix);
if (attribute.CreateLeaseContainerIfNotExists)
{
await CreateLeaseCollectionIfNotExistsAsync(leaseCosmosDBService, leasesDatabaseName, leasesCollectionName, attribute.LeasesContainerThroughput);
}
monitoredContainer = monitoredCosmosDBService.GetContainer(monitoredDatabaseName, monitoredCollectionName);
leasesContainer = leaseCosmosDBService.GetContainer(leasesDatabaseName, leasesCollectionName);
if (!string.IsNullOrEmpty(attribute.StartFromTime))
{
attribute.StartFromTime = ResolveAttributeValue(attribute.StartFromTime);
}
}
catch (Exception ex)
{
throw new InvalidOperationException(string.Format("Cannot create container information for {0} in database {1} with lease {2} in database {3} : {4}", attribute.ContainerName, attribute.DatabaseName, attribute.LeaseContainerName, attribute.LeaseDatabaseName, ex.Message), ex);
}
return new CosmosDBTriggerBinding<T>(
parameter,
processorName,
monitoredContainer,
leasesContainer,
attribute,
_drainModeManager,
_logger);
}