Microsoft Dynamics CRM 2011

What is the difference between IOrganizationService and OrganizationServiceProxy in Dynamics CRM?

Does it have to do with accessing services in the Crm context and outside the Crm context?

+3


source to share


5 answers


The simple answer is IOrganizationService is an interface whereas OrganizationServiceProxy is a class that implements the IOrganizationService interface. This means that any default IOrganizationService properties / methods are also available through OrganizationServiceProxy.

If executed in the context of a plugin or custom workflow action, it will give you access to the IOrganizationService that you can use to poll the CRM.



If you are writing something external, such as a Windows service or standalone application, then you usually use the OrganizationServiceProxy class to set up a connection to the CRM web service. You can obviously assign this to the IOrganizationService later (e.g. dependency injection / for unit testing purposes). Or, if you prefer, there is no reason why you can continue to use OrganizationServiceProxy.

+11


source


The IOrganisationService is used by plugins and custom workflow actions and is obtained from the execution context.



Whereas the OrganizationServiceProxy is mainly used for code that runs outside of the Dynamics CRM application.

+3


source


If you are using sdk assemblies (specifically microsoft.xrm.sdk.dll), then you will use the IOrganizationService implementation, and the call times will be identical. The main purpose of OrganizationServiceProxy is to provide options for establishing a CRM connection in code that runs outside of CRM. OrganizationServiceProxy class implements IOrganizationService and provides an authenticated WCF channel for an organization service

+2


source


OrganizationService

is a higher level class that provides richer functionality on the client side and actually uses it OrganizationServiceProxy

internally.

The Microsoft.Xrm.Client assembly that contains this higher level API cannot be used in plugins, etc., but is intended for rich clients and ASP.NET.

It's worth noting that the Microsoft.Xrm.Client assembly has been removed from the CRM2016 SDK. For 2016 projects, you might consider using XRM Toolking builds.

See msdn.microsoft.com/.../dn689057.aspx

It has similar functionality as you specify in the connection manager -msdn.microsoft.com/.../mt608573.aspx

+2


source


OrganizationServiceProxy is an implementation of IOrganizationService. This is similar to the case, since List is an implementation of the IList interface. When it comes to why on Earth Microsoft provides an interface and implementation besides testing, I can offer one interesting case that happened in my life. I needed to re-read information from crm dynamics. In my case, OrganizationServiceProxy expired faster, then the information was retrieved from CRM. To fix this, I created the following facade:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;

    namespace OrganizationService
    {
        public delegate IOrganizationService CreateOrganizationServiceFunc(string organizationServiceUrl, string userName, string password, TimeSpan? timeout = null, bool useSSL = false);
        public class OrganizationServiceFacade : IOrganizationService
        {
            private IOrganizationService _serviceInternal { get; set; }

            private CreateOrganizationServiceFunc _creator;

            Func<IOrganizationService> _orgServiceFactory;
            public OrganizationServiceFacade(Func<IOrganizationService> orgServiceFactory)
            {
                _orgServiceFactory = orgServiceFactory;
                CreateService();
            }

            private void CreateService()
            {
                _serviceInternal = _orgServiceFactory();
            }

            public void Associate(string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection relatedEntities)
            {
                try
                {
                    _serviceInternal.Associate(entityName, entityId, relationship, relatedEntities);
                }
                catch (System.ServiceModel.Security.MessageSecurityException mex)
                {
                    CreateService();
                    _serviceInternal.Associate(entityName, entityId, relationship, relatedEntities);
                }
            }

            public Guid Create(Entity entity)
            {
                Guid result;
                try
                {
                    result = _serviceInternal.Create(entity);
                }
                catch (System.ServiceModel.Security.MessageSecurityException mex)
                {
                    CreateService();
                    result = _serviceInternal.Create(entity);
                }
                return result;
            }

            public void Delete(string entityName, Guid id)
            {
                try
                {
                    _serviceInternal.Delete(entityName, id);
                }
                catch (System.ServiceModel.Security.MessageSecurityException mex)
                {
                    CreateService();
                    _serviceInternal.Delete(entityName, id);
                }
            }

            public void Disassociate(string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection relatedEntities)
            {
                try
                {
                    _serviceInternal.Disassociate(entityName, entityId, relationship, relatedEntities);
                }
                catch (System.ServiceModel.Security.MessageSecurityException mex)
                {
                    CreateService();
                    _serviceInternal.Disassociate(entityName, entityId, relationship, relatedEntities);
                }
            }

            public OrganizationResponse Execute(OrganizationRequest request)
            {
                try
                {
                    return _serviceInternal.Execute(request);
                }
                catch (System.ServiceModel.Security.MessageSecurityException mex)
                {
                    CreateService();
                    return _serviceInternal.Execute(request);
                }
            }

            public Entity Retrieve(string entityName, Guid id, ColumnSet columnSet)
            {
                try
                {
                    return _serviceInternal.Retrieve(entityName, id, columnSet);
                }
                catch (System.ServiceModel.Security.MessageSecurityException mex)
                {
                    CreateService();
                    return _serviceInternal.Retrieve(entityName, id, columnSet);
                }
            }

            public EntityCollection RetrieveMultiple(QueryBase query)
            {
                try
                {
                    return _serviceInternal.RetrieveMultiple(query);
                }
                catch (System.ServiceModel.Security.MessageSecurityException mex)
                {
                    CreateService();
                    return _serviceInternal.RetrieveMultiple(query);
                }
            }

            public void Update(Entity entity)
            {
                try
                {
                    _serviceInternal.Update(entity);
                }
                catch (System.ServiceModel.Security.MessageSecurityException mex)
                {
                    CreateService();
                    _serviceInternal.Update(entity);
                }
            }
        }
    }

      

and then I have a simple reconnect mechanism:

        Trace.TraceInformation("Creation of CRM connection");
        for (var maxTry = 0; maxTry < reconectionTries; maxTry++)
        {
            try
            {
                var service = new OrganizationServiceFacade(() =>
                    XrmServiceCreator.CreateOrganizationService("organizationServiceUrl",
                        "username",
                        "userpassword",
                        DefaultTimeout));

                var response = (WhoAmIResponse) service.Execute(new WhoAmIRequest());
                if (response.Results.Count == 0)
                {
                    throw new InvalidDataException($"CRM returned no data in response. Number of retries is: {maxTry}, user name: {ConfigSettings.Default.RunAsUser}, Default timeout = {DefaultTimeout}");
                }

                return service;
            }
            catch (Exception e)
            {
                Trace.TraceError("Exception: {0}", e);
            }
        }

      

the XrmServiceCreator.CreateOrganizationService method returns an IOrganizationService instance, and an OrganizationServiceProxy instance is created behind the curtains

+1


source







All Articles