I have a simple class that writes to XRM. It has a method that adds to multiple orgs.
So the code below works; it creates the IOrganization service fine. But there is some overhead with it. What if you want to jump around, writing to one org and then the next? That can be expensive.
So the code below works; it creates the IOrganization service fine. But there is some overhead with it. What if you want to jump around, writing to one org and then the next? That can be expensive.
Uri OrganizationUri = new Uri(String.Format("{0}/{1}/XRMServices/2011/Organization.svc",
_server, org));
using (OrganizationServiceProxy
serviceProxy = new OrganizationServiceProxy(OrganizationUri,
null, _credentials, null))
{
serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
return (IOrganizationService)serviceProxy;
}
The idea is to keep a list of initialized Orgs, and re-use them when needed. So I create a XRMGateway class with this private member:
private List<KeyValuePair<string,
IOrganizationService>>
_organizationServices;
In my constructor I initialize the object:
_organizationServices = new
List<KeyValuePair<string, IOrganizationService>>();
Then whenever I want to write to the new org, I do this:
public void AddBillHeader(string org, ApprovalStatus
approvalStatus, )
{
IOrganizationService svc = GetXrmService(org);
//do
my stuff
}
The magic lies in the GetXrmService(org) method:
private IOrganizationService
GetXrmService(string org)
{
try
{
foreach (KeyValuePair<string, IOrganizationService>
kvp in _organizationServices)
{
if (kvp.Key == org)
{
return kvp.Value;
}
}
Uri OrganizationUri = new
Uri(String.Format("{0}/{1}/XRMServices/2011/Organization.svc",
_server, org));
using (OrganizationServiceProxy
serviceProxy = new OrganizationServiceProxy(OrganizationUri,
null, _credentials, null))
{
serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
KeyValuePair<string,
IOrganizationService> kvp = new KeyValuePair<string, IOrganizationService>(org,
(IOrganizationService)serviceProxy);
_organizationServices.Add (kvp);
return (IOrganizationService)serviceProxy;
}
}
catch (Exception
ex)
{
throw;
}
}
No comments:
Post a Comment