Search code examples
asp.net-core.net-coredynamics-crmdataverse

How can I implement IOrganizationService in ASP.NET Core?


I have an ASP.NET Core 7 project that I use to connect to Dynamics 365 / Dataverse.

I do not use any services or interfaces as of now. In each controller I am creating a new instance of a ServiceClient using Microsoft.PowerPlatform.Dataverse.Client.

For example:

public class ContactsController : Controller
{
    private readonly IConfiguration _configuration;

    public ContactsController(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    [Authorize]
    public IActionResult Index()
    {
        string? connectionString = _configuration.GetConnectionString("Dataverse");

        using (ServiceClient svc = new ServiceClient(connectionString))
        {
            if (svc.IsReady)
            {
                //Get data from Dataverse using svc
                return View();
            }

            return View();
        }
    }
}

However, I know this approach is not good. I am not re-using the ServiceClient and creating new ones for every call in each controller. I know that I should register the ServiceClient and IOrganizationService in Program.cs like this for example:

builder.Services.AddScoped<IOrganizationService, ServiceClient>();

But I don't know how to write the interface and service class for this and how to add the connection string.


Solution

  • You could try using below code that the will create the ServiceClient and abstracts away the direct usage of ServiceClient in your controllers.

    IOrganizationServiceWrapper.cs:

    public interface IOrganizationServiceWrapper
    {
        bool IsReady { get; }
        .......
        .......
    }
    

    OrganizationServiceWrapper.cs:

    public class OrganizationServiceWrapper : IOrganizationServiceWrapper
    {
        private readonly ServiceClient _serviceClient;
    
        public OrganizationServiceWrapper(string connectionString)
        {
            _serviceClient = new ServiceClient(connectionString);
        }
    
        public bool IsReady => _serviceClient.IsReady;
        
        ...
        ...
    }
    

    Program.cs:

    builder.Services.AddScoped<IOrganizationServiceWrapper>(sp => 
    {
        var configuration = sp.GetRequiredService<IConfiguration>();
        var connectionString = configuration.GetConnectionString("Dataverse");
        return new OrganizationServiceWrapper(connectionString);
    });
    

    ContactsController.cs:

    public class ContactsController : Controller
    {
        private readonly IOrganizationServiceWrapper _orgService;
    
        public ContactsController(IOrganizationServiceWrapper orgService)
        {
            _orgService = orgService;
        }
    
        [Authorize]
        public IActionResult Index()
        {
            if (_orgService.IsReady)
            {
                return View();
            }
            return View();
        }
    }