【问题标题】:How to EnableProxyTypes on a service accessed from the IWorkflowContext in CRM 2011?如何在从 CRM 2011 中的 IWorkflowContext 访问的服务上启用代理类型?
【发布时间】:2019-08-15 23:51:42
【问题描述】:

我有一个 C# 工作流,我正在尝试创建一个 IOrganzationService,其中启用了代理类型,因此我可以使用我早期绑定的数据类型...

这就是我创建IOrganizationSerivce的方式

IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

但由于我没有OrganizationServiceProxy 对象,我无法在服务上调用 EnableProxyTypes(),并且使用早期绑定实体对服务的任何创建调用都会失败。

我知道我可以恢复到在 app.config 中设置服务器 url url、sdk 服务器 url 和组织,并使用它来创建一个 OrganizationServiceProxy,但似乎我应该能够只在我已经拥有的 IOrganizationService...

更新

我相信这是 2011 年和 2013 年的一个错误,早就解决了。请参阅 Jim 的答案以及为什么您不应该使用 2015 年或更新实例的已接受答案的链接。

【问题讨论】:

    标签: dynamics-crm-2011


    【解决方案1】:

    这是一篇旧帖子,解决了很久以前修复的问题。但是这里提供的一些解决方法不受支持,会导致使用它们的工作流活动中断。

    请看这个:Remove unsupported code that uses reflection in custom workflow activities

    【讨论】:

      【解决方案2】:

      为了能够在工作流的上下文中使用早期绑定的实体,请尝试以下 sn-p:

      IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
      IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
      
      var type = Type.GetType("Microsoft.Crm.Workflow.SynchronousRuntime.WorkflowContext, Microsoft.Crm.Workflow, Version=5.0.0.0");
      type.GetProperty("ProxyTypesAssembly").SetValue(serviceFactory, typeof(YourServiceContext).Assembly, null); //YourServiceContext - the name of crm context
      IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId); 
      

      如您所见,有一个名为ProxyTypesAssembly 的属性指定您保留早期绑定实体的程序集。我可以设置此属性的唯一方法是使用反射,因为无法访问 WorkflowContextBaseWorkflowContext 的基类)。

      【讨论】:

      • 这正是我所需要的!我很惊讶这没有更多的记录......
      • 这似乎在 CRM 2013 中被破坏,type 为空。你遇到过变通的方法吗?
      • @AntSwift 在下面查看我针对 CRM 2013+ 修复的新答案
      • 使用此解决方法的任何人都必须将其删除。它不再需要,并且会导致工作流活动中断。更多信息:Remove unsupported code that uses reflection in custom workflow activities
      【解决方案3】:

      使用反射的答案并不理想,您将无法在沙盒隔离模式下注册您的程序集。

      您可以将以下内容添加到 Plugin 或 Workflow 项目中 AssemblyInfo.cs 文件的末尾。

      [assembly: Microsoft.Xrm.Sdk.Client.ProxyTypesAssembly()]
      

      【讨论】:

        【解决方案4】:

        对于 CRM 2013(我希望及以后)的修复要简单得多:

        IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
        IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
        
        // this is the important change
        var property = serviceFactory.GetType().GetProperty("ProxyTypesAssembly");
        
        property.SetValue(serviceFactory, typeof(YourServiceContext).Assembly, null);
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId); 
        

        【讨论】:

        • 您可以在沙盒环境中使用反射,只要您不访问通常无法访问的内容。所以你可以反射性地调用公共方法,但不能调用私有方法
        【解决方案5】:

        几个月前我发现了同样的问题。 您遇到的问题是由于 api 不一致造成的。

        OrganizationServiceProxy 实现了 IOrganizationService,它只有少数成员 create、update 等。它还继承自 ServiceProxy,成员 ClientCredentials、IsAuthenticated、DeviceCredentials 等。

        现在您在接口或抽象基类上找不到的是 EnableProxyTypes 成员。

        所以基本上你的运气不好。我结束了使用具体的 OrganizationServiceProxy。

        看看这里并自己做比较。

        http://technet.microsoft.com/en-us/library/microsoft.xrm.sdk.iorganizationservice_members.aspx, http://technet.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationserviceproxy.aspx, http://technet.microsoft.com/en-us/library/gg306039.aspxhttp://technet.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationserviceproxy_methods.aspx

        猜想创建 api 的 ms crm 开发团队应该使用 tdd 和一个模拟框架在开发早期就开始使用它。

        干杯 鲁斯汀

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-08-19
          • 2013-06-04
          • 2011-08-18
          • 1970-01-01
          • 2011-07-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多