web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Connect Online CRM Rest API in .Net Core(Console App)

(0) ShareShare
ReportReport
Posted on by 958

Hi all,

I want to connect online CRM account with Rest API(to authenticate) in .Net Core Console App to perform CRUD Operations.

But it shows an error of some compatibility issues.

Error while connecting to CRM Could not load type 'System.ServiceModel.Description.MetadataConversionError' from assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=34567VNXC'

How could i resolve the same? 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    ThomasN Profile Picture
    3,190 on at

    Hi Shakti, thanks for reaching out.

    Can you provide some details around how you are connecting?

    One way to connect from external object is to use Oauth2.0 with client credentials by creating an app user in D365 which stores the secret. Then have object make authentication call to Azure AD to get a bearer token, then pass that token in the header.

    Just giving you quick tip here, if this is right track we can talk more.

  • Shakti Singh Rajput Profile Picture
    958 on at

    Actually my client want to perform CRUD operations through the Rest API and they want a code for the same and authenticate of API with Never Expire token.

    Whenever they want to perform any operation they don't want to push token manually.

    So i do the same in .Net framework using Soap Service URL but when i am going to do the same in .Net Core it display and error of some package is not compatible with that service URL.

    So, How could i do the same with Rest API in .Net Core?

  • Shakti Singh Rajput Profile Picture
    958 on at

    Here is code:-

    using System;

    using System.Net;

    using Microsoft.IdentityModel.Clients.ActiveDirectory;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Client;

    using Microsoft.Xrm.Sdk.Query;

    using System.ServiceModel.Description;

    namespace Dynamic365CodeconversiontoCore

    {

       class Program

       {

           static IOrganizationService service;

           static void Main(string[] args)

           {

               string userName = "************";

               string password = "**********";

               string organizationURL = "[View:https://-------.api.crm.dynamics.com/XRMServices/2011/Organization.svc:750:50]";

               service = ConnectToOnlineMSCRM(userName, password, organizationURL);

               GetAccountByUnit(service, "new_businessunit");

              CreateEntity(service);

               //Console.ReadLine();

           }

           public static IOrganizationService ConnectToOnlineMSCRM(string UserName, string Password, string SoapOrgServiceUri)

           {

               try

               {

                   ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

                   ClientCredentials credentials = new ClientCredentials();

                   credentials.UserName.UserName = UserName;

                   credentials.UserName.Password = Password;

                   Uri serviceUri = new Uri(SoapOrgServiceUri);

                   OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);

                   proxy.EnableProxyTypes();

                   service = (IOrganizationService)proxy;

                   return service;

               }

               catch (Exception ex)

               {

                   Console.WriteLine("Error while connecting to CRM " + ex.Message);

                   Console.ReadKey();

               }

               return service;

           }

           /// <summary>

           /// Create entity

           /// </summary>

           /// <param name="service"></param>

           public static void CreateEntity(IOrganizationService service)

           {

               try

               {

                   Entity account = new Entity("account");

                   account["name"] = "Org-002";

                  account["new_formname"] = "Organization";

                  service.Create(account);

                   Guid accountId = service.Create(account);

               }

               catch (Exception ex)

               {

           throw ex;

       }

           //}

           /// <summary>

           /// get entity

           /// </summary>

           /// <param name="service"></param>

           /// <param name="new_businessunit"></param>

           /// <returns></returns>

           }

       }

    }

  • Shakti Singh Rajput Profile Picture
    958 on at

    Please suggest for the same.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Try with this  - Make sure dont forget to add following reference from SDK -

    Microsoft.Crm.Sdk.Proxy.dll

    Microsoft.Xrm.Sdk.dll

    and .NET framework  System Assembly -

    System.Runtime.Serialization

    System.ServiceModel

    arunpotti.wordpress.com/.../comment-page-1

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Client;

    using Microsoft.Crm.Sdk.Messages;

    using System.Net;

    using System.ServiceModel.Description;

    using System;

    namespace ConnectDynamicsCRM

    {

       class Program

       {

           static void Main(string[] args)

           {

               IOrganizationService organizationService = null;

               try

               {

                   ClientCredentials clientCredentials = new ClientCredentials();

                   clientCredentials.UserName.UserName = "XXXX@XXXynamics.onmicrosoft.com";

                   clientCredentials.UserName.Password = "XXXXXX";

                   // For Dynamics 365 Customer Engagement V9.X, set Security Protocol as TLS12

                   ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                   // Get the URL from CRM, Navigate to Settings -> Customizations -> Developer Resources

                   // Copy and Paste Organization Service Endpoint Address URL

                   organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri("xxxx.crm.dynamics.com/.../Organization.svc&quot;),

                    null, clientCredentials, null);

                   if (organizationService != null)

                   {

                       Guid userid = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).UserId;

                       if (userid != Guid.Empty)

                       {

                           Console.WriteLine("Connection Established Successfully...");

                       }

                       Entity account = new Entity("account");

                       account["name"] = "Org-002";

                       //account["new_formname"] = "Organization";

                       Guid accountId =  organizationService.Create(account);

                   }

                   else

                   {

                       Console.WriteLine("Failed to Established Connection!!!");

                   }

               }

               catch (Exception ex)

               {

                   Console.WriteLine("Exception caught - " + ex.Message);

               }

               Console.ReadKey();

           }

       }

    }

  • Shakti Singh Rajput Profile Picture
    958 on at

    Hy Goutam,

    Is it working in .Net Core console app?

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    yes,I just tested just now . You can download the DLL from here http://xrm.tools/SDK

    and go to pluginregistration  folder .

  • Shakti Singh Rajput Profile Picture
    958 on at

    Sorry for late response,

    But which  DLL file i need to add?

  • Shakti Singh Rajput Profile Picture
    958 on at

    Got it ,

    Thanks again and i am trying to implement the code.

  • gdas Profile Picture
    50,091 Moderator on at

    Shakti ,

    dont forget to close the thread by verifying correct answers , which helps other to implement in future .

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans