C
C#14mo ago
julian

✅ Contracts.dll for Domain not being activated

Hi, I'm trying out to build a Domain project where I have a Contracts project, that will have the interfaces (and logics) used in the Webclient. I'm getting this error: System.InvalidOperationException: Unable to resolve service for type 'Workcruit.Domain.Contracts.Interfaces.ITestInterface' while attempting to activate 'BackendForFrontend.Controllers.AuthController'. at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired) at lambda_method8(Closure, IServiceProvider, Object[]) at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>cDisplayClass6_0.<CreateControllerFactory>gCreateController|0(ControllerContext controllerContext) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>gAwaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>gLogged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>gLogged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>gAwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context) My code in the Domain:
ITest.cs
namespace Workcruit.Domain.Contracts.Interfaces
{
public interface ITestInterface
{
Task<string> Test();
}
}

Test.cs
public class Test : Contracts.Interfaces.ITestInterface
{
Task<string> ITestInterface.Test()
{
return Task.FromResult("Test");
}
}

Program.cs
public class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Workcruit.Domain");
try
{
Console.WriteLine("Starting Workcruit.Domain...");
await Host.CreateDefaultBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<HostService>();
services.AddScoped<Contracts.Interfaces.ITestInterface, Workcruit.Domain.Services.Test>();
})
.RunConsoleAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
ITest.cs
namespace Workcruit.Domain.Contracts.Interfaces
{
public interface ITestInterface
{
Task<string> Test();
}
}

Test.cs
public class Test : Contracts.Interfaces.ITestInterface
{
Task<string> ITestInterface.Test()
{
return Task.FromResult("Test");
}
}

Program.cs
public class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Workcruit.Domain");
try
{
Console.WriteLine("Starting Workcruit.Domain...");
await Host.CreateDefaultBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<HostService>();
services.AddScoped<Contracts.Interfaces.ITestInterface, Workcruit.Domain.Services.Test>();
})
.RunConsoleAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
Code in the WebClient:
public AuthController(Workcruit.Domain.Contracts.Interfaces.ITestInterface testInterface)
{
_testInterface = testInterface;
}

public async Task<ActionResult> Test()
{
string test = await _testInterface.Test();
return Ok(test);
}
public AuthController(Workcruit.Domain.Contracts.Interfaces.ITestInterface testInterface)
{
_testInterface = testInterface;
}

public async Task<ActionResult> Test()
{
string test = await _testInterface.Test();
return Ok(test);
}
Getting the error written at the top of this thread.
24 Replies
JakenVeina
JakenVeina14mo ago
System.InvalidOperationException: Unable to resolve service for type 'Workcruit.Domain.Contracts.Interfaces.ITestInterface' while attempting to activate 'BackendForFrontend.Controllers.AuthController
You have not registered ITestInterface with your IoC container
julian
julianOP14mo ago
Hmm, how do I register it in the IoC container? I'm pretty new to Domain 😛 I know how to work with Domains, but not creating them
JakenVeina
JakenVeina14mo ago
no idea what Domain is but somewhere you're using Microsoft.Extensions.DependencyInjection
julian
julianOP14mo ago
But I'm registering the ITestService in my Program.cs This is how it looks in the WebClient:
private readonly Workcruit.Domain.Contracts.Interfaces.ITestInterface _testInterface;

public AuthController(ICandidateUserRepo candidateUserRepo, Workcruit.Domain.Contracts.Interfaces.ITestInterface testInterface)
{
_candidateUserRepo = candidateUserRepo;
_testInterface = testInterface;
}

public async Task<ActionResult> Test()
{
string test = await _testInterface.Test();
return Ok(test);
}
private readonly Workcruit.Domain.Contracts.Interfaces.ITestInterface _testInterface;

public AuthController(ICandidateUserRepo candidateUserRepo, Workcruit.Domain.Contracts.Interfaces.ITestInterface testInterface)
{
_candidateUserRepo = candidateUserRepo;
_testInterface = testInterface;
}

public async Task<ActionResult> Test()
{
string test = await _testInterface.Test();
return Ok(test);
}
This is the Program.cs in the other project:
public class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Workcruit.Domain");
try
{
Console.WriteLine("Starting Workcruit.Domain...");
await CreateHostBuilder(args).Build().RunAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
// Register contracts and implementations
services.AddTransient<ITestInterface, Test>();
// Add other services as needed
});
}
public class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Workcruit.Domain");
try
{
Console.WriteLine("Starting Workcruit.Domain...");
await CreateHostBuilder(args).Build().RunAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
// Register contracts and implementations
services.AddTransient<ITestInterface, Test>();
// Add other services as needed
});
}
julian
julianOP14mo ago
No description
julian
julianOP14mo ago
Trying to expose the Contracts project, and I'm able to see the interfaces in my other project, but they aren't getting activated.
JakenVeina
JakenVeina14mo ago
okay, so .Domain is the project you're running that is throwing?
julian
julianOP14mo ago
It's throwing inside the .WebClient project, that's a seperate project, even though it's activated in the .Domain project 😛
JakenVeina
JakenVeina14mo ago
uhm well, I don't see any .WebClient project, for one
julian
julianOP14mo ago
It's not in the same solution, it's two completely different projects
JakenVeina
JakenVeina14mo ago
then why are we looking at code in this solution? specifically, why are we not looking at the bootstrapping code for the project where the issue is actually happening? why are we looking at code that doesn't RUN in the project where the issue is actually happening?
julian
julianOP14mo ago
private readonly Workcruit.Domain.Contracts.Interfaces.ITestInterface _testInterface;

public AuthController(ICandidateUserRepo candidateUserRepo, Workcruit.Domain.Contracts.Interfaces.ITestInterface testInterface)
{
_candidateUserRepo = candidateUserRepo;
_testInterface = testInterface;
}

public async Task<ActionResult> Test()
{
string test = await _testInterface.Test();
return Ok(test);
}
private readonly Workcruit.Domain.Contracts.Interfaces.ITestInterface _testInterface;

public AuthController(ICandidateUserRepo candidateUserRepo, Workcruit.Domain.Contracts.Interfaces.ITestInterface testInterface)
{
_candidateUserRepo = candidateUserRepo;
_testInterface = testInterface;
}

public async Task<ActionResult> Test()
{
string test = await _testInterface.Test();
return Ok(test);
}
This is how it's setup inside the project where it's crashing
JakenVeina
JakenVeina14mo ago
does the .WebClient project load .Domain and call Program.CreateHostBuilder? that's not a setup for anything and you already posted that that snippet does not contain an IoC registration for ITestInterface
julian
julianOP14mo ago
But I don't have access to the implementation, only the Interface, so how would I activate it in the .WebClient Projhect?
JakenVeina
JakenVeina14mo ago
huh? we're talking about registering it the IoC container activates it how would you not "have access" to it? where is your IoC setup code in .WebClient?
julian
julianOP14mo ago
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(o =>
{
o.Cookie.SecurePolicy = CookieSecurePolicy.Always;
o.Cookie.SameSite = SameSiteMode.None;
o.Cookie.HttpOnly = true;
})
.AddOpenIdConnect("Auth0", options => ConfigureOpenIdConnect(options));

services.AddControllersWithViews();

services.AddHttpClient();

// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/build";
});

services.AddSingleton<DapperContext>();
services.AddSingleton<TokenHelper>();
services.AddSingleton<PasswordHelper>();
services.AddScoped<IPositionRepo, PositionRepo>();
services.AddScoped<ICandidateUserRepo, CandidateUserRepo>();
services.AddScoped<IApplicationRepo, ApplicationRepo>();
services.AddScoped<ICompanyRepo, CompanyRepo>();
services.AddScoped<IApplicationStatusRepo, ApplicationStatusRepo>();

}
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(o =>
{
o.Cookie.SecurePolicy = CookieSecurePolicy.Always;
o.Cookie.SameSite = SameSiteMode.None;
o.Cookie.HttpOnly = true;
})
.AddOpenIdConnect("Auth0", options => ConfigureOpenIdConnect(options));

services.AddControllersWithViews();

services.AddHttpClient();

// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/build";
});

services.AddSingleton<DapperContext>();
services.AddSingleton<TokenHelper>();
services.AddSingleton<PasswordHelper>();
services.AddScoped<IPositionRepo, PositionRepo>();
services.AddScoped<ICandidateUserRepo, CandidateUserRepo>();
services.AddScoped<IApplicationRepo, ApplicationRepo>();
services.AddScoped<ICompanyRepo, CompanyRepo>();
services.AddScoped<IApplicationStatusRepo, ApplicationStatusRepo>();

}
JakenVeina
JakenVeina14mo ago
there is no registration for ITestInterface
julian
julianOP14mo ago
Tried doing that. I'm getting this error: System.ArgumentException: 'Cannot instantiate implementation type 'Workcruit.Domain.Contracts.Interfaces.ITestInterface' for service type 'Workcruit.Domain.Contracts.Interfaces.ITestInterface'.'
JakenVeina
JakenVeina14mo ago
right, you have register a concrete type what implementation of ITestInterface do you intend to use in .WebClient if you can't do
var test = new ITestInterface();
var test = new ITestInterface();
how do you expect the IoC container to?
julian
julianOP14mo ago
I'm just trying to setup the .Domain project. It's mostly to seperate logics. So Core/Business logics etc. will be in the .Domain, and the WebApp/WebClient will be in .WebClient. But It's supposed to be SQL queries etc. in the .Domain
JakenVeina
JakenVeina14mo ago
that sounds wonderful and irrelevant to me
what implementation of ITestInterface do you intend to use in .WebClient
julian
julianOP14mo ago
The ITestInterface will be removed once I get this working. Currently the ITestInterface is just to get the two projects working together. ITestInterface has a Test() method, that just returns a string. So, there is no purpose of ITestInterface other than getting the two projects to actually work together, so I actually can implement the logics I want..
JakenVeina
JakenVeina14mo ago
once I get this working
get what working? What is not working other than your attempts to use ITestInterface? How is ITestInterface supposed to "get the two projects working together"? What does "get the two projects working together" even mean?
Accord
Accord14mo 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