065 Preparing the Lookup resource application to run - chempkovsky/CS82ANGULAR GitHub Wiki

Notes

  • Since, it's a demo project, we did not create separate WebApi app for the PhbkEmployeeView Lookup resource services, though it is highly recommended.
    • instead, we created a separate Dbcontext for the PhbkEmployeeView Lookup resource
    • instead, we created a separate consumer
    • instead, we will create a separate copy of the Service Bus in the LpPhBkWebApp-app with a separate section in the appsettings.json.
  • Having three "instead", it'll be very easy to migrate to the separate WebApi app for the PhbkEmployeeView Lookup resource services.
  • Detailed comments about configuration have already been made in the article 46
  • We do not describe step by step instructions in this article
    • the only information summary will be presented

Set up LpPhBkWebApp

appsettings json for LpPhBkWebApp

  • the following elements must be added to the appsettings.json file

    • connection string for the LpEmpPhBkContext dbcontext
    • PhbkEmployeeViewExtForLkUpConf-section to configure second instance of the Service Bus
  • We introduce ConnectionStrings:LpEmpPhBkConnection-item to configure LpEmpPhBkContext-DbContext

Click to show the code
  "ConnectionStrings": {
    "LpEmpPhBkConnection": "Data Source=SVR2016SQL2017;Initial Catalog=LpEmpPhBkDbDef;Persist Security Info=True;User ID=sa;Password=your_password_heer",
    "LpPhBkConnection": "Data Source=SVR2016SQL2017;Initial Catalog=LpPhBkDbDef;Persist Security Info=True;User ID=sa;Password=your_password_heer"
  },
  • open phbk-employee-view.extforlkup.interface.cs mentioned in the article 059. In the end of the file you will find the PhbkEmployeeViewExtForLkUpConf-class along with the instructions on how to use it
Click to show the code
...
/*
  In appsettings.json it must be added the section like below.
  1. If a RabbitMq cluster is present:

  "PhbkEmployeeViewExtForLkUpConf": {
    "HostName ": "192.168.100.3",
    "Username": "Admin",
    "Password": "Admin",
    "VirtualHostName": "phbkhost",
    "ClusterIpAddresses": [
      "192.168.100.4",
      "192.168.100.5",
      "192.168.100.6"
    ]
  }


  2. If a RabbitMq cluster is not present:

  "PhbkEmployeeViewExtForLkUpConf": {
    "HostName ": "192.168.100.3",
    "Username": "Admin",
    "Password": "Admin",
    "VirtualHostName": "phbkhost",
    "ClusterIpAddresses": []
  }

  3. In the Program.cs file add the code like below:

  var builder = WebApplication.CreateBuilder(args);
  ...
  ConfigurationManager configuration = builder.Configuration;
   ...
  var myOptions = new PhbkEmployeeViewExtForLkUpConf();
  configuration.GetSection(PhbkEmployeeViewExtForLkUpConf.ConfName).Bind(myOptions);
   ...

*/

    public class PhbkEmployeeViewExtForLkUpConf {
        public static string ConfName = "PhbkEmployeeViewExtForLkUpConf";
        public string HostName { get; set; } = String.Empty;
        public string Username { get; set; } = String.Empty;
        public string Password { get; set; } = String.Empty;
        public string VirtualHostName { get; set; } = String.Empty;
        public string[] ClusterIpAddresses { get; set; } = null!;
    }
...
  • We will use the same RabbitMq server, Vhost, user as those desribed in the article 046. Thus, the PhbkEmployeeViewExtForLkUpConf-section will be identical to the PhbkDivisionViewExtForLkUpConf-section.
Click to show the code
  "PhbkDivisionViewExtForLkUpConf": {
    "HostName": "192.168.100.3",
    "Username": "admin",
    "Password": "admin",
    "VirtualHostName": "phbkhost",
    "ClusterIpAddresses": []
  },
  "PhbkEmployeeViewExtForLkUpConf": {
    "HostName": "192.168.100.3",
    "Username": "admin",
    "Password": "admin",
    "VirtualHostName": "phbkhost",
    "ClusterIpAddresses": []
  }

Program file for LpPhBkWebApp

  • Open Program.cs-file of the LpPhBkWebApp-project and register LpPhbkDbContext-context
Click to show the code
...
var builder = WebApplication.CreateBuilder(args);
...
ConfigurationManager configuration = builder.Configuration;
...
builder.Services.AddDbContext<LpEmpPhBkContext>(options =>
    options.UseSqlServer(configuration.GetConnectionString("LpEmpPhBkConnection")));
  • to configure the second instance of the MassTransit Service Bus
    • read the article first. (It gives an example configuration)
  • Add the IBusLpPhbkEmployee.cs-file in the Consumers-folder of the LpPhBkControllers.csproj-project with a code
Click to show the code
using MassTransit;
namespace LpPhBkControllers.Consumers
{
    public interface IBusLpPhbkEmployee: IBus
    {
    }
}
  • Back to the Program.cs file
    • we get the PhbkEmployeeViewExtForLkUpConf-section from appsettings.json-file
    • using PhbkEmployeeViewExtForLkUpConf-section we configure second instance of the MassTransit Service Bus
      • builder.Services.AddMassTransit<IBusLpPhbkEmployee>-code declares the second copy of the Service Bus.
Click to show the code
#region MassTransit config
...
var phbkEmployeeViewExtForLkUpConf = new PhbkEmployeeViewExtForLkUpConf();
configuration.GetSection(PhbkEmployeeViewExtForLkUpConf.ConfName).Bind(phbkEmployeeViewExtForLkUpConf);
builder.Services.AddMassTransit<IBusLpPhbkEmployee>(x => {
    x.AddConsumer<PhbkDivisionViewExtForLkUpMsgConsumer>(typeof(PhbkEmployeeViewExtForLkUpMsgConsumerDefinition));
    //.Endpoint(e => { 
    //    e.Name = "phbk-division-view"; 
    //});

    x.UsingRabbitMq((context, configurator) => {
        configurator.Host(phbkEmployeeViewExtForLkUpConf.HostName, phbkEmployeeViewExtForLkUpConf.VirtualHostName, h => {
            h.Username(phbkEmployeeViewExtForLkUpConf.Username);
            h.Password(phbkEmployeeViewExtForLkUpConf.Password);
            if (phbkEmployeeViewExtForLkUpConf.ClusterIpAddresses != null)
            {
                if (phbkEmployeeViewExtForLkUpConf.ClusterIpAddresses.Length > 0)
                {

                    h.UseCluster((configureCluster) => {
                        for (int i = 0; i < phbkEmployeeViewExtForLkUpConf.ClusterIpAddresses.Length; i++)
                        {
                            configureCluster.Node(phbkEmployeeViewExtForLkUpConf.ClusterIpAddresses[i]);
                        }
                    });
                }
            }
            // h.PublisherConfirmation = true;
            // h.ConfigureBatchPublish(configure => { });
        });
        configurator.ConfigureEndpoints(context);
        // 
        // Quorum Queue settings
        //
        // configurator.SetQuorumQueue(3);
        //
    });
});

#endregion

Set up PhBkWebApp

appsettings json for PhBkWebApp

Click to show the code
  "PhbkDivisionViewExtForLkUpConf": {
    "HostName": "192.168.100.3",
    "Username": "admin",
    "Password": "admin",
    "VirtualHostName": "phbkhost",
    "ClusterIpAddresses": []
  },
  "PhbkEmployeeViewExtForLkUpConf": {
    "HostName": "192.168.100.3",
    "Username": "admin",
    "Password": "admin",
    "VirtualHostName": "phbkhost",
    "ClusterIpAddresses": []
  }

Program file for PhBkWebApp

  • read the article first. (It gives an example configuration)
    • Add the IBusLpPhbkEmployee.cs-file in the MassTansitBuses-folder of the PhBkControllers.csproj-project with a code
Click to show the code
using MassTransit;
namespace PhBkControllers.MassTansitBuses
{
    public interface IBusLpPhbkEmployee: IBus
    {
    }
}
  • Back to the Program.cs file
    • we get the PhbkEmployeeViewExtForLkUpConf-section from appsettings.json-file
    • using PhbkEmployeeViewExtForLkUpConf-section we configure second instance of the MassTransit Service Bus
      • builder.Services.AddMassTransit<IBusLpPhbkEmployee>-code declares the second copy of the Service Bus.
      • x.AddRequestClient<IPhbkEmployeeViewExtForLkUpMsg>();-code registers second producer for the second copy of the Service Bus.
      • configurator.ConfigureEndpoints(context);- method call is NOT used.
Click to show the code
#region MassTransit config
...
var phbkEmployeeViewExtForLkUpConf = new PhbkEmployeeViewExtForLkUpConf();
configuration.GetSection(PhbkEmployeeViewExtForLkUpConf.ConfName).Bind(phbkEmployeeViewExtForLkUpConf);
builder.Services.AddMassTransit<IBusLpPhbkEmployee>(x => {

    x.AddRequestClient<IPhbkEmployeeViewExtForLkUpMsg>();

    x.UsingRabbitMq((context, configurator) => {
        configurator.Host(phbkEmployeeViewExtForLkUpConf.HostName, phbkEmployeeViewExtForLkUpConf.VirtualHostName, h => {
            h.Username(phbkEmployeeViewExtForLkUpConf.Username);
            h.Password(phbkEmployeeViewExtForLkUpConf.Password);
            if (phbkEmployeeViewExtForLkUpConf.ClusterIpAddresses != null)
            {
                if (phbkEmployeeViewExtForLkUpConf.ClusterIpAddresses.Length > 0)
                {

                    h.UseCluster((configureCluster) => {
                        for (int i = 0; i < phbkEmployeeViewExtForLkUpConf.ClusterIpAddresses.Length; i++)
                        {
                            configureCluster.Node(phbkEmployeeViewExtForLkUpConf.ClusterIpAddresses[i]);
                        }
                    });
                }
            }
            // h.PublisherConfirmation = true;
            // h.ConfigureBatchPublish(configure => { });
        });
        // 
        // Quorum Queue settings
        //
        // configurator.SetQuorumQueue(3);
        //
    });
});

#endregion
⚠️ **GitHub.com Fallback** ⚠️