Skip to content

Commit 2c2feab

Browse files
committed
Remove using NameValueCollectionWrapper for .NET Core QueryString
This change is performace improvement.
1 parent b53b9ec commit 2c2feab

5 files changed

Lines changed: 107 additions & 11 deletions

File tree

src/ServiceStack/Host/NetCore/NetCoreRequest.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.AspNetCore.Http.Internal;
1616
using Microsoft.Extensions.DependencyInjection;
1717
using ServiceStack.Configuration;
18+
using ServiceStack.NetCore;
1819

1920
namespace ServiceStack.Host.NetCore
2021
{
@@ -175,13 +176,7 @@ public INameValueCollection QueryString
175176
if (queryString != null)
176177
return queryString;
177178

178-
var nvc = new NameValueCollection();
179-
foreach (var query in request.Query)
180-
{
181-
foreach (var value in query.Value)
182-
nvc.Add(query.Key, value);
183-
}
184-
return queryString = new NameValueCollectionWrapper(nvc);
179+
return queryString = new NetCoreQueryStringCollection(request.Query);
185180
}
186181
}
187182

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#if NETSTANDARD1_6
2+
3+
using System;
4+
using System.Collections;
5+
using System.Linq;
6+
using Microsoft.AspNetCore.Http;
7+
using ServiceStack.Web;
8+
9+
namespace ServiceStack.NetCore
10+
{
11+
public class NetCoreQueryStringCollection : INameValueCollection
12+
{
13+
IQueryCollection originalQuery;
14+
15+
public NetCoreQueryStringCollection(IQueryCollection originalQuery)
16+
{
17+
this.originalQuery = originalQuery;
18+
}
19+
20+
#region ICollection implementation
21+
public int Count => originalQuery.Count;
22+
23+
public bool IsSynchronized => false;
24+
25+
public object SyncRoot => originalQuery;
26+
27+
public void CopyTo(Array array, int index)
28+
{
29+
throw new NotImplementedException();
30+
}
31+
32+
public IEnumerator GetEnumerator()
33+
{
34+
return originalQuery.GetEnumerator();
35+
}
36+
#endregion
37+
38+
public object Original => originalQuery;
39+
40+
public string this[int index] => Get(index);
41+
42+
public string this[string name]
43+
{
44+
get { return originalQuery[name]; }
45+
46+
set { throw new NotSupportedException(); }
47+
}
48+
49+
public string[] AllKeys => originalQuery.Keys.ToArray();
50+
51+
public void Add(string name, string value)
52+
{
53+
throw new NotSupportedException();
54+
}
55+
56+
public void Clear()
57+
{
58+
throw new NotSupportedException();
59+
}
60+
61+
public string Get(int index)
62+
{
63+
return Get(GetKey(index));
64+
}
65+
66+
public string Get(string name)
67+
{
68+
return originalQuery[name];
69+
}
70+
71+
public string GetKey(int index)
72+
{
73+
return AllKeys[index];
74+
}
75+
76+
public string[] GetValues(string name)
77+
{
78+
return originalQuery[name];
79+
}
80+
81+
public bool HasKeys()
82+
{
83+
return originalQuery.Count > 0;
84+
}
85+
86+
public void Remove(string name)
87+
{
88+
throw new NotSupportedException();
89+
}
90+
91+
public void Set(string key, string value)
92+
{
93+
throw new NotSupportedException();
94+
}
95+
96+
public override string ToString()
97+
{
98+
return String.Join("&", originalQuery.Select(pair => pair.Key + "=" + pair.Value.ToString()));
99+
}
100+
}
101+
}
102+
103+
#endif

src/ServiceStack/ServiceStack.Signed.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
<Compile Include="NativeTypes\Kotlin\KotlinGenerator.cs" />
136136
<Compile Include="NetCore\NetCoreContainerAdapter.cs" />
137137
<Compile Include="NetCore\NetCoreLogFactory.cs" />
138+
<Compile Include="NetCore\NetCoreQueryStringCollection.cs" />
138139
<Compile Include="Platform.cs" />
139140
<Compile Include="Platforms\PlatformNet.Wcf.cs" />
140141
<Compile Include="Platforms\PlatformNet.Config.cs" />

src/ServiceStack/ServiceStack.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
<Compile Include="NativeTypes\Kotlin\KotlinGenerator.cs" />
130130
<Compile Include="NetCore\NetCoreContainerAdapter.cs" />
131131
<Compile Include="NetCore\NetCoreLogFactory.cs" />
132+
<Compile Include="NetCore\NetCoreQueryStringCollection.cs" />
132133
<Compile Include="Platform.cs" />
133134
<Compile Include="Platforms\PlatformNet.Wcf.cs" />
134135
<Compile Include="Platforms\PlatformNet.Config.cs" />

tests/ServiceStack.WebHost.Endpoints.Tests/CacheServerFeatureTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,11 +426,7 @@ public object Any(ShortCircuitImpl request)
426426

427427
public object Any(CachedRequest request)
428428
{
429-
#if NETCORE
430-
var cacheKey = String.Join("&", Request.QueryString.Cast<string>().Select(key => key + "=" + Request.QueryString[key]));
431-
#else
432429
var cacheKey = Request.QueryString.ToString();
433-
#endif
434430

435431
return Request.ToOptimizedResultUsingCache(Cache, cacheKey, () => request);
436432
}

0 commit comments

Comments
 (0)