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

时间:2011-08-28 08:10:38

标签: plugins dynamics-crm-2011 dynamics-crm-online

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

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

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

  

System.Security.SecurityException:请求类型的权限   'System.Security.Permissions.SecurityPermission,mscorlib,   Version = 4.0.0.0,Culture = neutral,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);

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

由于

1 个答案:

答案 0 :(得分:3)

您的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}的值,我认为您的请求被沙箱阻止,因为它违反了constraints