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)

How to declare a global variable in WCF application for CRM integration, where we can keep organisation service and use in different levels of DLLs in project?

(0) ShareShare
ReportReport
Posted on by 592

few more details :

I am creating organisation service objects and trying to use different levels of DLLs in project.

1.  static variables we cant add as its not thread safe. ( I have tried this approach and got some error) 

2. WCF application its a ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerCall means multiple threads can access the WCF service. 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Nishkarsh Vaish Profile Picture
    335 on at

    Hi

    As soon as I understand,you can add it in a new file as Class with below code and call it everywhere as Global.service():-

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Client;

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.ServiceModel.Description;

    using System.Text;

    using System.Threading.Tasks;

    using System.Net;

    namespace Console

    {

       class Global

       {

           public static IOrganizationService service()

           {

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;

               ClientCredentials Credentials = new ClientCredentials();

               Credentials.UserName.UserName = "UserID.onmicrosoft.com";

               Credentials.UserName.Password = "Password";

               Uri OrganizationUri = new Uri("yourorganizationurl.dynamics.com/.../Organization.svc");

               Uri HomeRealmUri = null;

               using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))

               {

                   serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

                   IOrganizationService service = (IOrganizationService)serviceProxy;

                   return service;

               }

           }

       }

    }

  • MS CRM DYNAMICS Profile Picture
    592 on at

    Suppose I am using organisation service 5 times in my project and if I call 5 times this function then this will increase the performance by calling the CRM 5 times to create the organisation service . Correct me if I am wrong. But I need to call CRM once and use the organisation service across my code.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Bhabani,

    Take a look at this document-

    msdn.microsoft.com/.../gg509027.aspx;MSPPError=-2147217396#Performance

    It talks about caching the organizationserviceproxy instance and reuse it within the application along with some more points.

    Hope this helps.

  • Nishkarsh Vaish Profile Picture
    335 on at

    Run it when your project starts and store it in global class and call it from there.I thought it will work for you.

    Refer this link:-

    https://stackoverflow.com/questions/1293926/c-sharp-winforms-global-variables

  • MS CRM DYNAMICS Profile Picture
    592 on at


    Hi any one having idea how to implement below lines

    var container = IoC.Initialize();
    _configurationService = container.GetInstance<IConfigurationService>();

    -------------------------------------------------------------------------------------------

    public static IOrganizationService CRMService
    {
    get
    {
    try
    {
    lock (_lockObject)
    {
    if (instance == null)
    {
    var container = IoC.Initialize();
    _configurationService = container.GetInstance<IConfigurationService>();


    instance = Connect();
    }
    return instance;
    }
    }
    catch (Exception ex)
    {
    throw new Exception("Unable to connect to CRM", ex);
    }

    }
    }

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Bhabani,

    Though I am not sure whats your implementation but below link may help you.

    dynamicscrmcoe.com/implementing-dependency-inversion-dynamics-365

    Also, if you have got the answer for you initial question "global variable for crm service" then please share it here so that it can help others :)

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