简体   繁体   English

在Dynamics CRM 2013的控制台应用程序中创建IOrganizationServiceFactory

[英]Create IOrganizationServiceFactory in Console Application for Dynamics CRM 2013

I am trying to create an instance of IOrganizationServiceFactory as I want to create multiple threads to connect to the service endpoind. 我想创建IOrganizationServiceFactory的实例,因为我想创建多个线程以连接到服务endpoind。 To create multiple thread safe Service Contexts you can use the factory. 要创建多线程安全服务上下文,可以使用工厂。 However I can not seem to create one. 但是我似乎无法创建一个。

Is this possible within a C# Console Application or is it limited for only plugins and workflows ? 这是否可以在C#控制台应用程序中实现?还是仅限于插件和工作流程?

I can create a OrganizationServiceProxy , however I do not know how to proceed from here. 我可以创建OrganizationServiceProxy ,但是我不知道如何从这里继续。

This is the code that I currently have: 这是我目前拥有的代码:

var serverName = (string)ConfigurationManager.AppSettings["OrganisationUrl"];
Uri organisationUri = new Uri(string.Format("{0}/XRMServices/2011/Organization.svc", serverName));
Uri homeRealmUri = null;
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;


var serviceProxy = new OrganizationServiceProxy(organisationUri, homeRealmUri, credentials, null);

You can design a class implementing interface IOrganizationServiceFactory . 您可以设计一个实现IOrganizationServiceFactory接口的类。 This class can be made responsible for creating and issueing IOrganizationService instances. 可以使此类负责创建和发布IOrganizationService实例。

I made a basic example: 我做了一个基本的例子:

class OrganizationServiceFactory: IOrganizationServiceFactory, IDisposable
{
    private readonly ConcurrentBag<OrganizationServiceProxy> _issuedProxies =
        new ConcurrentBag<OrganizationServiceProxy>(); 

    public IOrganizationService CreateOrganizationService(Guid? userId)
    {
        var serverName = (string)ConfigurationManager.AppSettings["OrganisationUrl"];
        Uri organisationUri = new Uri(string.Format("{0}/XRMServices/2011/Organization.svc", serverName));
        var credentials = new ClientCredentials();
        credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;

        var serviceProxy = new OrganizationServiceProxy(organisationUri, null, credentials, null);
        _issuedProxies.Add(serviceProxy);
        return serviceProxy;
    }

    public void Dispose()
    {
        foreach (var serviceProxy in _issuedProxies)
        {
            serviceProxy.Dispose();
        }
    }
}

Clients of the factory can obtain IOrganizationService instances by calling CreateOrganizationService(Guid? userId) , but cannot be responsible for disposing the proxies. 工厂的客户端可以通过调用CreateOrganizationService(Guid? userId)获得IOrganizationService实例,但不能负责处理代理。 The factory will do that for them. 工厂将为他们做到这一点。

Btw, you can make the creation of multiple proxy-instances a bit more efficient using the IServiceManagement<IOrganizationService> interface, but that's another topic. 顺便说一句,您可以使用IServiceManagement<IOrganizationService>接口使创建多个代理实例的效率更高,但这是另一个主题。

You may find this article on MSDN useful: Optimize CRM 2011 Service Channel allocation for multi-threaded processes . 您可能会发现MSDN上的这篇文章有用: 优化多线程进程的CRM 2011服务通道分配

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

相关问题 在CRM Dynamics 2013中创建营销列表 - Creating a Marketing List in CRM Dynamics 2013 列出Dynamics CRM 2013/2015中的实体关系 - List Entity Relationships in Dynamics CRM 2013/2015 QueryByAttribute(Dynamics CRM 2013)空引用异常 - QueryByAttribute (Dynamics CRM 2013) null reference exception Dynamics CRM 2013 SDK帮助程序代码未编译 - Dynamics CRM 2013 SDK helper code not compiling Microsoft Dynamics CRM 2013中支持的事务(CRM-Webservice) - Transactions supported in Microsoft Dynamics CRM 2013 (CRM-Webservice) Microsoft Dynamics 365 CRM 身份验证的示例代码,然后重定向到 C# 控制台应用程序 - sample code for Microsoft Dynamics 365 CRM authentication and then redirect to C# console application 从控制台应用程序调用动态CRM Web服务时如何指定代理详细信息 - How to specify proxy details when calling to dynamics CRM Web Service from Console Application 在CRM Dynamics 2013中,如何检索帐户列表并针对特定用户针对它们创建电话活动 - In CRM Dynamics 2013 how can I retrieve a list of accounts and create phone call activities against them for specific users 是否可以在Dynamics CRM(2013/2016)中以编程方式禁用审核? - Is it possible to disable audit programmatically in Dynamics CRM (2013/2016)? Microsoft Dynamics CRM 2013中的System.Security.SecurityException - System.Security.SecurityException In Microsoft Dynamics CRM 2013
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM