C#C
C#3y 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>g
Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>gLogged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g
AwaitRequestTask|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());
        }
    }
}


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);
}


Getting the error written at the top of this thread.
Was this page helpful?