Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Console App - Early Bound Classes - 2 Organizations

Posted on by Microsoft Employee

Hello,

I'm trying to learn Dynamics 365/CRM development.

In my current scenario, I need to build a console application that connects to two different organizations : in fact, I want to copy/transform some data from Org1 to Org2.

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

 

I've found a similar post here: https://stackoverflow.com/questions/10548683/how-to-create-one-net-solution-for-crm-2011-that-connects-to-two-different-serv but there is no source code for the Proxy.DLL. In consequence, I can't reproduce this proposed solution.

 

You may have some additional explanations or an other solution?
Thank you for your help.

*This post is locked for comments

  • tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: Console App - Early Bound Classes - 2 Organizations

    It's good you make it working :) Good luck with your project :)

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Console App - Early Bound Classes - 2 Organizations

    Else if I'm a beginner, your advices were sufficient precious for me.

    After some adjustments, I can now read accounts1 and accounts2 lists.

    A lot of thanks, Pawel :)

  • Verified answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: Console App - Early Bound Classes - 2 Organizations

    Well a long road ahead of you. I believe that you should first go through some tutorials of how to connect to CRM environment like this one:

    msdn.microsoft.com/.../jj602970.aspx

    Unfortunately based on your code I can tell that you are also not very experienced in C#, so this will be hard for you until you will brush up your coding skills in general. Anyway I will post my comments here - maybe they will be helpful.

    Firstly - never ever copy the code from Internet and just assume it's working. The crucial thing is to UNDERSTAND what the code does. There is not point in using someone else's work if you cannot learn anything from it.

    You don't need Microsoft.CrmSdk.Extensions, they are outdated. Remove them from all projects.

    When you are generating Early Bound classes, you can specify /serviceContextName:<name>, basically it's a class which will help you to do Linq to CRM calls, so in your case it should be /serviceContextName:XrmSourceServiceContext for source org and /serviceContextName:XrmTargetServiceContext. There is also /namespace parameter where you can specify namespace for the classes. In your case it should be Proxy1 or Proxy2. This is what is causing the error in your code and the reason I believe that you need to go through some programming tutorials first, because knowledge of classes, namespaces and objects is crucial before even writing "Hello world" - not mentioning application communicating with CRM.

    Also this lines:

    var proxy2 = new OrganizationServiceProxy(url2, null, null, null);

    are of course bad, you have to connect to CRM, not provide nulls everywhere (of course that the code was simplified on SO answer). See my link and try to connect to your organization and obtain IOrganizationService. I believe that you will not need to use all those EnableProxyTypes lines but I cannot verify it by myself right now.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Console App - Early Bound Classes - 2 Organizations

    Hi Pawel,

    Thank you for your feedback.

    As I told you : I’m a beginner. And there a lot of things that I need to understand, else if obvious for you.

    I’ll do my best to explain my actions, step by step : I need to identify where I’m wrong.

     

    Visual Studio 2017 Community :

    1. New Project > Class Library (.NET Framework) > Name : Proxy1

    Tools > NuGet Manager > Install Microsoft.CrmSdk.CoreAssemblies v8.2.0.2 & Install Microsoft.CrmSdk.Extensions v7.1.0.1 (Microsoft.CrmSdk.Deployment v7.1.0 included)

    Add Existing Element : XrmSource.cs (generated with CrmSvcUtil)

    Build solution

     

    2. Proxy2 : Idem #1

     

    3. New Project > Console Application (.NET Framework) > Name : MyConsoleApp

    Tools > NuGet Manager > Install Microsoft.CrmSdk.CoreAssemblies v8.2.0.2 & Install Microsoft.CrmSdk.Extensions v7.1.0.1 (Microsoft.CrmSdk.Deployment v7.1.0 included)

    Copy/Paste StackOverflow Code : https://stackoverflow.com/questions/10548683/how-to-create-one-net-solution-for-crm-2011-that-connects-to-two-different-serv

    Add :

    - using Microsoft.Xrm.Sdk.Client;

    - using System.Reflection;

    References > Add a reference > Browse ..\Proxy1\Proxy1\bin\Debug\Proxy1.dll

    References > Add a reference > Browse ..\Proxy1\Proxy1\bin\Debug\Proxy2.dll

    At this time, I have the following code

    using System;
    using Microsoft.Xrm.Sdk.Client;
    using System.Reflection;
    
    
    namespace MyConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                var url1 = new Uri("mycrmdev.crm4.dynamics.com/.../Organization.svc");
                var proxy1 = new OrganizationServiceProxy(url1, null, null, null);
                proxy1.EnableProxyTypes(Assembly.Load("Proxy1")); // Proxy1.dll
    
                var url2 = new Uri("mycrmprod.crm4.dynamics.com/.../Organization.svc");
                var proxy2 = new OrganizationServiceProxy(url2, null, null, null);
                proxy2.EnableProxyTypes(Assembly.Load("Proxy2")); // Proxy2.dll
    
                using (var context1 = new Proxy1.XrmSourceServiceContext(proxy1))
                using (var context2 = new Proxy2.XrmTargetServiceContext(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);
                }
    
            }
        }
    }
    

    And I get errors here :

    using (var context1 = new Proxy1.Proxy1ServiceContext(proxy1))
    using (var context2 = new Proxy2.Proxy2ServiceContext(proxy2))

    I don't know what to do at this moment :(
  • Suggested answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: Console App - Early Bound Classes - 2 Organizations

    The solution from StackOverflow is correct. This Proxy.dll simply contains early bound classes. So create two separate class libraries and generate early bounds for one org in one library, and for the second org in second library. Then reference both libraries in your application and load different proxy assemblies for both IOrganizationService instances that you will be using.

    This is really clearly described in StackOverflow answer, I don't understand what problem do you have with following this guidelines. As far as I understand, you are able to generate early bounds for both organizations right?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans