示例#1
0
    public override void Configure(IFunctionsHostBuilder builder)
    {
        var services = builder.Services;

        // register custom service in the container
        services.AddSingleton(_ =>
        {
            var configurationRoot = builder.GetContext().Configuration;
            var customComponentInitializationValue = configurationRoot.GetValue <string>("CustomComponentValue");

            return(new CustomComponent(customComponentInitializationValue));
        });

        builder.UseNServiceBus(() =>
        {
            var configuration = new StorageQueueTriggeredEndpointConfiguration(AzureStorageQueueTriggerFunction.EndpointName);

            configuration.UseSerialization <NewtonsoftSerializer>();

            // Disable persistence requirement
            configuration.Transport.DisablePublishing();

            // optional: log startup diagnostics using Functions provided logger
            configuration.LogDiagnostics();

            return(configuration);
        });
    }
    public static void DisablePublishing(StorageQueueTriggeredEndpointConfiguration endpointConfiguration)
    {
        #region asq-disable-publishing

        endpointConfiguration.Transport.DisablePublishing();

        #endregion
    }
    StaticUsage(StorageQueueTriggeredEndpointConfiguration storageQueueTriggeredEndpointConfiguration, FunctionExecutionContext executionContext)
    {
        #region asq-enable-diagnostics

        storageQueueTriggeredEndpointConfiguration.LogDiagnostics();

        #endregion
    }
    public static void EnablePersistence(StorageQueueTriggeredEndpointConfiguration endpointConfiguration)
    {
        #region asq-enable-persistence

        var persistence = endpointConfiguration.AdvancedConfiguration.UsePersistence <AzureStoragePersistence>();
        persistence.ConnectionString("<connection-string>");

        #endregion
    }
    public static void EnableDelayedRetries(StorageQueueTriggeredEndpointConfiguration endpointConfiguration, int numberOfDelayedRetries, TimeSpan timeIncreaseBetweenDelayedRetries)
    {
        #region asq-enable-delayed-retries

        var recoverability = endpointConfiguration.AdvancedConfiguration.Recoverability();
        recoverability.Delayed(settings =>
        {
            settings.NumberOfRetries(numberOfDelayedRetries);
            settings.TimeIncrease(timeIncreaseBetweenDelayedRetries);
        });

        #endregion

        #region asq-configure-error-queue

        endpointConfiguration.AdvancedConfiguration.SendFailedMessagesTo("error");

        #endregion
    }
        public async Task Should_load_handlers_from_assembly_when_using_FunctionsHostBuilder()
        {
            // The message handler assembly shouldn't be loaded at this point because there is no reference in the code to it.
            Assert.False(AppDomain.CurrentDomain.GetAssemblies().Any(a => a.FullName == "Testing.Handlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"));

            var serviceCollection = new ServiceCollection();

            var configuration = new StorageQueueTriggeredEndpointConfiguration("assemblyTest");

            configuration.UseSerialization <XmlSerializer>();
            configuration.Transport.DisablePublishing();
            configuration.EndpointConfiguration.UsePersistence <InMemoryPersistence>();

            var settings = configuration.AdvancedConfiguration.GetSettings();

            var endpointFactory = FunctionsHostBuilderExtensions.Configure(configuration, serviceCollection,
                                                                           Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ExternalHandlers"));

            var serviceProvider = serviceCollection.BuildServiceProvider();
            var endpoint        = endpointFactory(serviceProvider);


            // we need to process an actual message to have the endpoint being created
            await endpoint.Process(GenerateMessage(), new ExecutionContext());

            // The message handler assembly should be loaded now because scanning should find and load the handler assembly
            Assert.True(AppDomain.CurrentDomain.GetAssemblies().Any(a => a.FullName == "Testing.Handlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"));

            // Verify the handler and message type have been identified and loaded:
            var registry         = settings.Get <MessageHandlerRegistry>();
            var dummyMessageType = registry.GetMessageTypes().FirstOrDefault(t => t.FullName == "Testing.Handlers.DummyMessage");

            Assert.NotNull(dummyMessageType);
            var dummyMessageHandler = registry.GetHandlersFor(dummyMessageType).SingleOrDefault();

            Assert.AreEqual("Testing.Handlers.DummyMessageHandler", dummyMessageHandler.HandlerType.FullName);

            // ensure the assembly is loaded into the right context
            Assert.AreEqual(AssemblyLoadContext.GetLoadContext(Assembly.GetExecutingAssembly()), AssemblyLoadContext.GetLoadContext(dummyMessageType.Assembly));
        }
            public override Task Start(CancellationToken token)
            {
                endpoint = new TestableFunctionEndpoint(context =>
                {
                    var functionEndpointConfiguration = new StorageQueueTriggeredEndpointConfiguration(Name);

                    // Tests are not exercising pub/sub and processing pipeline is the same as with the regular endpoints (message-driven pub/sub should work as usual)
                    functionEndpointConfiguration.Transport.DisablePublishing();

                    var endpointConfiguration = functionEndpointConfiguration.AdvancedConfiguration;

                    endpointConfiguration.TypesToIncludeInScan(functionComponentType.GetTypesScopedByTestClass());

                    endpointConfiguration.Recoverability()
                    .Immediate(i => i.NumberOfRetries(0))
                    .Delayed(d => d.NumberOfRetries(0))
                    .Failed(c => c
                            // track messages sent to the error queue to fail the test
                            .OnMessageSentToErrorQueue(failedMessage =>
                    {
                        scenarioContext.FailedMessages.AddOrUpdate(
                            Name,
                            new[] { failedMessage },
                            (_, fm) =>
                        {
                            var messages = fm.ToList();
                            messages.Add(failedMessage);
                            return(messages);
                        });
                        return(Task.CompletedTask);
                    }));

                    endpointConfiguration.RegisterComponents(c => c.RegisterSingleton(scenarioContext.GetType(), scenarioContext));

                    configurationCustomization(functionEndpointConfiguration);
                    return(functionEndpointConfiguration);
                });

                return(Task.CompletedTask);
            }