简体   繁体   English

为什么在Dynamics 365 XRM工具SDK中使用IOrganizationService而不是CrmServiceClient?

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

Microsoft sample code for accessing Dynamics often looks like this: 用于访问Dynamics的Microsoft示例代码通常如下所示:

    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: 但是同样可以(并且更简单)直接使用Crm服务客户端,如下所示:

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? 我的问题:为什么要使用那个IOrganizationService属性? It seems as if it has only a subset of the functionality of the CrmServiceClient. 看起来好像它只有CrmServiceClient的一部分功能。 And CrmServiceClient used directly seems both faster, simpler, more efficient, and more feature-rich. 直接使用的CrmServiceClient似乎更快,更简单,更高效,功能更丰富。

Any idea about why the sample code always has this additional layer of indirection? 有关为什么示例代码总是有这个额外的间接层的任何想法?

Thanks. 谢谢。

IOrganizationService is an interface that defines the most basic methods required to access all Dynamics functions. IOrganizationService是一个interface ,用于定义访问所有Dynamics功能所需的最基本方法。 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. IOrganizationService自CRM 2011以来一直存在,而CrmServiceClient是围绕CRM 2016引入的。使用IOrganizationService一个简单原因是,它已存在很长时间并存在于现有代码库中。

CrmServiceClient implements IOrganizationService , and also provides a range of other methods, eg authenticating with CRM. CrmServiceClient实现了IOrganizationService ,还提供了一系列其他方法,例如使用CRM进行身份验证。 Before CrmServiceClient was introduced we used CrmConnection to authenticate to CRM. 在介绍CrmServiceClient之前,我们使用CrmConnection对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. 当我们必须从CrmConnection迁移到CrmServiceClient ,我们只需要更改为CrmServiceClient ,提取IOrganizationService ,其余的代码库保持不变。

Programming to the IOrganizationService interface makes your code far more portable, and reusable. IOrganizationService接口进行编程使您的代码更具可移植性和可重用性。 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. 出于测试目的,当您想要使用新的MockOrganizationService类模拟IOrganizationService时。

When you want to move code between an external application and a plugin. 当您想在外部应用程序和插件之间移动代码时。 In a plugin the CrmServiceClient is not provided. 在插件中,不提供CrmServiceClient

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Dynamics 365 XRM Tooling SDK 通过主 ID 一般获取实体记录的枚举集 - How to generically get enumerated set of entity records by primary id with Dynamics 365 XRM Tooling SDK 365更新后无法使用Xrm.Tooling.Connector连接到Dynamics 365 - Can't connect to Dynamics 365 with Xrm.Tooling.Connector after 365 update Dynamics 365:为什么 .net6 控制台客户端出现“Xrm.Sdk WSDL”错误? - Dynamics 365: why I get "Xrm.Sdk WSDL" error for .net6 console client? Dynamics 365-使用IOrganizationService创建OrganizationServiceProxy - Dynamics 365 - Create OrganizationServiceProxy using IOrganizationService 如何防止 Dynamics 365 在每次建立 CrmServiceClient 连接时进行身份验证 - How to prevent that Dynamics 365 authenticates every time a CrmServiceClient connection is made 访问Dynamics365时发生错误“ Microsoft.Xrm.Sdk.dll中发生System.ServiceModel.Security.MessageSecurityException” - An error “System.ServiceModel.Security.MessageSecurityException' occurred in Microsoft.Xrm.Sdk.dll” occurs when accessing Dynamics365 Microsoft Dynamics CRM SDK CRMServiceClient连接字符串缓存错误 - Microsoft Dynamics CRM SDK CRMServiceClient connection string cache bug Dynamics CRM Xrm工具错误“无法分配2147483647字节的托管内存缓冲区。” - Dynamics CRM Xrm Tooling error “Failed to allocate a managed memory buffer of 2147483647 bytes.” 使用XRM SDK从ms dynamics crm获取相关数据 - get related data from ms dynamics crm using XRM SDK Dynamics 365 CRM SDK或Rest API - Dynamics 365 CRM SDK or Rest API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM