Base solution for your next web application
Open Closed

Resolving Dependency injection in maui #12153


User avatar
0
WirelessDynamics created

Dear All

we need to build a customer service in Maui as follow:

public class DownloadDataService : IDownloadDataService,ITransientDependency { private LoockUpDataDto loockUpDataDto;

protected IDownLoadDataAppService downLoadDataAppService { get; set; }
public DownloadDataService()
{
        downLoadDataAppService=DependencyResolver.Resolve<IDownLoadDataAppService>();
}





public async Task ExecuteAsync()

{
    await WebRequestExecuter.Execute(
         async () => await downLoadDataAppService.DownloadLoockup(3),
             (result) =>
             {
                 loockUpDataDto = result;

                 return Task.CompletedTask;
             });





}

}

public interface IDownloadDataService { Task ExecuteAsync(); }

I register using ITransientDependency

also I resolve as follow in the constructior of My page as

downloadDataService = DependencyResolver.Resolve<IDownloadDataService>();

await SetBusyAsync(async () => { await downloadDataService.ExecuteAsync(); });

but once to navigate to this page the app hangs,

what I am doing wrong?


5 Answer(s)
  • User Avatar
    0
    m.aliozkaya created
    Support Team

    Hi @WirelessDynamics,

    Please check the name of your interface and class.

    Naming conventions are very important here. For example, you can change the name of PersonAppService to MyPersonAppService or another name which contains the 'PersonAppService' postfix. This registers it to IPersonAppService because it has the same postfix. You can not, however, name your service without the postfix, such as 'PeopleService'. If you do so, it's not registered to the IPersonAppService automatically. Instead, it's registered to the DI framework using self-registration (not the interface). In this case, you can manually register it.

  • User Avatar
    0
    WirelessDynamics created

    hello

    1-This is true if I am using the Naming convention but I am not, we are marking the service with ITransientDependency which should do the registering as far as I understand

    2- In addition, in the Maui project in the Service folder, asp.net zero is doing the same for IAccountService and AccountService with the only difference that it is marked with ISinglton.

    3-In all way what is the right way to do DI in Asp.net Zero if we don't want to use Naming Convention

    Thanks. Mina.

  • User Avatar
    0
    m.aliozkaya created
    Support Team

    Hi,

    If you don't want to use naming conventions. Please take a look https://aspnetboilerplate.com/Pages/Documents/Dependency-Injection#custom-direct-registration

    Also if you are using for ProxyServices please check ProxyAppServiceBase because URL's are created there. Customize for your requirements.

  • User Avatar
    0
    WirelessDynamics created

    that is exactly what I am referring to, the sent URL states how to make DI without Naming Convenstion using the ITrienstDependecnt and ISigneltonDependency.

    and this is exactly what I am using in my code but it is not working

  • User Avatar
    0
    m.aliozkaya created
    Support Team

    Hi,

    Could you share the error? When I tried it myself, everything was okay.

    For example, if you have IPersonAppService, the naming convention should be ProxyPersonAppService. But if you want to change this name to ProxyPeopleAppService

    You can go to the Maui module class. Put this code into the PreInitialize method IocManager.Register<IPersonAppService, ProxyPeopleAppService>(DependencyLifeStyle.Transient);