0

I'm currently facing a problem while trying to test my Create View, here's the error I'm getting Unable to resolve service for type 'Services.IAvocatService' while attempting to activate 'ExamenWeb.Controllers.CreationDossier'.

Here's my startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<IClientService, ClientService>()
    .AddScoped<IServiceDossier, ServiceDossier>()
    .AddScoped<ISpecialiteService, SpecialiteService>()
    .AddTransient<IUnitOfWork, UnitOfWork>()
    .AddScoped<IDataBaseFactory, DataBaseFactory>();
    services.AddControllersWithViews();   
}

Here's my controller along with constructor and the two create methods. I'm not understanding the problem because everything was injected correctly in the constructor

public class CreationDossier : Controller
{
    private IServiceDossier doss;
    private IClientService cl;
    private IAvocatService av;
    public CreationDossier(IServiceDossier doss, IClientService cl, IAvocatService av)
    {
        this.doss = doss;
        this.cl = cl;
        this.av = av;

    }
public ActionResult Create()
{
    ViewBag.AvocatFK = new SelectList(av.GetMany(), "AvocatId", "Avocat");
    ViewBag.ClientFK = new SelectList(cl.GetMany(), "CIN", "Client");
    return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Dossier collection)
{
    try
    {
        doss.Add(collection);
        doss.Commit();
        return RedirectToAction(nameof(Index));
    }
    catch
    {
        return View();
    }
}

Here's my cs.html file

@model Domain.Dossier
@using Domain
@{
    ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>Dossier</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="DateDepot" class="control-label"></label>
                <input asp-for="DateDepot" class="form-control" />
                <span asp-validation-for="DateDepot" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Description" class="control-label"></label>
                <input asp-for="Description" class="form-control" />
                <span asp-validation-for="Description" class="text-danger"></span>
            </div>
            <div class="form-group form-check">
                <label class="form-check-label">
                    <input class="form-check-input" asp-for="Clos" /> @Html.DisplayNameFor(model => model.Clos)
                </label>
            </div>
            <div class="form-group">
                <label asp-for="Frais" class="control-label"></label>
                <input asp-for="Frais" class="form-control" />
                <span asp-validation-for="Frais" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="AvocatFK" class="control-label"></label>
                <select asp-for="AvocatFK" class="form-control" asp-items="ViewBag.AvocatFK"></select>
            </div>
            <div class="form-group">
                <label asp-for="ClientFK" class="control-label"></label>
                <select asp-for="ClientFK" class="form-control" asp-items="ViewBag.ClientFK"></select>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Backs
  • 24,430
  • 5
  • 58
  • 85
Compte Gmail
  • 137
  • 3
  • 8
  • 2
    Look like you miss out inject/register `IAvocatService` in `ConfigureServices` method. – Yong Shun Dec 31 '21 at 11:10
  • 2
    `IAvocatService` is not configured in your `ConfigureServices()` method but is injected in `CreationDossier()` constructor. Just configure your app properly and it would work. – Ivan Khorin Dec 31 '21 at 11:11
  • Prevent posting [images of exceptions](https://idownvotedbecau.se/imageofanexception/). Instead post full stack traces in textual form. – Steven Dec 31 '21 at 11:29

1 Answers1

3

You haven't registered IAvoatService in Startup.cs you need something like

public void ConfigureServices(IServiceCollection services)
{
    // register other services as in original implementation
    services.AddTransient<IAvocatService, ??AvocatServiceImplementation>()
}

Where AvocatServiceImplementation is the concrete class that implements IAvocatService.

satnhak
  • 9,407
  • 5
  • 63
  • 81
  • my pleasure, glad to help (note that you need to register it with the correct lifetime https://stackoverflow.com/questions/38138100/addtransient-addscoped-and-addsingleton-services-differences) – satnhak Dec 31 '21 at 11:20