locked
Synchronize calls to WebRequest.Create() or not? RRS feed

  • Question

  • I am trying to build an application that does multiple file uploads. But I am confused about using the WebRequest.Create() method. It is a static method. So do I need to synchronize my calls to it?

    I am planning to write a whole class that takes care of the ftp uploads. And this class is to be utilized in a multi-threaded application. I don't know if I might run into any further synchronization problems...! I require some advice in this area.

    I am a bundle of mistakes intertwined together with good intentions
    Monday, December 22, 2008 6:15 AM

Answers

  • To clarify a little bit, instance methods *on a given instance* are generally not threadsafe with respect to each other.  For example, if I call WebRequest.GetResponse() on one thread and on a different thread change the ContentLength halfway through the upload, bad things might happen.  These kinds of things are in general very difficult to diagnose at runtime.  They may result in an exception being thrown, or simply "incorrect" behavior (like only uploading half your file because you changed to ContentLength in the middle of the upload :) ).

    Instance methods of *different* instances of the same class are almost certainly threadsafe with respect to each other - you can upload 100 different files using 100 different WebRequests on 100 different threads at the same time, and none of them should interfere with each other.

    -dave

    Thursday, January 22, 2009 2:33 AM
    Moderator
  • You may only do one upload at a time per WebRequest object.  You may, however, have as many WebRequest objects as you would like, and therefore as many uploads as you would like at a given time.

    -dave

    Monday, January 26, 2009 10:54 PM
    Moderator

All replies

  • WebRequest.Create is thread-safe, you don't have to sync calls to it. The returned WebRequest is not thread-safe, of course.

    In general, static members of the framework are thread-safe. If in doubt, look up the containing class and look for a paragraph called "Thread safety" near the end.

    Mark Rockmann
    Monday, December 29, 2008 2:00 PM
  • Mark Rockman said:

    The returned WebRequest is not thread-safe, of course.

    Why do you say so?
    EDIT: I mean how will one eventually find out?

    I am a bundle of mistakes intertwined together with good intentions
    Wednesday, December 31, 2008 7:36 AM
  • The majority of instance members of framework classes are not thread-safe, i.e. cannot safely be called concurrently. The documentation of the specific type/member should clarify usage and exceptions to this thread-safety rule.

    Mark Rockmann
    Wednesday, December 31, 2008 3:22 PM
  • I've done multiple file uploads with the ftp-System.net classes. Now if you say that they are not thread safe, how can I test to find out if uploads are happening correctly.

    I am a bundle of mistakes intertwined together with good intentions
    Thursday, January 1, 2009 6:54 AM
  • If you're getting an OK response, then it is uploading successfully

    Mariya
    Monday, January 19, 2009 4:16 PM
    Moderator
  • To clarify a little bit, instance methods *on a given instance* are generally not threadsafe with respect to each other.  For example, if I call WebRequest.GetResponse() on one thread and on a different thread change the ContentLength halfway through the upload, bad things might happen.  These kinds of things are in general very difficult to diagnose at runtime.  They may result in an exception being thrown, or simply "incorrect" behavior (like only uploading half your file because you changed to ContentLength in the middle of the upload :) ).

    Instance methods of *different* instances of the same class are almost certainly threadsafe with respect to each other - you can upload 100 different files using 100 different WebRequests on 100 different threads at the same time, and none of them should interfere with each other.

    -dave

    Thursday, January 22, 2009 2:33 AM
    Moderator
  • Does this mean that I have to do only one upload at a time?
    I am a bundle of mistakes intertwined together with good intentions
    Friday, January 23, 2009 10:12 AM
  • You may only do one upload at a time per WebRequest object.  You may, however, have as many WebRequest objects as you would like, and therefore as many uploads as you would like at a given time.

    -dave

    Monday, January 26, 2009 10:54 PM
    Moderator