Async and a single instance class working with Models

user3362714

I am using a WebApi delegating handler to forward requests to another server.

protected override async System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
    string requestContent = string.Empty;
    UriBuilder forwardUri = new UriBuilder(request.RequestUri);
    //remove the proxy port and replace with an Http port
    forwardUri.Port = 1900;
    //send it on to the requested URL
    request.RequestUri = forwardUri.Uri;
    HttpClient client = new HttpClient();
    if (request.Method == HttpMethod.Get)
    {
        request.Content = null;
    }
    try
    {
        var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
        if (response.IsSuccessStatusCode)
        {
            ManageOnlineRequests(response);
        }
        return response;
    }
    catch (Exception ex)
    {
        return ManageOfflineRequests(request);
    }
}

Everything works fine, but I am pseudo caching some data on the ManageOnlineRequests. For that purpose I am using a single instance class that contains a representation of the models I want to keep, so that I can mock the response and return those when in offline mode ManageOfflineRequests.

The question I have is : Since this is a Async method that contains a await inside, is there any potential issues accessing the lists I have inside singleton class called by the ManageOnlineRequests and ManageofflineRequests?

Thanks

i3arnon

As Yuval said, this is unrelated to async-await. If you plan on using these lists concurrently you need to make sure they are thread-safe.

Now, the simplest option is to use a lock, however if you are using List<T> specifically it's guaranteed to be thread-safe as long as you are only reading from it and not updating it concurrently:

It is safe to perform multiple read operations on a List, but issues can occur if the collection is modified while it’s being read

From List Class

A better solution would be to use the ImmutableList<T> out of Microsoft.Bcl.Immutable which is guaranteed to be thread-safe since it's immutable.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Variable in single instance class is not changing

From Dev

Django models class attribute and instance property?

From Dev

Observable with Async Pipe in template is not working for single value

From Dev

PDO fetch single class not working

From Dev

PDO fetch single class not working

From Dev

How to delegate class into an Async Task instance?

From Dev

Slim Framework -Single class instance for all routes

From Dev

Qt : Having a single instance of QPainter object in a class

From Dev

Static (single instance) Array For a Class C++

From Dev

How to access a django models ordering from model instance (or class)

From Dev

use wrapper class to initialize mutiple models for single view and LINQ Query

From Dev

Django : Migration of polymorphic models back to a single base class

From Dev

Boost async_write callback as a member function of another class instance

From Dev

How to fold elements without duplicates into single case class instance?

From Dev

Kotlin- Single instance of a non-singleton class?

From Dev

WPF accesses only single instance of derived class with dependency property

From Dev

Is a class instance that updates only a single var thread safe in Scala?

From Dev

Single instance of a MySQL database connection via PHP Class

From Dev

Single instance(singleton) of a class in a java application deployed in many nodes

From Dev

Javascript: Need to remove a single instance of a class on the page when the id is generated

From Dev

How to create single instance of a class by two different java processes

From Dev

Calling a member function in a single instance of a class that is referenced by an array of classes

From Dev

Targeting Child instance of single class using jQuery mouseover

From Dev

Single method to return an instance of different generic type of a class

From Dev

How to use single object instance for class to set the multiple member value?

From Dev

How to create single instance of class and use it multiple times?

From Dev

Async not working on controller's abstract super class method

From Dev

list remove function on class instance in a list not working properly

From Dev

I am having issues with resetting a single instance of a jquery countdown from an instance of a button of class reset

Related Related

  1. 1

    Variable in single instance class is not changing

  2. 2

    Django models class attribute and instance property?

  3. 3

    Observable with Async Pipe in template is not working for single value

  4. 4

    PDO fetch single class not working

  5. 5

    PDO fetch single class not working

  6. 6

    How to delegate class into an Async Task instance?

  7. 7

    Slim Framework -Single class instance for all routes

  8. 8

    Qt : Having a single instance of QPainter object in a class

  9. 9

    Static (single instance) Array For a Class C++

  10. 10

    How to access a django models ordering from model instance (or class)

  11. 11

    use wrapper class to initialize mutiple models for single view and LINQ Query

  12. 12

    Django : Migration of polymorphic models back to a single base class

  13. 13

    Boost async_write callback as a member function of another class instance

  14. 14

    How to fold elements without duplicates into single case class instance?

  15. 15

    Kotlin- Single instance of a non-singleton class?

  16. 16

    WPF accesses only single instance of derived class with dependency property

  17. 17

    Is a class instance that updates only a single var thread safe in Scala?

  18. 18

    Single instance of a MySQL database connection via PHP Class

  19. 19

    Single instance(singleton) of a class in a java application deployed in many nodes

  20. 20

    Javascript: Need to remove a single instance of a class on the page when the id is generated

  21. 21

    How to create single instance of a class by two different java processes

  22. 22

    Calling a member function in a single instance of a class that is referenced by an array of classes

  23. 23

    Targeting Child instance of single class using jQuery mouseover

  24. 24

    Single method to return an instance of different generic type of a class

  25. 25

    How to use single object instance for class to set the multiple member value?

  26. 26

    How to create single instance of class and use it multiple times?

  27. 27

    Async not working on controller's abstract super class method

  28. 28

    list remove function on class instance in a list not working properly

  29. 29

    I am having issues with resetting a single instance of a jquery countdown from an instance of a button of class reset

HotTag

Archive