1
votes

I have been working on a few weeks on a program to e-mail reminders about quotes. The program uses the CRM Dynamics 2016 SDK, leveraging early bound classes. The program connects to the CRM server, fetches data, and uses the aforementioned early bound classes to manipulate this data.

The core of my problems is based around the connection to the CRM server. It is connecting similar to the below:

var result123 = from r in xrm.QuoteSet where r.kro_QuotationNumber.StartsWith("123") select r;

The connection is configured in App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>


    <configSections>
      <section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client" />
    </configSections>

    <microsoft.xrm.client>
        <contexts>
          <add name="Xrm" type="Itelios.Crm.Business.Dynamics.XrmServiceContext" />
        </contexts>
    </microsoft.xrm.client>
    <connectionStrings>
        <add name="Xrm" connectionString="Server=https://crm.OrganizationName.com/XRMServices/2011/Organization.svc; Domain:OrganizationDomain; authtype=IFD; [email protected]; Password=password"/>
  </connectionStrings>

<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>

Here is the code of the connection in the main fonction :

    //To create the contexte, we should create an IOrganizationService
        OrganizationServiceProxy serviceproxy = null;
        try
        {
            ServerConnection server = new ServerConnection();
            server.ReadConfigurations();

            ServerConnection.Configuration config = server.configurations[0];
            //config = server.configurations.GetRange(1,1).First<ServerConnection.Configuration>();

            serviceproxy = new OrganizationServiceProxy(config.OrganizationUri, config.HomeRealmUri, config.Credentials, config.DeviceCredentials);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        serviceproxy.EnableProxyTypes();
        IOrganizationService service = (IOrganizationService)serviceproxy;


        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };


        //CRM Context Creation
        using (var xrm = new Xrm(service)) { etc. }

The code was working until we tried to create a executable file from this program, so that it could be utilized as a schedule task, and be run from a different server. With a few tries, I conclude the problem I was facing wasn't due to the executable file, but due to the computer change.

I tried the following:

  1. Changing the authentication type to AD, instead of IFD
  2. Adding a line of code that could negate the Token security check
  3. Trying to get the code on this page working
    • In this one, I don't know what is a relying party though.
  4. Trying a lot of things around the connection String, all of them don't mess up with the working code, but don't seem to do anything either.
    • Removing the Organization Name part in the server connection.
    • Removing the /XRMServices/2011/Organization.svc since it's an IFD connection
    • Changing the attribute "Url" into "Server" or "ServiceUri"
    • Adding "LoginPrompt=Never" at the end of the connection String
  5. Adding Xrm.Tooling.Connector reference, using CrmServiceClient to connect to the server.

The error is happening right after fetching the data I've searched for with the request.

Does anyone have advice on how to resolve this error, or perhaps insight into why it is being thrown?

1
Are you using Xrm.Tooling to make the connection? Your url does not have to contain the "/XRMServices/2011/Organization.svc" part, only the CRM address. Also it certainly is not a correct IFD configuration, because correct IFD configuration should be "orgname.something.com" without any organization name added after "/". Can you connect to the CRM using web browser from the server which is supposed to run your application? If yes, then are you using AD or Claims? Also post the full code which you use for connection. - Pawel Gradecki
Instead of Xrm.Tooling, the thing I used to make the connection was CrmSrvUtil.exe, generating the early bound classes. I'm using the CrmServiceHelper files though. My "some.th.ing" is actually like so "crm._orgname_.com" where I'll keep "orgname" secret for privacy purposes. Employees can access to the CRM online, even from the computer which is supposed to run the application, the problem is coming only from the code, not from the server, IMO. In the code itself, I'm creating a CRM context with IOrganizationService - Loowood
I noticed though I didn't add details concerning the error in my question. Technically, everything works well until the request is fetched. I think that means the connection is quite fine at first sight, but I might be wrong. The error have the ID 3242, and it's thrown at the foreach. Also for @PawelGradecki, I'm using AD FS. - Loowood
Please post the full code of your connection, don't describe it in a comment, simply edit your question. It's not possible to help you, you are too general, you must be more specific. Is your organization name "crm"? When you go to your Customizations -> Developer tools, is the organization service URL matching your crm address? Have you tried to debug your code on the server which is supposed to run it? Can't you simply include more detailed logging in your app to catch whatever error is there? - Pawel Gradecki
Sorry about the comments, I thought it would be better to just link my code. Nevertheless, I added back the full code in App.config, and the connection part in my main fonction (and thus in my main program). My organization name isn't "crm", it's the thing in between. For instance, if it were "test", my connection string would be "crm.test.com". I can indeed debug my code in the computer which is supposed to run it, that's why I don't need more logging. There is already a try/catch around the important parts. Also, I don't see where are those "Developer Tools" you're talking about ... - Loowood

1 Answers

0
votes

Nevermind, I found the root of my problems.

When I first started to use the SDK, I linked CrmServiceHelper as I said in my question. Although, since it was a very generic file, there was some libraries to install, especially Microsoft.IdentityModel. At this time, I didn't know it would be something crucial for my program to run, so I just commented the part concerning the Security Token.

While trying to re-install everything in another server, I found this the same bug, this time aware that Security Tokens were important.

Long story short, you have just to import Microsoft.IdentityModel from Nuget Package Tool Manager.

Since this problem was very related to connection disfunctionments, I was lured into thinking the bug was coming from the Connection part of my code.