C
C#2y ago
PatriQ

❔ SignalR .NET 7 API Dependency injection not working

Hello. As I have described here https://github.com/dotnet/aspnetcore/issues/46095#issuecomment-1587264717 with a trivial example, I cannot manage to make Dependency injection work with SignalR. Been trying so for days. Any help would be appreciated.
4 Replies
JakenVeina
JakenVeina2y ago
I don't see how the instantiation order affects anything you probably have a lifetime mismatch issue here in the thread you're responding to, the issue is that the developer is putting logic inside of constructors, and thus has issues when the constructors don't run in a particular order you're not doing this if _userInfo in your repository doesn't have the right info, it's cause it never got populated either the code that populates it isn't running, or its running on a different instance
PatriQ
PatriQOP2y ago
Hey thanks for answer. Yeah I guess I've given a wrong example. Thats exactly what I'm doing, extracting the tenant data in the constructor.
public class ScreeningRepository : IScreeningRepository
{
private readonly string tenantId;
public ScreeningRepository(IUserInfo userInfo)
{
//UserInfo here is empty
tenantId = userInfo.GetTenant();
}

public async Task<bool> DoSomething()
{
//Can't do anything because tenantId here is empty
return false;
}
}
public class ScreeningRepository : IScreeningRepository
{
private readonly string tenantId;
public ScreeningRepository(IUserInfo userInfo)
{
//UserInfo here is empty
tenantId = userInfo.GetTenant();
}

public async Task<bool> DoSomething()
{
//Can't do anything because tenantId here is empty
return false;
}
}
Here's the fixed code
JakenVeina
JakenVeina2y ago
as in, you fixed the fact that you weren't showing your actual code? cause that's not a fix that's specifically the thing you shouldn't do
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server