forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPclExport.Sl5.cs
More file actions
183 lines (154 loc) · 5.09 KB
/
PclExport.Sl5.cs
File metadata and controls
183 lines (154 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//Copyright (c) Service Stack LLC. All Rights Reserved.
//License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
#if SL5
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace ServiceStack
{
public class Sl5PclExport : PclExport
{
public static Sl5PclExport Provider = new Sl5PclExport();
public Sl5PclExport()
{
this.PlatformName = Platforms.Silverlight5;
}
public static PclExport Configure()
{
Configure(Provider);
return Provider;
}
public override string ReadAllText(string filePath)
{
return File.ReadAllText(filePath);
}
public override Assembly LoadAssembly(string assemblyPath)
{
var sri = System.Windows.Application.GetResourceStream(new Uri(assemblyPath, UriKind.Relative));
var myPart = new System.Windows.AssemblyPart();
var assembly = myPart.Load(sri.Stream);
return assembly;
}
public override Assembly[] GetAllAssemblies()
{
//TODO: Workout how to fix broken CoreCLR SL5 build that uses dynamic
#if !(SL5 && CORECLR) && !NO_DYNAMIC
return ((dynamic)AppDomain.CurrentDomain).GetAssemblies() as Assembly[];
#else
return new Assembly[0];
#endif
}
public override Type GetGenericCollectionType(Type type)
{
return type.GetInterfaces()
.FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ICollection<>));
}
public override void Config(HttpWebRequest req,
bool? allowAutoRedirect = null,
TimeSpan? timeout = null,
TimeSpan? readWriteTimeout = null,
string userAgent = null,
bool? preAuthenticate = null)
{
//throws NotImplementedException in both BrowserHttp + ClientHttp
//if (allowAutoRedirect.HasValue) req.AllowAutoRedirect = allowAutoRedirect.Value;
//if (userAgent != null) req.UserAgent = userAgent;
//Methods others than GET and POST are only supported by Client request creator, see
//http://msdn.microsoft.com/en-us/library/cc838250(v=vs.95).aspx
if (!IsBrowserHttp(req)) return;
if (req.Method != "GET" && req.Method != "POST")
{
req.Headers[HttpHeaders.XHttpMethodOverride] = req.Method;
req.Method = "POST";
}
}
public override HttpWebRequest CreateWebRequest(string requestUri, bool? emulateHttpViaPost = null)
{
var creator = emulateHttpViaPost.GetValueOrDefault()
? System.Net.Browser.WebRequestCreator.BrowserHttp
: System.Net.Browser.WebRequestCreator.ClientHttp;
return (HttpWebRequest)creator.Create(new Uri(requestUri));
}
private static bool IsBrowserHttp(WebRequest req)
{
return req.GetType().Name == "BrowserHttpWebRequest";
}
public override WebResponse GetResponse(WebRequest webRequest)
{
var task = webRequest.GetResponseAsync();
task.Wait();
var webRes = task.Result;
return webRes;
}
}
// Stopwatch shim for Silverlight
public sealed class Stopwatch
{
private long startTick;
private long elapsed;
private bool isRunning;
public static Stopwatch StartNew()
{
Stopwatch sw = new Stopwatch();
sw.Start();
return sw;
}
public Stopwatch() {}
public void Reset()
{
elapsed = 0;
isRunning = false;
startTick = 0;
}
public void Start()
{
if (!isRunning)
{
startTick = GetCurrentTicks();
isRunning = true;
}
}
public void Stop()
{
if (isRunning)
{
elapsed += GetCurrentTicks() - startTick;
isRunning = false;
}
}
public bool IsRunning
{
get { return isRunning; }
}
public TimeSpan Elapsed
{
get { return TimeSpan.FromMilliseconds(ElapsedMilliseconds); }
}
public long ElapsedMilliseconds
{
get { return GetCurrentElapsedTicks() / TimeSpan.TicksPerMillisecond; }
}
public long ElapsedTicks
{
get { return GetCurrentElapsedTicks(); }
}
private long GetCurrentElapsedTicks()
{
return (long) (this.elapsed + (IsRunning ? (GetCurrentTicks() - startTick) : 0));
}
private long GetCurrentTicks()
{
return Environment.TickCount * TimeSpan.TicksPerMillisecond;
}
public static long GetTimestamp()
{
return DateTime.UtcNow.Ticks;
}
}
}
#endif