Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8267061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:23:06+00:00 2026-06-08T05:23:06+00:00

Say I have the following code that inserts users into MS Dynamics in a

  • 0

Say I have the following code that inserts users into MS Dynamics in a MVC Application:

public bool CreateContact(string email)
{
    if (crm.contacts.Count(x => x.Email == email) > 0)
         return false; //Email already exist in the Crm. Skip

    var contact = new contact {Email = email};
    crm.AddTocontacts(contact);
    crm.SaveChanges();

    return true;
}

It works great for stopping users from registering with the same Email address, until recently when we are having MAJOR performance issues with Dynamics.

Apparently, users are getting a huge delay and often triple clicks the button that fires this piece of code.

Problem is, .Count() is firing at the same time in different Http Requests before .SaveChanges() finished in the first Request. As a result, we are seeing contacts with the same Email address.

While I already added a fix from the client-side, I would like to see if this can be done on the server side as well.

What would be a good strategy to make this thread-safe?


Edit:

While adding a constraint on the CRM is the best solution as many suggested here, I cannot implement that solution at the moment because duplicates already exist in the CRM long before this issue is discovered. Apparently there are more than one application that talks to the CRM.

With very little experience with locking and threading, I ended up doing the following:

internal static class ContactLock
{
    internal static readonly object Locker = new object();
}

public bool CreateContact(string email)
{
    lock(ContactLock.Locker)
    {
        if (crm.contacts.Any(x => x.Email == email))
            return false; //Email already exist in the Crm. Skip

        var contact = new contact {Email = email};
        crm.AddTocontacts(contact);
        crm.SaveChanges();

        return true;
    }
}

It passes my unit test and seems to be working without any issues.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:23:07+00:00Added an answer on June 8, 2026 at 5:23 am

    That kind of validation should typically be backed by a unique constraint in the underlying data store. If it is possible to create constraints in the CRM database, that’s where the fix should go.

    Your code snippet shows a typical place where some kind of locking is needed. The check (with Count()) and the SaveChanges() should be protected by a lock. I would suggest that you start with locking on an object that’s static – which means it will be a global lock preventing simultanous registrations. If that proves to be a problem, you could revise the locking strategy.

    Regarding the call taking a lot of time – that’s something you should address. Adding a unique constraint to the email column will force it to be indexed, which will probably improve performance a lot. If possible (again, I don’t know CRM) you should use linq’s Any() operator instead of Count() to check for existence. The former can break on the first hit, while the latter will have to continue scanning.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say that I have the following code: public class Shelter<A extends Animal, B
Let's say that I have the following code that's run in one thread of
Say that I have the following code: ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(myRunnable); Now, if
Say I have the following code: class Parent { static string MyField = ParentField;
Let's say I have following code cursor = connection.cursor() cursor.execute(query) after that point I
Say I have the following code snippet in c# static const bool DO_PERFORMANCE_CODE =
Say have the following code: public void SaveOrUpdate(OrderContract orderContract) { foreach (var orderContract in
Let's say we have following code: struct A{ virtual ~A(){} void f(){ p =
Say I have the following code: <span>Hello world!</span> And the following CSS: span{ color:red;
Say I have the following code: <div onclick='location.href=http://www.example.com/'> <a href='#' onclick='alert(blah)'>click</a> </div> Is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.