[英]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.