Why use IOrganizationService instead of CrmServiceClient in Dynamics 365 XRM tooling SDK?

Multi tool use
Multi tool use





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















Microsoft sample code for accessing Dynamics often looks like this:



    static void Main(string args)
{
try
{
string connectionString =
"Url=https://myorg.crm.dynamics.com; Username=me@myorg.com; Password=******; authtype=Office365";

using (CrmServiceClient conn = new CrmServiceClient(connectionString))
{
// Cast the proxy client to the IOrganizationService interface.
IOrganizationService orgService = (IOrganizationService)conn.OrganizationWebProxyClient ??
conn.OrganizationServiceProxy;

Console.WriteLine("Microsoft Dynamics CRM version {0}.", ((RetrieveVersionResponse)orgService.Execute(new RetrieveVersionRequest())).Version);
}
}
catch (FaultException<OrganizationServiceFault> osFaultException)
{
Console.WriteLine("Fault Exception caught");
Console.WriteLine(osFaultException.Detail.Message);
}
catch (Exception e)
{
Console.WriteLine("Uncaught Exception");
Console.WriteLine(e);
}
}
}


But it is equally possible (and simpler) to use the Crm Service Client directly, like this:



class Program
{
static void Main(string args)
{
try
{
string connectionString =
"Url=https://myorg.crm.dynamics.com; Username=me@myorg.com; Password=******; authtype=Office365";

using (CrmServiceClient conn = new CrmServiceClient(connectionString))
{
Console.WriteLine("Microsoft Dynamics CRM version {0}.", ((RetrieveVersionResponse)conn.Execute(new RetrieveVersionRequest())).Version);
}
}
catch (FaultException<OrganizationServiceFault> osFaultException)
{
Console.WriteLine("Fault Exception caught");
Console.WriteLine(osFaultException.Detail.Message);
}
catch (Exception e)
{
Console.WriteLine("Uncaught Exception");
Console.WriteLine(e);
}
}
}


My question: Why use that IOrganizationService property ever? It seems as if it has only a subset of the functionality of the CrmServiceClient. And CrmServiceClient used directly seems both faster, simpler, more efficient, and more feature-rich.



Any idea about why the sample code always has this additional layer of indirection?



Thanks.










share|improve this question































    3















    Microsoft sample code for accessing Dynamics often looks like this:



        static void Main(string args)
    {
    try
    {
    string connectionString =
    "Url=https://myorg.crm.dynamics.com; Username=me@myorg.com; Password=******; authtype=Office365";

    using (CrmServiceClient conn = new CrmServiceClient(connectionString))
    {
    // Cast the proxy client to the IOrganizationService interface.
    IOrganizationService orgService = (IOrganizationService)conn.OrganizationWebProxyClient ??
    conn.OrganizationServiceProxy;

    Console.WriteLine("Microsoft Dynamics CRM version {0}.", ((RetrieveVersionResponse)orgService.Execute(new RetrieveVersionRequest())).Version);
    }
    }
    catch (FaultException<OrganizationServiceFault> osFaultException)
    {
    Console.WriteLine("Fault Exception caught");
    Console.WriteLine(osFaultException.Detail.Message);
    }
    catch (Exception e)
    {
    Console.WriteLine("Uncaught Exception");
    Console.WriteLine(e);
    }
    }
    }


    But it is equally possible (and simpler) to use the Crm Service Client directly, like this:



    class Program
    {
    static void Main(string args)
    {
    try
    {
    string connectionString =
    "Url=https://myorg.crm.dynamics.com; Username=me@myorg.com; Password=******; authtype=Office365";

    using (CrmServiceClient conn = new CrmServiceClient(connectionString))
    {
    Console.WriteLine("Microsoft Dynamics CRM version {0}.", ((RetrieveVersionResponse)conn.Execute(new RetrieveVersionRequest())).Version);
    }
    }
    catch (FaultException<OrganizationServiceFault> osFaultException)
    {
    Console.WriteLine("Fault Exception caught");
    Console.WriteLine(osFaultException.Detail.Message);
    }
    catch (Exception e)
    {
    Console.WriteLine("Uncaught Exception");
    Console.WriteLine(e);
    }
    }
    }


    My question: Why use that IOrganizationService property ever? It seems as if it has only a subset of the functionality of the CrmServiceClient. And CrmServiceClient used directly seems both faster, simpler, more efficient, and more feature-rich.



    Any idea about why the sample code always has this additional layer of indirection?



    Thanks.










    share|improve this question



























      3












      3








      3


      1






      Microsoft sample code for accessing Dynamics often looks like this:



          static void Main(string args)
      {
      try
      {
      string connectionString =
      "Url=https://myorg.crm.dynamics.com; Username=me@myorg.com; Password=******; authtype=Office365";

      using (CrmServiceClient conn = new CrmServiceClient(connectionString))
      {
      // Cast the proxy client to the IOrganizationService interface.
      IOrganizationService orgService = (IOrganizationService)conn.OrganizationWebProxyClient ??
      conn.OrganizationServiceProxy;

      Console.WriteLine("Microsoft Dynamics CRM version {0}.", ((RetrieveVersionResponse)orgService.Execute(new RetrieveVersionRequest())).Version);
      }
      }
      catch (FaultException<OrganizationServiceFault> osFaultException)
      {
      Console.WriteLine("Fault Exception caught");
      Console.WriteLine(osFaultException.Detail.Message);
      }
      catch (Exception e)
      {
      Console.WriteLine("Uncaught Exception");
      Console.WriteLine(e);
      }
      }
      }


      But it is equally possible (and simpler) to use the Crm Service Client directly, like this:



      class Program
      {
      static void Main(string args)
      {
      try
      {
      string connectionString =
      "Url=https://myorg.crm.dynamics.com; Username=me@myorg.com; Password=******; authtype=Office365";

      using (CrmServiceClient conn = new CrmServiceClient(connectionString))
      {
      Console.WriteLine("Microsoft Dynamics CRM version {0}.", ((RetrieveVersionResponse)conn.Execute(new RetrieveVersionRequest())).Version);
      }
      }
      catch (FaultException<OrganizationServiceFault> osFaultException)
      {
      Console.WriteLine("Fault Exception caught");
      Console.WriteLine(osFaultException.Detail.Message);
      }
      catch (Exception e)
      {
      Console.WriteLine("Uncaught Exception");
      Console.WriteLine(e);
      }
      }
      }


      My question: Why use that IOrganizationService property ever? It seems as if it has only a subset of the functionality of the CrmServiceClient. And CrmServiceClient used directly seems both faster, simpler, more efficient, and more feature-rich.



      Any idea about why the sample code always has this additional layer of indirection?



      Thanks.










      share|improve this question
















      Microsoft sample code for accessing Dynamics often looks like this:



          static void Main(string args)
      {
      try
      {
      string connectionString =
      "Url=https://myorg.crm.dynamics.com; Username=me@myorg.com; Password=******; authtype=Office365";

      using (CrmServiceClient conn = new CrmServiceClient(connectionString))
      {
      // Cast the proxy client to the IOrganizationService interface.
      IOrganizationService orgService = (IOrganizationService)conn.OrganizationWebProxyClient ??
      conn.OrganizationServiceProxy;

      Console.WriteLine("Microsoft Dynamics CRM version {0}.", ((RetrieveVersionResponse)orgService.Execute(new RetrieveVersionRequest())).Version);
      }
      }
      catch (FaultException<OrganizationServiceFault> osFaultException)
      {
      Console.WriteLine("Fault Exception caught");
      Console.WriteLine(osFaultException.Detail.Message);
      }
      catch (Exception e)
      {
      Console.WriteLine("Uncaught Exception");
      Console.WriteLine(e);
      }
      }
      }


      But it is equally possible (and simpler) to use the Crm Service Client directly, like this:



      class Program
      {
      static void Main(string args)
      {
      try
      {
      string connectionString =
      "Url=https://myorg.crm.dynamics.com; Username=me@myorg.com; Password=******; authtype=Office365";

      using (CrmServiceClient conn = new CrmServiceClient(connectionString))
      {
      Console.WriteLine("Microsoft Dynamics CRM version {0}.", ((RetrieveVersionResponse)conn.Execute(new RetrieveVersionRequest())).Version);
      }
      }
      catch (FaultException<OrganizationServiceFault> osFaultException)
      {
      Console.WriteLine("Fault Exception caught");
      Console.WriteLine(osFaultException.Detail.Message);
      }
      catch (Exception e)
      {
      Console.WriteLine("Uncaught Exception");
      Console.WriteLine(e);
      }
      }
      }


      My question: Why use that IOrganizationService property ever? It seems as if it has only a subset of the functionality of the CrmServiceClient. And CrmServiceClient used directly seems both faster, simpler, more efficient, and more feature-rich.



      Any idea about why the sample code always has this additional layer of indirection?



      Thanks.







      c# .net dynamics-crm dynamics-crm-365






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 8 at 0:28









      Arun Vinoth

      10.2k132762




      10.2k132762










      asked Jan 3 at 21:26









      Stephan GStephan G

      5622723




      5622723
























          1 Answer
          1






          active

          oldest

          votes


















          3














          IOrganizationService is an interface that defines the most basic methods required to access all Dynamics functions. There are a number of general benefits to using interfaces.



          IOrganizationService has been around since CRM 2011, whilst CrmServiceClient was introduced around CRM 2016. A simple reason for using IOrganizationService is that has been around much longer and is present in existing code bases.



          CrmServiceClient implements IOrganizationService, and also provides a range of other methods, e.g. authenticating with CRM. Before CrmServiceClient was introduced we used CrmConnection to authenticate to CRM. When we had to migrate from CrmConnection to CrmServiceClient, we only had to change to CrmServiceClient, extract IOrganizationService and the rest of the code base remains the same.



          Programming to the IOrganizationService interface makes your code far more portable, and reusable. For example; when you don't know your service object is going to be created.



          IOrganizationService orgService = IOrganizationService)conn.OrganizationWebProxyClient ?? conn.OrganizationServiceProxy;


          For testing purposes when you want to mock IOrganizationService with a new MockOrganizationService class.



          When you want to move code between an external application and a plugin. In a plugin the CrmServiceClient is not provided.






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030019%2fwhy-use-iorganizationservice-instead-of-crmserviceclient-in-dynamics-365-xrm-too%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            IOrganizationService is an interface that defines the most basic methods required to access all Dynamics functions. There are a number of general benefits to using interfaces.



            IOrganizationService has been around since CRM 2011, whilst CrmServiceClient was introduced around CRM 2016. A simple reason for using IOrganizationService is that has been around much longer and is present in existing code bases.



            CrmServiceClient implements IOrganizationService, and also provides a range of other methods, e.g. authenticating with CRM. Before CrmServiceClient was introduced we used CrmConnection to authenticate to CRM. When we had to migrate from CrmConnection to CrmServiceClient, we only had to change to CrmServiceClient, extract IOrganizationService and the rest of the code base remains the same.



            Programming to the IOrganizationService interface makes your code far more portable, and reusable. For example; when you don't know your service object is going to be created.



            IOrganizationService orgService = IOrganizationService)conn.OrganizationWebProxyClient ?? conn.OrganizationServiceProxy;


            For testing purposes when you want to mock IOrganizationService with a new MockOrganizationService class.



            When you want to move code between an external application and a plugin. In a plugin the CrmServiceClient is not provided.






            share|improve this answer




























              3














              IOrganizationService is an interface that defines the most basic methods required to access all Dynamics functions. There are a number of general benefits to using interfaces.



              IOrganizationService has been around since CRM 2011, whilst CrmServiceClient was introduced around CRM 2016. A simple reason for using IOrganizationService is that has been around much longer and is present in existing code bases.



              CrmServiceClient implements IOrganizationService, and also provides a range of other methods, e.g. authenticating with CRM. Before CrmServiceClient was introduced we used CrmConnection to authenticate to CRM. When we had to migrate from CrmConnection to CrmServiceClient, we only had to change to CrmServiceClient, extract IOrganizationService and the rest of the code base remains the same.



              Programming to the IOrganizationService interface makes your code far more portable, and reusable. For example; when you don't know your service object is going to be created.



              IOrganizationService orgService = IOrganizationService)conn.OrganizationWebProxyClient ?? conn.OrganizationServiceProxy;


              For testing purposes when you want to mock IOrganizationService with a new MockOrganizationService class.



              When you want to move code between an external application and a plugin. In a plugin the CrmServiceClient is not provided.






              share|improve this answer


























                3












                3








                3







                IOrganizationService is an interface that defines the most basic methods required to access all Dynamics functions. There are a number of general benefits to using interfaces.



                IOrganizationService has been around since CRM 2011, whilst CrmServiceClient was introduced around CRM 2016. A simple reason for using IOrganizationService is that has been around much longer and is present in existing code bases.



                CrmServiceClient implements IOrganizationService, and also provides a range of other methods, e.g. authenticating with CRM. Before CrmServiceClient was introduced we used CrmConnection to authenticate to CRM. When we had to migrate from CrmConnection to CrmServiceClient, we only had to change to CrmServiceClient, extract IOrganizationService and the rest of the code base remains the same.



                Programming to the IOrganizationService interface makes your code far more portable, and reusable. For example; when you don't know your service object is going to be created.



                IOrganizationService orgService = IOrganizationService)conn.OrganizationWebProxyClient ?? conn.OrganizationServiceProxy;


                For testing purposes when you want to mock IOrganizationService with a new MockOrganizationService class.



                When you want to move code between an external application and a plugin. In a plugin the CrmServiceClient is not provided.






                share|improve this answer













                IOrganizationService is an interface that defines the most basic methods required to access all Dynamics functions. There are a number of general benefits to using interfaces.



                IOrganizationService has been around since CRM 2011, whilst CrmServiceClient was introduced around CRM 2016. A simple reason for using IOrganizationService is that has been around much longer and is present in existing code bases.



                CrmServiceClient implements IOrganizationService, and also provides a range of other methods, e.g. authenticating with CRM. Before CrmServiceClient was introduced we used CrmConnection to authenticate to CRM. When we had to migrate from CrmConnection to CrmServiceClient, we only had to change to CrmServiceClient, extract IOrganizationService and the rest of the code base remains the same.



                Programming to the IOrganizationService interface makes your code far more portable, and reusable. For example; when you don't know your service object is going to be created.



                IOrganizationService orgService = IOrganizationService)conn.OrganizationWebProxyClient ?? conn.OrganizationServiceProxy;


                For testing purposes when you want to mock IOrganizationService with a new MockOrganizationService class.



                When you want to move code between an external application and a plugin. In a plugin the CrmServiceClient is not provided.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 7 at 12:32









                James WoodJames Wood

                14.3k22567




                14.3k22567
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030019%2fwhy-use-iorganizationservice-instead-of-crmserviceclient-in-dynamics-365-xrm-too%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    kU4EVRXSI
                    V9uHN64,SMgLcrfCtnKtAQAbhohFWOr54R4k0 785Ku8f v pTF8,pJ7ZcBpK09Zl QfQ,1pVxGi4

                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas