043 Service bus Consumer for PhbkDivisionView - chempkovsky/CS82ANGULAR GitHub Wiki

Notes

  • We have already created the service, which sends data to the bus.
  • We have already created the Helper class which updates the data to the
  • MassTransit introduces Consumer
  • Now we need the code, which receives data from the Service bus and calls the methods of the Helper class.
    • with 01025-.masstransit.consumer.cs-Wizard's script we will generate the code for the Service bus Consumer.
  • Since we have two database contexts, PhbkDbContext and LpPhbkDbContext, the question is which one to use to run the wizard script.
    • PhbkDbContext must be used to generate Lookup resource Service bus Consumer.

Steps required to accomplish the task

Create the Consumers folder to LpPhBkControllers project

  • with Visual Studio add Consumers-folder to LpPhBkControllers project
Click to show the picture

project structure

Repeat the steps

Fourth page of the Wizard

  • On the Fourth page of the Wizard select 01025-.masstransit.consumer.cs-script type and click Next-button

Fifth page of the Wizard

  • On the Fifth page of the Wizard select masstransit.consumer.cs.t4-script and click Next-button

Sixth page of the Wizard

  • On the Sixth page of the Wizard click Save-button. close the Wizard
  • phbk-division-view.masstransit.consumer.cs-file will be created.

Open generated file

  • open phbk-division-view.masstransit.consumer.cs-file
Click to show the picture

project structure

  • We need to make modifications of the phbk-division-view.masstransit.consumer.cs-file
Find and replace
  • replace the using PhBkContext.PhBk;-line with the using LpPhBkContext.PhBk;

  • replace the PhbkDbContext db-code with the code LpPhbkDbContext db in the phbk-division-view.masstransit.consumer.cs-file

  • replace the PhbkDbContext dbcontext-code with the code LpPhbkDbContext dbcontext in the phbk-division-view.masstransit.consumer.cs-file

  • Note: The Lookup resource Helper and Consumer Classes are the only two classes that need to be modified after generation. We can not use LpPhbkDbContext to generate phbk-division-view.masstransit.consumer.cs-file. Without additional foreign keys, the Wizard will not be able to recognize the group of tables as a lookup resource. To be recognizable as a lookup resource, LprDivision01 and LprDivision02 tables must have a foreign key, which references PhbkDivision-table. In addition LprDivision02 must have a foreign key, which references PhbkEnterprise-table... In short, find and replace-approach is optimal.

Configuring MassTransit for an Application

  • open phbk-division-view.masstransit.consumer.cs-file
    • at the beginning of the file you will find instruction of how to setup the app to connect to Service bus
Click to show the code
...
/*
    according to https://masstransit-project.com/usage/configuration.html#configuration
    make sure Program.cs file contains the following code:

#region MassTransit config
using MassTransit;
#endregion
...
var builder = WebApplication.CreateBuilder(args);
...

#region MassTransit config
builder.Services.AddMassTransit(x => {

    x.AddConsumer<PhbkDivisionViewExtForLkUpMsgConsumer>(typeof(PhbkDivisionViewExtForLkUpMsgConsumerDefinition));
        //.Endpoint(e =>
        //{
            // override the default endpoint name
            // e.Name =   phbk-division-view;

            // specify the endpoint as temporary (may be non-durable, auto-delete, etc.)
            e.Temporary = false;

            // specify an optional concurrent message limit for the consumer
            e.ConcurrentMessageLimit = 8;

            // only use if needed, a sensible default is provided, and a reasonable
            // value is automatically calculated based upon ConcurrentMessageLimit if 
            // the transport supports it.
            e.PrefetchCount = 16;

            // set if each service instance should have its own endpoint for the consumer
            // so that messages fan out to each instance.
            e.InstanceId = "something-unique";
        //});

    x.UsingRabbitMq((context, configurator) => {
        configurator.Host("192.168.100.4", "RabbitMq_virtual_host_name", h =>
        {
            h.Username("RabbitMq_admin_name");
            h.Password("RabbitMq_admin_password");
            // 
            // Cluster settings
            //
            // h.UseCluster((configureCluster) =>
            // {
            //   configureCluster.Node("192.168.100.5");
            //   configureCluster.Node("192.168.100.6");
            //   ...
            //   configureCluster.Node("192.168.100.10");
            // });
            // h.PublisherConfirmation = true;
            //h.ConfigureBatchPublish(configure =>
            //{
            //});
        });
        // 
        // Quorum Queue settings
        //
        // configurator.SetQuorumQueue(3);
        //

        configurator.ConfigureEndpoints(context);

    });
});
builder.Services.AddOptions<MassTransitHostOptions>()
                .Configure(options =>
                {
                    // if specified, waits until the bus is started before
                    // returning from IHostedService.StartAsync
                    // default is false
                    options.WaitUntilStarted = true;

                    // if specified, limits the wait time when starting the bus
                    options.StartTimeout = TimeSpan.FromSeconds(10);

                    // if specified, limits the wait time when stopping the bus
                    options.StopTimeout = TimeSpan.FromSeconds(30);

                });
#endregion


*/
...
⚠️ **GitHub.com Fallback** ⚠️