CRM 2011在线插件操作-如何创建OrganizationServiceProxy?

发布于 2024-12-01 21:59:54 字数 1631 浏览 0 评论 0原文

我正在尝试创建一个插件,该插件创建一个任务来响应自定义实体的创建消息

我使用 CrmSvcUtil.exe 生成自定义 OrganizationServiceContext,我有一个控制台应用程序测试主机,它成功地使用它来创建任务(尽管使用 SDK serverConnect.GetServerConfiguration() 来创建 OrganizationServiceProxy)。

当我将插件程序集(沙盒)部署到在线实例时,下面的代码会崩溃:

System.Security.SecurityException:请求类型的权限 'System.Security.Permissions.SecurityPermission,mscorlib, 版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089' 失败

// Obtain the execution context from the service provider.
var executionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(executionContext.UserId);

//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

tracingService.Trace("Buiding");

var organizationUri = new Uri("{theuri}/XRMServices/2011/Organization.svc");          
var credentials = new ClientCredentials();
credentials.Windows.ClientCredential = NetworkCredential)CredentialCache.DefaultCredentials;

var organizationServiceProxy = new OrganizationServiceProxy(organizationUri, null, credentials, null);
organizationServiceProxy.EnableProxyTypes();

var context = new CustomContext(organizationServiceProxy);

有人能指出我正确的方向吗?

谢谢

I'm trying to create a plugin which creates a task in response to the create message for a custom entity.

I've used CrmSvcUtil.exe to generate a custom OrganisationServiceContext, I have a console application test host which successfully uses this to create a task (although using the SDK serverConnect.GetServerConfiguration() to create the OrganizationServiceProxy).

When i deploy the plugin assembly (sandboxed) to the online instance, the code below blows up with:

System.Security.SecurityException: Request for the permission of type
'System.Security.Permissions.SecurityPermission, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
failed

// Obtain the execution context from the service provider.
var executionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(executionContext.UserId);

//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

tracingService.Trace("Buiding");

var organizationUri = new Uri("{theuri}/XRMServices/2011/Organization.svc");          
var credentials = new ClientCredentials();
credentials.Windows.ClientCredential = NetworkCredential)CredentialCache.DefaultCredentials;

var organizationServiceProxy = new OrganizationServiceProxy(organizationUri, null, credentials, null);
organizationServiceProxy.EnableProxyTypes();

var context = new CustomContext(organizationServiceProxy);

Can somebody point me in the right direction?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靑春怀旧 2024-12-08 21:59:55

您的 CustomContext 应接受 Microsoft.Xrm.Sdk.IOrganizationService 类型的参数。像这样(crmsvcutil生成的文件的摘录)

/// <summary>
/// Constructor.
/// </summary>
public CrmContext(Microsoft.Xrm.Sdk.IOrganizationService service) : base(service)
{
}

您可以(并且应该)在插件上下文的帮助下简单地生成连接

var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);

using (var crmContext = new CrmContext(service))
{
 ...
}

根据您使用的 {theuri} 的值,我假设您的请求被沙箱阻止,因为它违反了其约束

Your CustomContext should accept a parameter of type Microsoft.Xrm.Sdk.IOrganizationService. Like this (excerpt of a crmsvcutil generated file)

/// <summary>
/// Constructor.
/// </summary>
public CrmContext(Microsoft.Xrm.Sdk.IOrganizationService service) : base(service)
{
}

You could (and should) simply generate the connection with help of the plugin context

var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);

using (var crmContext = new CrmContext(service))
{
 ...
}

Depending on the value of {theuri} you are using, I assume that your request is blocked by the sandbox as it violates its constraints.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文