Base solution for your next web application

Activities of "SASIMEXICO"

How can I call AppService from the beginning? How can I do it from the Web.Host?

  • Startup

      services.AddSignalR(_appConfiguration);
    
  • Static Class

     public static class SignalRExtension
      {
          private const string WEBAPI = "!GSSYSTEM_WAPI";
          private static SignalRExtensionAppService _signalRExtensionAppService;
          private static IRepository<Machine> _machineRepository;
    
          public static void AddSignalR(this IServiceCollection services, IConfiguration configuration)
          {
              var url = configuration["SignalR:Url"];
    
              //_signalRExtensionAppService = signalRExtensionAppService;
              //_machineRepository = machineRepository;
    
              HubConnection connection = null;
              connection = new HubConnectionBuilder()
                  .WithUrl(url)
                  .Build();
    
              connection.On<string, string, string, int>("GetMessage", (from, to, message, type) =>
              {
                  Task.Run(async () =>
                  {
                      var newMessage = $"{"SIG"}: {message}";
                      //log.Info(newMessage);
                      //Console.WriteLine(newMessage);
                      Recieve_SendMessageToUser(from, to, message, type);
                  });
              });
    
              connection.Closed += async (error) =>
              {
                  await Task.Delay(new Random().Next(0, 5) * 1000);
                  await connection.StartAsync();
                  connection.InvokeAsync<string>("JoinGroup", "monitor", WEBAPI, "1");
              };
    
              connection.StartAsync();
              connection.InvokeAsync<string>("JoinGroup", "monitor", WEBAPI, "1");
              services.AddSingleton(connection);
          }
    
          public static void Recieve_SendMessageToUser(string from, string to, string message, int type)
          {
              string action = message.Split('@')[0];
              switch (action)
              {
                  case "ActualizeRegisterDB":
                      ActualizeRegisterDB(message.Split('@')[1], message.Split('@')[2], message);
                      break;
                  default:
                      break;
              }
          }
    
          private static async Task ActualizeRegisterDB(string machineName, string tenant, string message)
          {
              int tenantId = 0;
              try
              {
                  var lastLogin = DateTime.ParseExact(message.Split('@')[3], "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
                  var lastLogout = DateTime.ParseExact(message.Split('@')[4], "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
    
                  int.TryParse(message.Split('@')[2], out tenantId);
    
                  CreateMachineDto createMachine = new CreateMachineDto()
                  {
                      Name = message.Split('@')[1],
                      LastLogin = lastLogin,
                      LastLogout = lastLogout,
                      TenantId = tenantId
                  };
    
                  //Call to SignalRExtensionAppService
    
              }
              catch (Exception ex)
              {
                  throw;
              }
          }
      }
    
  • AppService

     [AbpAuthorize(AppPermissions.Pages_Administration_Users)]
      public class MachineAppService : AsyncCrudAppService<Machine, MachineDto, int, ControllerInputDto, CreateMachineDto, UpdateMachineDto>, IMachineAppService
      {
          #region "Members"
    
          public ILogger log { get; set; }
    
          private readonly IRepository<Application> _applicationRepository;
          private readonly IRepository<Machine> _machineRepository;
          private readonly IRepository<AppVersion> _appVersionRepository;
          private readonly IRepository<Tag> _tagRepository;
          private readonly IRepository<License> _licenseRepository;
          private readonly IRepository<Tenant> _tenantRepository;
          private readonly IRepository<AppVendor> _appVendorRepository;
          private readonly IRepository<Active> _activeRepository;
          private readonly IRepository<SamAudit> _samAuditRepository;
          private readonly IRepository<ProductGroup> _productGroupRepository;
          private readonly IObjectMapper _objectMapper;
          private readonly HubConnection _connection;
          private readonly IFileProvider _fileProvider;
    
          #endregion
    
          #region "Constructor"
    
          public MachineAppService(
              IRepository<Machine> machineRepository,
              IRepository<Application> applicationRepository,
              IRepository<AppVersion> appVersionRepository,
              IRepository<Tag> tagRepository,
              IRepository<License> licenseRepository,
              IRepository<Tenant> tenantRepository,
              IRepository<AppVendor> appVendorRepository,
              IRepository<Active> activeRepository,
              IRepository<SamAudit> samAuditRepository,
              IRepository<ProductGroup> productGroupRepository,
              IObjectMapper objectMapper,
              HubConnection connection,
              IFileProvider fileProvider
              ) : base(machineRepository)
          {
              _applicationRepository = applicationRepository;
              _machineRepository = machineRepository;
              _appVersionRepository = appVersionRepository;
              _tagRepository = tagRepository;
              _licenseRepository = licenseRepository;
              _tenantRepository = tenantRepository;
              _appVendorRepository = appVendorRepository;
              _activeRepository = activeRepository;
              _samAuditRepository = samAuditRepository;
              _productGroupRepository = productGroupRepository;
              _objectMapper = objectMapper;
              _connection = connection;
              _fileProvider = fileProvider;
    
              log = NullLogger.Instance;
          }
    
          #endregion
    
          #region "Calls to SignalR"
          public async Task UpdateSigToMachine(List<UpdateMachineDto> input)
          {
              // signal => SIG_SHUTDOWN / SIG_STARTUP
              var tenantId = AbpSession.TenantId;
    
              try
              {
                  var machines = (from a in input
                                       select a);
    
                  foreach (var item in machines)
                  {
                      var signal = item.Active == true ? "SIG_STARTUP" : "SIG_SHUTDOWN";
                      await Update(item);
                      await _connection.InvokeAsync("SendSIGToMachine", item.Name, signal, tenantId.ToString());
                  }
    
                  return;
              }
              catch (Exception ex)
              {
                  log.Error(ex.Message, ex);
                  return;
              }
          }
    
          public async Task UpdateSigToAll(string signal)
          {
              var tenantId = AbpSession.TenantId;
              //int tenantId = 0;
              //int.TryParse(TenantId, out tenantId);
              // signal => SIG_SHUTDOWN / SIG_STARTUP
              bool recibeSignal = signal == "SIG_SHUTDOWN" ? false : true;
              await _connection.InvokeAsync("SendSIGToEverybody", tenantId, signal);
    
              try
              {
                  //var allMachines = _machineRepository.GetAll()
                  //        .Where(x => x.TenantId == tenantId);
    
                  var allMachines = _machineRepository.Query(a => a.Where(x => x.TenantId == tenantId));
    
                  foreach (var machine in allMachines)
                  {
                      if (recibeSignal)
                          machine.LastLogin = DateTime.Now;
                      else
                          machine.LastLogout = DateTime.Now;
    
                      machine.Active = recibeSignal;
                      _machineRepository.InsertOrUpdate(machine);
                  }
              }
              catch (Exception ex)
              {
                  log.Error(ex.Message, ex);
              }
          }
    
          //[AbpAllowAnonymousAttribute]
          [AbpAuthorize(AppPermissions.Pages_Administration_Users)]
          public async Task<List<string>> GetSignalRConnections()
          {
              return await _connection.InvokeAsync<List<string>>("GetConnectedUsers");
          }
    
          //[AbpAllowAnonymousAttribute]
          [AbpAuthorize(AppPermissions.Pages_Administration_Users)]
          public async Task<List<string>> GetSignalRMachineConnections()
          {
              var tenantId = AbpSession.TenantId;
              return await _connection.InvokeAsync<List<string>>("GetConnectedMachines", tenantId);
          }
    
          //[AbpAllowAnonymousAttribute]
          [AbpAuthorize(AppPermissions.Pages_Administration_Users)]
          public async Task<List<string>> GetAvailableApplicationsLogs(string machineName)
          {
              var tenantId = AbpSession.TenantId;
              return await _connection.InvokeAsync<List<string>>("GetAvailableApplicationsLogs", machineName, tenantId);
          }
    
          //[AbpAllowAnonymousAttribute]
          [AbpAuthorize(AppPermissions.Pages_Administration_Users)]
          public async Task<IActionResult> GetServiceLog(string machineName, string serviceName)
          {
              var tenantId = AbpSession.TenantId;
              var pathlog = await _connection.InvokeAsync<string>("GetServiceLog", machineName, tenantId, serviceName);
              FileStreamingAppService fileStreamingAppService = new FileStreamingAppService(_fileProvider);
              return await fileStreamingAppService.DownloadFile(pathlog.Split("@")[1]);
          }
    
          //[AbpAllowAnonymousAttribute]
          [AbpAuthorize(AppPermissions.Pages_Administration_Users)]
          public async Task UpdateApplicationsLogLevel(string machineName, string logLevel)
          {
              var tenantId = AbpSession.TenantId;
              await _connection.InvokeAsync<List<string>>("SendUpdateApplicationsLogLevel", machineName, logLevel, tenantId);
          }
    
          #endregion
    

Because i need to update machines date (last login and last logout) when the machine disconnected to SignalR. In this static class (add in sistem with singleton) I define the client to connected to SignalR and your events subscrived. This signalR client pass to MachineAppService for send message to machines.

Could you tell me what the injection of dependencies from the static class should be like or give me a new idea?

Thanks for all.

Regards

Yes! When methods are with "AppPermissions.Pages_Administration_Users" permission, the response is "current user did not login the application".

We are using Identity with IdentityModel (OpenId connection OAUTH), not Microsoft Identity... how the mobile app get the Token? And why using methods in web all is fine, but not in mobile?

The service require authorization, but I tested it with Authorization.Users and Anonymous decorators... Always NULL in AbpSession

Solved, thanks!

Showing 41 to 45 of 45 entries