/// <summary>
        /// Registers an <see cref="IApplicationInstanceIdRenter"/> that provides an ID for this application instance.
        /// Use the options to specify an implementation.
        /// </summary>
        public static IServiceCollection AddApplicationInstanceIdSource(this IServiceCollection services, Action <Options> sourceOptions)
        {
            var optionsObject = new Options(services);

            // The source MUST be registered first, since extensions on the options may overwrite the registration
            // The source itself MUST be singleton, to establish one value for the entire application
            // Everything else should be transient, to allow any lifetime of the dependencies for renting and returning the ID
            services.AddSingleton <IApplicationInstanceIdSource, DefaultApplicationInstanceIdSource>();

            sourceOptions(optionsObject);

            if (!services.Any(service => service.ServiceType == typeof(IApplicationInstanceIdRenter)))
            {
                throw new ArgumentException($"Use the options to specify an implementation.");
            }

            var exceptionHandlerFactory = optionsObject.ExceptionHandlerFactory;

            services.AddTransient(CreateExceptionHandler);

            return(services);

            // Local function that returns a new IExceptionHandler instance
            IExceptionHandler CreateExceptionHandler(IServiceProvider serviceProvider)
            {
                var handleExceptionAction = exceptionHandlerFactory?.Invoke(serviceProvider);
                var exceptionHandler      = new OptionalExceptionHandler(handleExceptionAction);

                return(exceptionHandler);
            }
        }
Beispiel #2
0
        public void HandleException(Object targetObject, FastMethod fastMethod, Exception ex, Object[] parameters)
        {
            Log.Error("Exception encountered: " + ex.Message, ex);

            if (OptionalExceptionHandler != null)
            {
                OptionalExceptionHandler.Handle(
                    new EPDataFlowExceptionContext(
                        DataFlowName, OperatorName, OperatorNumber, OperatorPrettyPrint, ex));
            }
        }
Beispiel #3
0
        public void RentId_WithOnePriorInvocation_ShouldReturnId2()
        {
            var exceptionHandler      = new OptionalExceptionHandler(null);
            var connectionFactory     = new ApplicationInstanceIdSourceDbConnectionFactory(this.Host.Services, _ => this.CreateDbConnection());
            var transactionalExecutor = new SqlTransactionalExecutor(connectionFactory);
            var otherSource           = new StandardSqlApplicationInstanceIdRenter(this.Host.Services, databaseName: null);

            otherSource.RentId();

            var id = this.Renter.RentId();

            Assert.Equal(2, id);
        }