1

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

我已使用 CrmSvcUtil.exe 生成自定义 OrganisationServiceContext,我有一个控制台应用程序测试主机,它成功地使用它来创建任务(尽管使用 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);

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

谢谢

4

1 回答 1

3

CustomContext应该接受 type 的参数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}您使用的值,我假设您的请求被沙箱阻止,因为它违反了它的约束

于 2011-08-29T14:16:31.933 回答