locked
[CRM 2011] Strange error SOMETIMES happens when creating users from code RRS feed

  • Question

  • Sometimes I get the following error when attempting to create a user programmatically from our application.

    The Web Service plug-in failed in OrganizationId: 6179c202-d76e-e311-bf63-f46d048de5bd; SdkMessageProcessingStepId: 5acabb1b-ea3e-db11-86a7-000a3a5473e8; EntityName: systemuser; Stage: 30; MessageName: Create; AssemblyName: Microsoft.Crm.Extensibility.InternalOperationPlugin, Microsoft.Crm.ObjectModel, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35; ClassName: Microsoft.Crm.Extensibility.InternalOperationPlugin; Exception: Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
       at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Web.Services.Protocols.LogicalMethodInfo.Invoke(Object target, Object[] values)
       at Microsoft.Crm.Extensibility.InternalOperationPlugin.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.V5PluginProxyStep.ExecuteInternal(PipelineExecutionContext context)
       at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)
    Inner Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
       at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args)
       at Microsoft.Crm.SecurityUtils.AddPrincipalToGroupByName(String principalName, Guid groupId, Boolean limitGlobalCatalogSearches)
       at Microsoft.Crm.SecurityUtils.AddPrincipalToGroupByName(String principalName, Guid groupId)
       at Microsoft.Crm.BusinessEntities.SecurityLibrary.AddPrincipalToGroupByName(String NTName, Guid groupId)
       at Microsoft.Crm.Authentication.UserManagementFactory.CheckForActiveDirectoryUser(String uniqueName, UserValidationParameters userValidationParameters, ExecutionContext context, Boolean limitGlobalCatalogSearches)
       at Microsoft.Crm.Authentication.UserManagementFactory.ValidateActiveDirectoryUser(String domainName, UserValidationParameters userValidationParameters, ExecutionContext context, Boolean limitGlobalCatalogSearches)
       at Microsoft.Crm.Authentication.UserManagementFactory.CreateUser(IBusinessEntity systemUser, Boolean setupUser, ExecutionContext context)
       at Microsoft.Crm.ObjectModel.SystemUserServiceInternal`1.CreateInternal(Guid organizationId, IBusinessEntity systemuser, ExecutionContext context)
       at Microsoft.Crm.ObjectModel.SystemUserServiceInternal`1.Create(IBusinessEntity systemuser, ExecutionContext context)
    Inner Exception: System.IO.FileNotFoundException: The system cannot find the file specified.


    .

    This is the Event Viewer log - the actual exception that I receive in code is unhelpful ("Unexpected error has occurred"). When this error occurs then the issue persists for a number of minutes before resolving on its own.

    What could be the reason? Is there a workaround?


    Friday, December 27, 2013 10:31 AM

All replies

  • Hi,

    Can you respond to the following queries:

    1. which crm version is it?
    2. has it been migrated from earlier version?
    3. are you assigning business unit to the user you are creating? (Look for sample here)
    4. share the code where you are trying to create the user.

    In case point 3 is the issue, I hope it helps to resolve your issue otherwise feel free to share the details so as to provide further solution.

    Thanks & Regards,


    Anubhav Bajpai

    Friday, December 27, 2013 11:07 AM
  • Have you tried monitoring the CRM Service Up-Time?If this issue occurs only a few times and resolve on it`s own maybe is the CRM Service that stops working

    Friday, December 27, 2013 11:31 AM
  • Hi,

    Can you respond to the following queries:

    1. which crm version is it?
    2. has it been migrated from earlier version?
    3. are you assigning business unit to the user you are creating? (Look for sample here)
    4. share the code where you are trying to create the user.

    In case point 3 is the issue, I hope it helps to resolve your issue otherwise feel free to share the details so as to provide further solution.

    Thanks & Regards,


    Anubhav Bajpai

    1. Apologies. It's CRM 2011.
    2. No, it's a fresh installation on a development machine.
    3. Of course! Anyway, if the problem was due to lack of BU, then the problem would occur all the time, not randomly. Also, note that the stack trace points to an error in a Microsoft code. Normally, when trying to perform operations with incorrect or missing data a more specific error occurs pointing towards the problem.
    4. I don't feel comfortable sharing the code at the moment. Because the error appears randomly, I don't feel it's due to my code. I can cook up a console application with simpler code doing something similar to see if that also fails - if it does, I'll post that.


    Saturday, December 28, 2013 12:26 PM
  • Have you tried monitoring the CRM Service Up-Time?If this issue occurs only a few times and resolve on it`s own maybe is the CRM Service that stops working


    The service is up and running. My code fails, but the CRM web site is working.
    Saturday, December 28, 2013 12:28 PM
  • Hi,

    Just a quick check: is your code running in context of "Calling User" or system administrator.

    As per my understanding, everything remains same for all calls to create user except the context being which may vary. It seems sometimes the context is not carrying the complete values which may be required for creating the users.

    Also, since this is sporadic issue. Give a try to this solution, although weird but may get the issue through.

    I hope you have already done the basic steps like:

    1. Recycle the AppPool in IIS,
    2. restart IIS
    3. restart the async service.

    Addition: If these are users from a federated domain, you will need to add them as username@domain.com instead of the domain\username. In case if this is coming in picture. 

    Let me know your progress for the same.

    Thanks & Regards,


    Anubhav Bajpai


    • Edited by Anubhav Bajpai Monday, December 30, 2013 11:46 AM Added "Addition"
    Monday, December 30, 2013 11:36 AM
  • Hi,

    Is it an app that's running for a long time? The credentials cache can time out if not refreshed, or something similar, I have seen it a few times, some token that needs refreshing. It might be a long shot with this crappy description but I don't recall exactly what was happening.

    Regards


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    Monday, December 30, 2013 3:03 PM
  • The application is (currently) using the CRM administrators login data, and it is a separate application (not a CRM plugin or anything like that). It's definitely not a cache issue - when the problem occurs IISRESET doesn't help.

    Even the context (as per Anubhav Bajpai suggestion) remains the same - for the time being the CRM login information is hard-coded.

    At the moment the users created by the application are regular domain users, so there's no need for the federated username format. Anyway, once more, the issue occurs spontaneously.

    In fact, if it starts to fail, it will continue to fail creating the same user (same data provided) for a number of minutes (it's difficult to pinpoint the exact time, but I'd wager it's somewhere between 5 and 10 minute) and after a while it'll work.

    EDIT:
    Also, please note the end line of the stack trace:

    Inner Exception: System.IO.FileNotFoundException: The system cannot find the file specified.

    Personally I found this VERY strange...

    Tuesday, December 31, 2013 2:01 PM
  • Hi,

    I'd suggest that you turn on tracing during the 5-10 min failure window to capture more error messages. I am hoping that this would let you know which file MSCRM is not able to find. 


    Ronald

    Tuesday, December 31, 2013 4:40 PM
  • Hi,

    What I meant by cache is not the IIS, but the credential cache in the application which might time out after some 30 minutes. If that is the case the only reason it could bounce back is if the credential cache is updated. I don't have the entire picture clear if that isn't obvious but I've run into similar problems and in that case it was some security token that had timed out.

    Good Luck!


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    Thursday, January 2, 2014 8:30 AM
  • The external application is an internal web service, so IISRESET most definitely should clear the cache I reckon.

    Than again, I'm not entirely certain if the credential cache actually needs clearing - who's exact credentials are being cached and problematic? The user being created, or the administrators credentials? I'm not sure of such inner workings of CRM, but I doubt you need to send credentials of the user being created. You do need the current user data, of course, but that's the admin account - it doesn't change, and all other CRM operations are performed without a problem.

    Thursday, January 2, 2014 9:04 AM
  • Hi,

    I think I'm using the wrong terminology. The credentials cache isn't what needs refreshing, it's rather the connection to the CRM, but since I didn't quite solve that problem and it was a non vital part of that system we didn't follow through on the troubleshooting.

    When you connect to the CRM you set up the IOrganizationService where you identify the user you want to impersonate. This connection has a security token (or something similar) that has a life span of roughly 34 minutes if I remember correctly and this needs refreshing. I don't know if this is your problem of course but it might be worth checking out.

    Sorry for the confusion


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    Thursday, January 2, 2014 9:20 AM
  • Ahh, I see. Well, in this case, IISRESET (that is - restarting both our application and the CRM) re-establishes the Organization Service, and thus the cashe is current. I'll look into the code which creates it, to make sure a new connection is created on demand when needed (and not kept in memory), but given how IISRESET works I doubt this is the cause of this particular problem.

    Not to mention that if this is a credential issue, then OTHER operations would likely also fail in a similar manner, but this is not the case. I've only got trouble creating users and only "every once in a while" at that. :/

    Thursday, January 2, 2014 9:25 AM
  • One possibility is an Active Directory problem, such as if a domain controller is not always accessible, or maybe DNS problems. When creating a user, CRM will communicate with an AD domain controller. Although the 'The system cannot find the file specified' error message refers to a file, rather than a server, I could imagine this error being thrown in this scenario.


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Friday, January 3, 2014 12:06 PM
    Moderator
  • One possibility is an Active Directory problem, such as if a domain controller is not always accessible, or maybe DNS problems. When creating a user, CRM will communicate with an AD domain controller. Although the 'The system cannot find the file specified' error message refers to a file, rather than a server, I could imagine this error being thrown in this scenario.


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Also unlikely.

    While my code fails, manually creating a new user in CRM works without errors, so it's not an AD problem.

    It's just that my code will fail to function at weird moments and refuse to work for a number of minutes...


    var parentBu = ctx.BusinessUnitSet.Where(bu => bu.ParentBusinessUnitId == null).Single(); //find top-level BU             var crmUser = ctx.SystemUserSet.Where(su => su.DomainName == user.DomainName).SingleOrDefault();             if (crmUser == null)             {                 crmUser = newSystemUser()                 {                     BusinessUnitId = parentBu.ToEntityReference(),                     AccessMode = newOptionSetValue((int)SystemUser.accessmodeValues.ReadWrite),                     CALType = newOptionSetValue((int)SystemUser.caltypeValues.Full),                     DomainName = user.DomainName,                     FirstName = user.Name,                     LastName = companyName                 };                 try                 { //running via Execute to get the record ID - I know I can generate the ID manually before creating the user, which I might change to do at one point                     crmUser.Id = (ctx.Execute(new Microsoft.Xrm.Sdk.Messages.CreateRequest() { Target = crmUser }) as Microsoft.Xrm.Sdk.Messages.CreateResponse).id;                 }                 catch (Exception ex)                 {                     throw; //don't know what to do with this error yet. Catch is so I can inspect the Exception only!                 }

    This is the code I'm using. It's simple enough and shouldn't fail with such strange errors. CTX is an OrganizationServiceContext generated with CRMSVCUTIL.




    Friday, January 3, 2014 12:24 PM
  • As David points out, the error message "file not found" is probably not correct, it's unfortunately not the first time CRM gives weird error messages. It's very hard to troubleshoot this type of intermittent errors. I would turn on CRM tracing for a couple of days if it doesn't slow down operation too much and perhaps insert logging lines in the code that writes out all the variables that are fetched from the CRM and exception data when the error occurs.

    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    Friday, January 3, 2014 12:51 PM
  • Hi, Thanks for sharing the code. Can you check out and trace the value of the Business Unit in your linq expression. "var parentBu = ctx.BusinessUnitSet.Where(bu => bu.ParentBusinessUnitId == null).Single(); //find top-level BU" Single or First, you may check it out. Let me know your progress. Thanks & Regards,

    Anubhav Bajpai

    Friday, January 3, 2014 1:05 PM
  • Hi, Thanks for sharing the code. Can you check out and trace the value of the Business Unit in your linq expression. "var parentBu = ctx.BusinessUnitSet.Where(bu => bu.ParentBusinessUnitId == null).Single(); //find top-level BU" Single or First, you may check it out. Let me know your progress. Thanks & Regards,

    Anubhav Bajpai


    It finds the correct BU, if that's what you're asking. If it didn't (null value) then "ToEntityReference()" would fail. Or should I be looking for a different (not null but wrong BU?) error?
    Friday, January 3, 2014 1:47 PM
  • I found this blog post with a similar error. Not sure if the resolution makes sense but it is worth a look.

    If their error message and resolution is of any indication:

    at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)
    Inner Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
    at Microsoft.Crm.ObjectModel.UserSettingsServiceInternal`1.PopulateOrgDefaults(IBusinessEntity entity, ExecutionContext context)

    It would mean your problem is related to AD:

    at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)
    Inner Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
    at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args)



    Ronald



    • Edited by Ronald Liu Friday, January 3, 2014 6:09 PM
    Friday, January 3, 2014 5:43 PM