I want to call a AppService from a static Class. This Static Class is defined in a Web.Host project.
AppService:
public interface ISignalRExtensionAppService : IApplicationService { Task<bool> Create(CreateMachineDto input); }
public class SignalRExtensionAppService : ISignalRExtensionAppService
{ public ILogger log { get; set; } private IDbContextProvider<GenTimeDbContext> _sampleDbContextProvider;
public SignalRExtensionAppService(IDbContextProvider<myDbContext> sampleDbContextProvider) { _sampleDbContextProvider = sampleDbContextProvider; log = NullLogger.Instance; } public async Task<bool> Create(CreateMachineDto input) { try { log.Debug(string.Format("Machine: {0} with TenantId is {1} send: {2} - {3}", input.Name, input.TenantId, input.LastLogin, input.LastLogin)); using (var context = _sampleDbContextProvider.GetDbContext()) { var machine = context.Machines.Where(x => x.Name == input.Name && x.TenantId == input.TenantId).FirstOrDefault(); machine.LastLogin = input.LastLogin; machine.LastLogout = input.LastLogout; context.SaveChanges(); } return true; } catch (Exception ex) { log.Error(ex.Message, ex); return false; } } public async Task<string> GetMachineName(int input) { string result = string.Empty; try { log.Debug(string.Format("MachineId: {0}.", input)); using (var context = _sampleDbContextProvider.GetDbContext()) { var machine = context.Machines.Where(x => x.Id == input).FirstOrDefault(); result = machine.Name; } } catch (Exception ex) { log.Error(ex.Message, ex); } return result; }
- Static Class:
private static SignalRExtensionAppService _signalRExtensionAppService;
public static void AddSignalR(this IServiceCollection services, IConfiguration configuration)
{
var url = configuration["SignalR:Url"];
_signalRExtensionAppService = signalRExtensionAppService(); **<-- Error How to pass IDbContextProvider<myDbContext> sampleDbContextProvider**
*************************************
- Startup
// Call to staic class services.AddSignalR(_appConfiguration);
how to pass IDbContextProvider<myDbContext> sampleDbContextProvider or IRepository<Application> _applicationRepository when make a new?
Thanks for all
Regards
17 Answer(s)
-
0
You need dependency injection, but it is not recommended to use global dependency injection containers in static methods.
Why do you want to use application services in static methods? Maybe you can do it in another way.
-
0
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
-
0
Where is your static method called?
If the application is called once after it is started, you can call it in the module and use dependency injection.
Please share more code.
-
0
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
-
0
You can move the contents of the
AddSignalR
method to thePostInitialize
method of theWebHostModule
class.And use
IocManager
to get the relevant services you need.https://github.com/aspnetzero/aspnet-zero-core/blob/dev/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Web.Host/Startup/AbpZeroTemplateWebHostModule.cs#L58
-
0
Ok thanks... I try it
-
0
I've tried to do it, but I don't know what I'm doing wrong.
WebHostModule
IocManager.Resolve<AppService.ISignalRExtensionAppService>();
ISignalRExtensionAppService
public interface ISignalRExtensionAppService : IApplicationService { Task Create(CreateMachineDto input); }
SignalRExtensionAppService
public class SignalRExtensionAppService : ISignalRExtensionAppService { #region "Members" public ILogger log { get; set; } private readonly IDbContextProvider<GenTimeDbContext> _sampleDbContextProvider; private readonly IRepository<Machine> _machineRepository; private readonly HubConnection _connection; #endregion #region "Constructor" public SignalRExtensionAppService( IRepository<Machine> machineRepository, IDbContextProvider<GenTimeDbContext> sampleDbContextProvider, HubConnection connection ) { _machineRepository = machineRepository; _connection = connection; _sampleDbContextProvider = sampleDbContextProvider; log = NullLogger.Instance; InvokedOn(); } #endregion #region "Private Method" #region "SignalR" private async Task InvokedOn() { _connection.On<string, string, string, int>("GetMessage", (from, to, message, type) => { Task.Run(async () => { var newMessage = $"{"SIG"}: {message}"; log.Info(newMessage); Recieve_SendMessageToUser(from, to, message, type); }); }); } private async Task 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 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 }; await Create(createMachine); } catch (Exception ex) { log.Error(ex.Message, ex); } } #endregion #endregion #region "Interface Implementation Method" public async Task Create(CreateMachineDto input) { try { log.Debug(string.Format("Machine: {0} with TenantId is {1} send: {2} - {3}", input.Name, input.TenantId, input.LastLogin, input.LastLogin)); var machine = _machineRepository.GetAll().Where(x => x.Name == input.Name && x.TenantId == input.TenantId).FirstOrDefault(); machine.LastLogin = input.LastLogin; machine.LastLogout = input.LastLogout; await _machineRepository.InsertOrUpdateAsync(machine); } catch (Exception ex) { log.Error(ex.Message, ex); } } #endregion
}
SignalR events arrive without problem. But when the "Create" is done, I have the following error:
Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'GenTimeDbContext'.
Thanks for all
Regards
-
0
Try
[UnitOfWork] public virutal async Task Create(CreateMachineDto input) { }
-
0
-
0
but the IRepository is empty.
Do you mean that the database has data but
_machineRepository
does not find any data? -
0
-
0
Does the TenantId and UserId of the AbpSession in the
Create(CreateMachineDto input)
method have a value?Can you share the code for the
Machine
entity? -
0
Dto
[AutoMapTo(typeof(Machine))] public class CreateMachineDto { public int? Id { get; set; } [Required] [StringLength(AbpTenantBase.MaxNameLength)] public string Name { get; set; } public string Description { get; set; } public bool? Active { get; set; } public int? TenantId { get; set; } public DateTime InstallDate { get; set; } public DateTime LastLogin { get; set; } public DateTime? LastLogout { get; set; } public bool? IsBlocked { get; set; } public string ReasonIsBlocked { get; set; } // OperatingSystem public string OperationSystem { get; set; } public string OSVersion { get; set; } public string OSArchitecture { get; set; } public string OSSerialNumber { get; set; } public string OSLanguage { get; set; } public string OSTotalVisibleMemorySize { get; set; } public string OSSystemDrive { get; set; } //ComputerSystem public string CSNumberOfLogicalProcessors { get; set; } public string CSManufacturer { get; set; } public string CSModel { get; set; } //LogicalDisk public string LDDiskSize { get; set; } public string LDFreeSpaceDisk { get; set; } public DateTime? DateWasBlocked { get; set; } public int? LicenseId { get; set; } public CreateApplicationDto Application { get; set; } public ICollection<TagDto> Tags { get; set; } }
Model:
public partial class Machine : FullAuditedEntity, IMayHaveTenant { public Machine() { Events = new HashSet<Event>(); MachineAppVersions = new HashSet<MachineAppVersion>(); TagMachines = new HashSet<TagMachine>(); } public string Name { get; set; } public string Description { get; set; } public bool? Active { get; set; } public DateTime? InstallDate { get; set; } public DateTime? LastLogin { get; set; } public DateTime? LastLogout { get; set; } public bool? IsBlocked { get; set; } public string ReasonIsBlocked { get; set; } public DateTime? DateWasBlocked { get; set; } // OperatingSystem public string OperationSystem { get; set; } public string OSVersion { get; set; } public string OSArchitecture { get; set; } public string OSSerialNumber { get; set; } public string OSLanguage { get; set; } public string OSTotalVisibleMemorySize { get; set; } public string OSSystemDrive { get; set; } //ComputerSystem public string CSNumberOfLogicalProcessors { get; set; } public string CSManufacturer { get; set; } public string CSModel { get; set; } //LogicalDisk public string LDDiskSize { get; set; } public string LDFreeSpaceDisk { get; set; } public int? TenantId { get; set; } public int? LicenseId { get; set; } public License License { get; set; } public ICollection<Event> Events { get; set; } public ICollection<MachineAppVersion> MachineAppVersions { get; set; } public ICollection<TagMachine> TagMachines { get; set; } }
-
0
Does the TenantId and UserId of the AbpSession in the Create(CreateMachineDto input) method have a value?
You can inject the IAbpSession interface to see it.
-
0
The TenantId and UserId of the AbpSession is null.
How do I inject the AbpSession?
I do the following:
private readonly IRepository<Machine> _machineRepository; private readonly IAbpSession _session; private readonly HubConnection _connection; #endregion #region "Constructor" public SignalRExtensionAppService( IRepository<Machine> machineRepository, IAbpSession session, HubConnection connection ) { _machineRepository = machineRepository; _connection = connection; _session = session; //_sampleDbContextProvider = sampleDbContextProvider; log = NullLogger.Instance; InvokedOn(); }
-
0
Try
using (_unitOfWorkManager.Current.DisableFilter(AbpDataFilters.MayHaveTenant)) { var machine = _machineRepository.GetAll().Where(x => x.Name == input.Name && x.TenantId == input.TenantId).FirstOrDefault(); }
-
0
Great It's working fine!!
Very, very thanks.
Regards