Microsoft Dynamics CRM 2011

时间:2015-05-06 06:18:30

标签: dynamics-crm crm microsoft-dynamics

Dynamics CRM中 IOrganizationService OrganizationServiceProxy 有什么区别?

它是否与访问Crm上下文和Crm Context之外的服务有关?

5 个答案:

答案 0 :(得分:12)

最简单的答案是IOrganizationService是一个接口,而OrganizationServiceProxy是一个实现IOrganizationService接口的类。这意味着默认情况下,IOrganizationService上的任何属性/方法也可通过OrganizationServiceProxy获得。

如果在插件或自定义工作流活动的上下文中运行,它将允许您访问可用于询问CRM的IOrganizationService。

如果您正在编写外部文件,例如Windows服务或独立应用程序,那么您通常使用类OrganizationServiceProxy来建立与CRM Web服务的连接。显然,您可以稍后将其分配给IOrganizationService(例如依赖注入/用于单元测试)。或者,如果您愿意,没有理由继续使用OrganizationServiceProxy。

答案 1 :(得分:3)

IOrganisationService在插件和自定义工作流活动中使用,并从执行上下文派生。

虽然OrganisationServiceProxy主要用于在Dynamics CRM应用程序之外运行的代码。

答案 2 :(得分:2)

如果您正在使用sdk程序集(特别是microsoft.xrm.sdk.dll),那么您将使用IOrganizationService的实现,并且调用时间将相同。 OrganizationServiceProxy的主要目的是提供在CRM外部运行的代码中建立与CRM连接的选项.OrganioServiceProxy类实现IOrganizationService并为组织服务提供经过身份验证的WCF通道

答案 3 :(得分:2)

OrganizationService是一个更高级别的类,它提供更丰富的客户端功能,并且实际上在其中使用OrganizationServiceProxy

包含此更高级API的Microsoft.Xrm.Client程序集不能在插件等中使用,但适用于富客户端和ASP.NET。

值得注意的是,Microsoft.Xrm.Client程序集已从CRM2016 SDK中删除。对于2016项目,您可以考虑使用XRM Toolking程序集。

请参阅msdn.microsoft.com /.../ dn689057.aspx

它具有与连接管理器相似的功能-msdn.microsoft.com /.../ mt608573.aspx

答案 4 :(得分:1)

OrganizationServiceProxy是IOrganizationService的实现。它与List是接口IList的实现类似。如果要谈论为什么在地球上除了测试之外,微软还提供接口和实现,我可以提出一个在我生命中发生的有趣案例。我需要重新阅读动态crm的信息。在我的情况下,OrganizationServiceProxy过期更快,然后从CRM收到信息。为了解决这个问题,我创建了以下立面:

    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);
                }
            }
        }
    }

然后我有了简单的重新连接机制:

        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);
            }
        }

方法XrmServiceCreator.CreateOrganizationService返回IOrganizationService的实例,并在窗帘后面创建OrganizationServiceProxy的实例