Skip to content

Commit 14f9dfe

Browse files
Fix static variables not being initialized in derived class HttpWebRequest (#88)
1 parent e184fea commit 14f9dfe

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

source/nanoFramework.System.Net.Http/Http/System.Net.HttpWebRequest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ static private void CheckPersistentConnections(object unused)
112112
/// </summary>
113113
static HttpWebRequest()
114114
{
115+
// The static constructor of the base class does not get called before the derived static constructor
116+
// so you have to make sure the base class gets initialized in any case if you want to use "RegisterPrefix"
117+
Initialize();
118+
115119
// Creates instance of HttpRequestCreator. HttpRequestCreator creates HttpWebRequest
116120
HttpRequestCreator Creator = new HttpRequestCreator();
117121
// Register prefix. HttpWebRequest handles both http and https

source/nanoFramework.System.Net.Http/Http/System.Net.WebRequest.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public abstract class WebRequest : MarshalByRefObject, IDisposable
2525
// (ASP .NET is 90 seconds)
2626

2727
// Lock to syncronize update of s_PrefixList
28-
private static object g_listLock = new object();
28+
private static object g_listLock;
2929
// List of WebRequestPrefixElement that keeps prefix ( string ) and
3030
// IWebRequestCreate
31-
private static ArrayList s_PrefixList = new ArrayList();
31+
private static ArrayList s_PrefixList;
3232

3333
private static IWebProxy s_defaultProxy = null;
3434

@@ -38,7 +38,27 @@ public abstract class WebRequest : MarshalByRefObject, IDisposable
3838
/// </summary>
3939
protected WebRequest()
4040
{
41+
}
42+
43+
/// <summary>
44+
/// Static constructor to initialize the static variables before the class is used
45+
/// </summary>
46+
static WebRequest()
47+
{
48+
Initialize();
49+
}
4150

51+
/// <summary>
52+
/// Initialize has to be called before the class is used.
53+
/// Normally should be called by the constructor above.
54+
/// </summary>
55+
public static void Initialize()
56+
{
57+
if (g_listLock == null)
58+
{
59+
g_listLock = new object();
60+
s_PrefixList = new ArrayList();
61+
}
4262
}
4363

4464
~WebRequest()

0 commit comments

Comments
 (0)