Skip to main content

Notifications

Dynamics 365 Community / Blogs / Adrian Begovich's Blog / Dynamics 365: C# Console Ap...

Dynamics 365: C# Console Application For Testing Plugins

Adrian Begovich Profile Picture Adrian Begovich 21,009 Super User 2024 Season 2

This is a C# console application template for testing plugins.

using CRM.Plugins;
using CrmEarlyBound;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Net;

namespace CRM.Console
{
    class Program
    {
        static readonly string url = "";
        static readonly string clientId = "";
        static readonly string clientSecret = "";

        static readonly string connectionString = $@"
        AuthType = ClientSecret;
        Url = {url};
        ClientId = {clientId};
        ClientSecret = {clientSecret};
        RequireNewInstance = True";

        static void Main()
        {
            ITracingService tracer = new ConsoleTracing();

            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                using (ServiceClient service = new ServiceClient(connectionString))
                {
                    if (service.IsReady)
                    {
                        tracer.Trace("service.IsReady is {0}.", service.IsReady.ToString());
                        var response = (WhoAmIResponse)service.Execute(new WhoAmIRequest());
                        tracer.Trace("User ID is {0}.", response.UserId);

                        Test_Method(tracer, service);
                    }
                    else
                    {
                        tracer.Trace("A web service connection was not established.");
                    }
                }
            }
            catch (Exception e)
            {
                tracer.Trace(e.ToString());
            }

            tracer.Trace("Press any key to exit.");
            System.Console.ReadKey();
        }

        private static void Test_Method(ITracingService tracer, ServiceClient service)
        {
            tracer.Trace("Test_Method");
            Entity entity = service.Retrieve(Account.EntityLogicalName, new Guid("3d8b831e-8dd8-eb11-bacb-002248158935"), new ColumnSet(true));
            AccountService accountSvc = new AccountService(entity.ToEntity(), tracer, service);
            accountSvc.Method();
        }
    }
}

using Microsoft.Xrm.Sdk;

namespace CRM.Console
{
    internal class ConsoleTracing : ITracingService
    {
        public void Trace(string format, params object[] args)
        {
            System.Console.WriteLine(format, args);
        }
    }
}

Comments

*This post is locked for comments

  • Adrian Begovich Profile Picture Adrian Begovich 21,009 Super User 2024 Season 2
    Posted at
    FakeXrmEasy is a great option as well. https://dynamicsvalue.com/
  • Flydancer Profile Picture Flydancer 1,332
    Posted at
    For real testing scenarios you should have a look at FakeXrmEasy