lars pehrsson
lars pehrsson

Reputation: 163

How to create one .net solution for crm 2011 that connects to two different servicecontexts using early bound code

I want to create a VS 2010 C# console application that connects to two (or more) different CRM 2011 servicecontexts / tenants. I want to be able to update data in one with data from the other.

If I create two different early bound classes with crmsvcutil I get a compiler error: "Duplicate 'Microsoft.Xrm.Sdk.Client.ProxyTypesAssemblyAttribute' attribute"

If I concatenate the two files, it compiles but then I get a runtime error: “A proxy type with the name account has been defined by multiple types".

How can this be accomplished?

Upvotes: 4

Views: 1436

Answers (2)

Daryl
Daryl

Reputation: 18895

You can implement the ICustomizeCodeDomService Interface and disable the auto generation of the ProxyTypesAssemblyAttribute. As long as they're in different namespaces you'll have one dll and not have to load two separately.

** UPDATE **

This will not work. Please see https://stackoverflow.com/a/24785440/227436

Upvotes: 0

John
John

Reputation: 246

Create a separate library project for each set of early bound classes and place one of the crmsvcutil files in each library (add assembly references as required). Now, on the console app, add references to the libraries.

Let's say I created two library projects that compile to Proxy1.dll and Proxy2.dll. The root namespaces for each library are Proxy1 and Proxy2. In the ConsoleApp.exe, I add the two references and the following:

var url1 = new Uri(".../XRMServices/2011/Organization.svc");
var proxy1 = new OrganizationServiceProxy(url1, null, null, null);
proxy1.EnableProxyTypes(Assembly.Load("Proxy1")); // Proxy1.dll

var url2 = new Uri(".../XRMServices/2011/Organization.svc");
var proxy2 = new OrganizationServiceProxy(url2, null, null, null);
proxy2.EnableProxyTypes(Assembly.Load("Proxy2")); // Proxy2.dll

using (var context1 = new Proxy1.Proxy1ServiceContext(proxy1))
using (var context2 = new Proxy2.Proxy2ServiceContext(proxy2))
{
  var accounts1 = context1.AccountSet;
  var accounts2 = context2.AccountSet;

  foreach (var account in accounts1) Console.WriteLine("1: {0}: {1}", account.GetType(), account.Id);
  foreach (var account in accounts2) Console.WriteLine("2: {0}: {1}", account.GetType(), account.Id);
}

Upvotes: 5

Related Questions