简体   繁体   English

Dynamics 365-使用IOrganizationService创建OrganizationServiceProxy

[英]Dynamics 365 - Create OrganizationServiceProxy using IOrganizationService

I have a handler class (some sdk requests are handled here) for a custom entity and this handler is referenced in many plugins/classes. 我有一个用于自定义实体的处理程序类(此处处理了一些sdk请求),并且在许多插件/类中都引用了该处理程序。 This entity must be reached over admin context instead of calling user. 必须通过管理上下文访问此实体,而不是调用用户。 Instead of passing the service "that is created over admin guid" to the handler class, we are trying to impersonate the service inside the handler class. 与其将“通过admin guid创建的”服务传递给处理程序类,我们不打算在处理程序类中模拟该服务。 For example; 例如;

----- Inside the Plugin ----- -----插件内部-----

IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

// Instead of doing something like this;
var adminOrganizationService = factory.CreateOrganizationService(Guid.Parse("~ADMINGUID~"));
MyEntityHandler myEntityHandler = new MyEntityHandler(adminOrganizationService);
// Use myEntityHandler

// What i want to do is, calling my entity handler with the service of calling user;

MyEntityHandler myEntityHandler = new MyEntityHandler(factory.CreateOrganizationService(context.UserId));
// Use myEntityHandler

and inside my entity handler, change the CallerID of the IOrganizationService instance by casting it to the OrganizationServiceProxy first. 然后在我的实体处理程序中,通过将IOrganizationService实例的CallerID强制转换为OrganizationServiceProxy来进行更改。

----- Inside the Handler ----- -----处理程序内部-----

private IOrganizationService service;

public MyEntityHandler(IOrganizationService organizationService)

{  
            // This is what i have tried.
            service = organizationService;
            (service as OrganizationServiceProxy).CallerId = Guid.Parse("~ADMINGUID~");
}

I get 'Exception: System.NullReferenceException: Object reference not set to an instance of an object.' 我收到“异常:System.NullReferenceException:对象引用未设置为对象的实例。” in the casting part. 在铸件中。 Is there any way to do something like this. 有什么办法可以做这样的事情。 I hope i explained myself well enough, thanks... 我希望我能很好地解释自己,谢谢。

This is not working simply because the object that is passed to the plugin is not OrganizationServiceProxy which is used in some external apps but not in the plugins. 这不仅仅因为传递给插件的对象不是在某些外部应用程序中使用但不在插件中使用的OrganizationServiceProxy所致。 The object in plugins is different based on the isolation mode, as far as I remember for not-isolated mode it is Microsoft.Xrm.Extensibility.InprocessProxyService and for Sandbox mode it is Microsoft.Crm.Sandbox.SandboxOrganizationServiceWrapper . 插件中的对象根据隔离模式而有所不同,据我所记得,对于非隔离模式,它是Microsoft.Xrm.Extensibility.InprocessProxyService ;对于沙盒模式,它是Microsoft.Crm.Sandbox.SandboxOrganizationServiceWrapper Both of these types are not in SDK, so you can access their properties only with reflection. 这两种类型都不在SDK中,因此您只能通过反射访问它们的属性。 The first one has CallerId property, so you should be able to access it using reflection, but the second one does not, so I don't think it's possible to replace this caller id. 第一个具有CallerId属性,因此您应该可以使用反射来访问它,但是第二个则不能,因此我认为不可能替换此呼叫者ID。

The only valid approach that you should take is to pass IOrganizationServiceFactory to your handlers instead of IOrganizationService and then create IOrganizationService based on your needs. 您应该采取的唯一有效方法是将IOrganizationServiceFactory而不是IOrganizationService传递给处理程序,然后根据需要创建IOrganizationService If you don't want to change your whole project structure (which I believe would be the best approach, but I already know that people are afraid of refactoring, for no good reason, and we end up in clumsy projects that have to rewritten after few years of such maintenance) just create a second constructor for your handler which will take IOrganizationServiceFactory as a parameter - this would keep your existing code intact and in handlers for which you need to use both admin and not-admin services, you will simply use the second constructor. 如果您不想更改整个项目的结构(我认为这是最好的方法,但是我已经知道人们会出于无理由而害怕重构,而我们最终会遇到笨拙的项目,这些项目之后必须重新编写)几年的维护)只需为您的处理程序创建第二个构造函数,该构造函数将IOrganizationServiceFactory作为参数-这将使您现有的代码保持完整,并且在需要同时使用admin和IOrganizationServiceFactory -admin服务的处理程序中,您只需使用第二个构造函数。

public class MyHandler
{
    private IOrganizationService service;
    private IOrganizationService adminService;

    public MyHandler(IOrganizationService service)
    {
        this.service = service;
    }

    public MyHandler(IOrganizationServiceFactory factory, Guid userId) //instead of passing userId here, it would be better to pass it to the method that you want to perform
    {
        this.service = factory.CreateOrganizationService(userId);
        this.adminService = factory.CreateOrganizationService(null);
    }
}

Or simply that way: 或简单地这样:

public class MyHandler
{
    private IOrganizationService service;
    private IOrganizationService adminService;

    public MyHandler(IOrganizationService service)
    {
        this.service = service;
    }

    public MyHandler(IOrganizationService service, IOrganizationService adminService) : this(service)
    {
        this.adminService = adminService;
    }
}

This is simply the example, of course I do not know much about your architecture, but for sure such approach would be much better, cleaner and easier to maintain in the future then what you are trying to do right now (for no good reason, again - don't be afraid of refactoring the code, most of the work would do Visual Studio for you...) 这只是示例,当然我对您的体系结构了解不多,但是可以肯定的是,这种方法将来会比您现在正在尝试的方法更好,更清洁,更容易维护(没有充分的理由,再次-不要害怕重构代码,大多数工作都会为您做Visual Studio ...)

You can pass null so that System User can be impersonated by factory during CreateOrganizationService . 您可以传递null以便在CreateOrganizationService期间工厂可以模拟系统用户。

// Use the factory to generate the Organization     
Service.OrganizationServiceImpersonated = factory.CreateOrganizationService(null);

Read more 阅读更多

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

相关问题 为什么在Dynamics 365 XRM工具SDK中使用IOrganizationService而不是CrmServiceClient? - Why use IOrganizationService instead of CrmServiceClient in Dynamics 365 XRM tooling SDK? 如何重构方法以用 IOrganizationService 替换 OrganizationServiceProxy - How to refactor method to replace OrganizationServiceProxy with IOrganizationService 使用 Rest 在 Dynamics 365 Business Central 中创建项目 API - Create Item in Dynamics 365 Business Central using Rest API 适用于Microsoft Dynamics CRM的IOrganizationService的单个实例 - Single instance of IOrganizationService for microsoft dynamics crm 无法在 Dynamics CRM 365 online 中创建系统用户 - Unable to Create a systemuser in Dynamics CRM 365 online Dynamics 365 - 为“审计”实体注册“创建”步骤 - Dynamics 365 - Register a 'Create' step for 'audit' entity 如何使用HttpWebRequest在Dynamics 365中发布数据 - How to POST data in Dynamics 365 using HttpWebRequest 使用Dynamics 365 Web Api控制台的异常 - Exception using dynamics 365 web Api Console 使用 Kingswaysoft 将注释上传到 Dynamics 365 - Upload annotation into Dynamics 365 using Kingswaysoft 使用 CorrelationId 上下文更新 Dynamics 365 中的实体 - Updating entity in Dynamics 365 using CorrelationId context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM