Daniel Gustafsson
Daniel Gustafsson

Reputation: 1817

Prevent API method to be called more then once at a time

I have an image upload function where i am able to drag and drop a lot of images at the same time to upload the images. This works fine but the problem is that the images when it is upploaded is supposed to get a specific id if it is the first one to get uploaded.

But as it is now i'm doing a check if there is any other uploaded and then if there isn't i upload the image. The problem here is that the first image hasn't been saved before the next one does the check if it is the first. So it will end up uploading all the images with the id that only the first should have.

Is there a possibility to make the repository method so that it only can get called one and have to save before the next call can reach the method?

EDIT:

    foreach (string fileName in request.Files)
            {
                var companyName = request.Form.Get(0);
                var productId = request.Form.Get(1);
                if (string.IsNullOrWhiteSpace(companyName))
                {
                    throw new Exception("No company found!");
                }
                if (string.IsNullOrWhiteSpace(productId))
                {
                    throw new Exception("No product picked!");
                }
                HttpPostedFileBase file = request.Files[fileName];
                if (file != null && file.ContentLength > 0)
                {



                    //This is the method that calls the API
                    SaveUploadResource(file, companyName, productId);
                }
            }

This is the repository method:

                var productImage = pamContext.ProductResources.Where(w => w.ProductId == productId && w.CompanyName == companyName && w.LanguageCode == "en" && w.ResourceTypeId == "I");
                if (!productImage.Any())
                {
                    productResource.ResourceTypeId = "I";
                }
                pamContext.ProductResources.Add(productResource);
                pamContext.SaveChanges();

Upvotes: 0

Views: 1147

Answers (1)

Yetiish
Yetiish

Reputation: 703

Perhaps you could use Lock statement

    private Object myLock= new Object();

    foreach (string fileName in request.Files)
            {
                var companyName = request.Form.Get(0);
                var productId = request.Form.Get(1);
                if (string.IsNullOrWhiteSpace(companyName))
                {
                    throw new Exception("No company found!");
                }
                if (string.IsNullOrWhiteSpace(productId))
                {
                    throw new Exception("No product picked!");
                }
                HttpPostedFileBase file = request.Files[fileName];
                if (file != null && file.ContentLength > 0)
                {

                    lock (myLock)
                    {
                        //This is the method that calls the API
                        SaveUploadResource(file, companyName, productId);
                    }
                }
            }

The lock will essentially pause any sequential executions until the preceding one has completed. Take care to ensure your method does not take too long to run relative to the number of calls being made, or response times could stack up and become very slow.

Upvotes: 1

Related Questions