Skip to content

Commit d81304d

Browse files
committed
reviews, collectionresult, stream check
1 parent 1416541 commit d81304d

File tree

20 files changed

+103
-176
lines changed

20 files changed

+103
-176
lines changed

src/NetCoreStack.Proxy/Extensions/HttpResponseExtensions.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/NetCoreStack.Proxy/Extensions/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public static void AddNetCoreProxy(this IServiceCollection services,
5252
var genericRegistry = registryDelegate.MakeGenericMethod(type);
5353
genericRegistry.Invoke(null, new object[] { services });
5454
}
55+
5556
services.AddSingleton(proxyBuilderOptions);
5657
}
5758

src/NetCoreStack.Proxy/Extensions/TypeCoreExtensions.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using NetCoreStack.Common;
22
using System;
3-
using System.Linq;
43
using System.Reflection;
54
using System.Threading.Tasks;
65

@@ -36,25 +35,6 @@ internal static bool IsGenericTask(this Type type)
3635
return false;
3736
}
3837

39-
internal static Type GetTaskType(this Type type)
40-
{
41-
if (type.IsGenericType() && type.GetGenericTypeDefinition() == typeof(Task<>))
42-
return type.GetGenericArguments()[0];
43-
44-
throw new Exception("Type " + type.FullName + " is not an instance of Task<T>");
45-
}
46-
47-
internal static PropertyInfo GetProperty(this MethodInfo method)
48-
{
49-
var hasReturn = method.ReturnType != typeof(void);
50-
Func<PropertyInfo, bool> predicate = prop => prop.GetSetMethod() == method;
51-
52-
if (hasReturn)
53-
predicate = prop => prop.GetGetMethod() == method;
54-
55-
return method.DeclaringType.GetProperties().FirstOrDefault(predicate);
56-
}
57-
5838
internal static bool IsDirectStreamTransport(this Type returnType)
5939
{
6040
var result = false;
@@ -64,11 +44,6 @@ internal static bool IsDirectStreamTransport(this Type returnType)
6444
result = true;
6545
}
6646

67-
if (returnType.IsGenericType())
68-
{
69-
70-
}
71-
7247
return result;
7348
}
7449
}

src/NetCoreStack.Proxy/HttpDispatchProxy.cs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,6 @@ protected object GetProxy()
4949
return this;
5050
}
5151

52-
protected void DirectStreamTransport(ResponseContext context)
53-
{
54-
_proxyManager.HttpContext.Response.ContentType = Constants.ContentTypeJsonWithEncoding;
55-
_proxyManager.HttpContext.Response.Headers.Add(Constants.MetadataInnerHeader, context.MetadataInner);
56-
_proxyManager.HttpContext.Response.Headers.Add(Constants.MetadataHeader, context.Metadata);
57-
58-
if (!context.ResultContent.HasValue())
59-
{
60-
throw new ArgumentNullException(nameof(context.ResultContent));
61-
}
62-
63-
_proxyManager.HttpContext.Items.Add(Constants.CollectionResultItemKey, context.ResultContent);
64-
}
65-
6652
private async Task<ResponseContext> InternalInvokeAsync(MethodInfo targetMethod, object[] args, Type genericReturnType = null)
6753
{
6854
var requestContext = new RequestContext(targetMethod,
@@ -99,11 +85,6 @@ private async Task<ResponseContext> InternalInvokeAsync(MethodInfo targetMethod,
9985
throw new ProxyException(result, new HttpRequestException());
10086
}
10187

102-
if (descriptor.MethodDescriptor.IsDirectStreamTransport)
103-
{
104-
DirectStreamTransport(responseContext);
105-
}
106-
10788
return responseContext;
10889
}
10990

@@ -126,15 +107,7 @@ public override async Task<T> InvokeAsyncT<T>(MethodInfo method, object[] args)
126107

127108
var responseContext = await InternalInvokeAsync(method, args, typeof(T));
128109
var methodDescriptor = responseContext.RequestDescriptor.MethodDescriptor;
129-
if (methodDescriptor.IsDirectStreamTransport)
130-
{
131-
// Dummy null return, Performance improvements for CollectionResult type.
132-
return default(T);
133-
}
134-
else
135-
{
136-
return (T)responseContext.Value;
137-
}
110+
return (T)responseContext.Value;
138111
}
139112

140113
public override object Invoke(MethodInfo method, object[] args)

src/NetCoreStack.Proxy/Internal/Constants.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22
{
33
public class Constants
44
{
5-
public static string Prefix = "NetCore";
6-
5+
public static string Prefix = "NetCoreStack";
76
public const string ProxySettings = "ProxySettings";
87
public const string ContentTypeJsonWithEncoding = "application/json; charset=utf-8";
9-
public const string CollectionResultItemKey = "CollectionResultItemKey";
10-
118
public readonly static string ClientUserAgentHeader = "User-Agent";
12-
public readonly static string MetadataHeader = $"X-{Prefix}-Metadata";
13-
public readonly static string MetadataInnerHeader = $"X-{Prefix}-MetadataInner";
149
}
1510
}

src/NetCoreStack.Proxy/Internal/ProxyResultExecutor.cs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using NetCoreStack.Common;
2-
using NetCoreStack.Proxy.Extensions;
1+
using NetCoreStack.Proxy.Extensions;
32
using Newtonsoft.Json;
43
using System;
54
using System.Net.Http;
@@ -13,39 +12,20 @@ public static async Task<ResponseContext> ExecuteAsync(HttpResponseMessage respo
1312
RequestDescriptor descriptor,
1413
Type genericReturnType = null)
1514
{
16-
string metadata = response.GetMetadataHeader();
17-
string metadataInner = response.GetMetadataInnerHeader();
18-
var context = new ResponseContext(response, descriptor, metadata, metadataInner);
15+
var context = new ResponseContext(response, descriptor);
1916
var methodDescriptor = descriptor.MethodDescriptor;
2017

2118
if (response == null)
2219
return context;
2320

24-
if (typeof(ProxyException).FullName != metadata && methodDescriptor.IsCollectionResult)
25-
{
26-
if (string.IsNullOrWhiteSpace(metadataInner))
27-
throw new InvalidOperationException($"{nameof(CollectionResult)} should have MetaDataInnerType info on header!");
28-
29-
context.ResultContent = await response.Content.ReadAsStringAsync();
30-
31-
// Direct stream transports
32-
if (methodDescriptor.IsDirectStreamTransport)
33-
{
34-
context.Value = TaskHelper.CreateFromResult(methodDescriptor.ReturnType);
35-
return context;
36-
}
37-
38-
return context;
39-
}
40-
41-
context.ResultContent = await response.Content.ReadAsStringAsync();
42-
4321
if ((int)response.StatusCode >= 500)
4422
{
4523
throw new ProxyException("Proxy call result content is can not be null, " +
46-
"The exception may have occurred on the side of the API or Server", null);
24+
"The exception may have occurred on the Server", null);
4725
}
4826

27+
context.ResultContent = await response.Content.ReadAsStringAsync();
28+
4929
if (methodDescriptor.IsVoidReturn)
5030
return context;
5131

src/NetCoreStack.Proxy/NetCoreStack.Proxy.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
<ItemGroup>
1313
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="1.1.1" />
14-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="1.1.1" />
1514
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.1" />
1615
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
1716
<PackageReference Include="NetCoreStack.Common" Version="1.0.0-*" />

src/NetCoreStack.Proxy/ProxyMethodDescriptor.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using Microsoft.AspNetCore.Mvc;
22
using Microsoft.AspNetCore.Mvc.Abstractions;
3-
using Microsoft.AspNetCore.Mvc.Controllers;
4-
using NetCoreStack.Common;
53
using NetCoreStack.Proxy.Extensions;
64
using System;
75
using System.Collections.Generic;
@@ -25,33 +23,11 @@ public class ProxyMethodDescriptor
2523

2624
public List<ParameterDescriptor> Parameters { get; set; }
2725

28-
public bool IsDirectStreamTransport
29-
{
30-
get
31-
{
32-
return ReturnType.IsDirectStreamTransport();
33-
}
34-
}
35-
3626
public bool IsVoidReturn { get; }
3727
public bool IsTaskReturn { get; }
38-
3928
public bool IsGenericTaskReturn { get; }
4029
public bool IsActionResult { get; }
4130

42-
public bool IsCollectionResult
43-
{
44-
get
45-
{
46-
if (ReturnType.IsAssignableFrom(typeof(CollectionResult)))
47-
{
48-
return true;
49-
}
50-
51-
return false;
52-
}
53-
}
54-
5531
public ProxyMethodDescriptor(MethodInfo methodInfo)
5632
{
5733
MethodInfo = methodInfo;

src/NetCoreStack.Proxy/ResponseContext.cs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,16 @@ namespace NetCoreStack.Proxy
77
public class ResponseContext
88
{
99
public RequestDescriptor RequestDescriptor { get; }
10-
public string Metadata { get; set; }
11-
public string MetadataInner { get; set; }
1210
public string ResultContent { get; set; }
1311
public HttpResponseMessage Response { get; set; }
1412
public object Value { get; set; }
1513

1614
public ResponseContext(HttpResponseMessage response,
1715
RequestDescriptor requestDescriptor,
18-
string metadata,
19-
string metadataInner,
2016
Type genericReturnType = null)
2117
{
22-
23-
if (response == null)
24-
{
25-
throw new ArgumentNullException(nameof(response));
26-
}
27-
28-
if (requestDescriptor == null)
29-
{
30-
throw new ArgumentNullException(nameof(requestDescriptor));
31-
}
32-
33-
Response = response;
34-
RequestDescriptor = requestDescriptor;
35-
Metadata = metadata;
36-
MetadataInner = metadataInner;
18+
Response = response ?? throw new ArgumentNullException(nameof(response));
19+
RequestDescriptor = requestDescriptor ?? throw new ArgumentNullException(nameof(requestDescriptor));
3720
}
3821
}
3922
}

test/NetCoreStack.Proxy.ServerApp/Controllers/GuidelineController.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.AspNetCore.Mvc;
22
using Microsoft.Extensions.Logging;
3+
using NetCoreStack.Common;
34
using NetCoreStack.Proxy.Test.Contracts;
45
using Newtonsoft.Json;
56
using System;
@@ -33,6 +34,51 @@ public async Task<IEnumerable<Post>> GetPostsAsync()
3334
return items;
3435
}
3536

37+
/// <summary>
38+
/// CollectionResult Direct Stream Transport - Return the client without Deserialization
39+
/// </summary>
40+
/// <returns></returns>
41+
[HttpGet(nameof(GetCollectionStream))]
42+
public async Task<CollectionResult<Post>> GetCollectionStream()
43+
{
44+
var httpRequest = new HttpRequestMessage(HttpMethod.Get, new Uri("https://jsonplaceholder.typicode.com/posts"));
45+
var response = await Factory.Client.SendAsync(httpRequest);
46+
var content = await response.Content.ReadAsStringAsync();
47+
var items = JsonConvert.DeserializeObject<List<Post>>(content);
48+
49+
50+
var count = items.Count;
51+
Logger.LogDebug($"{nameof(GetPostsAsync)}, PostsCount:{items.Count}");
52+
return new CollectionResult<Post>
53+
{
54+
Data = items,
55+
Draw = 1,
56+
TotalRecords = count,
57+
TotalRecordsFiltered = count
58+
};
59+
}
60+
61+
[HttpGet(nameof(GetCollectionStreams))]
62+
public IEnumerable<CollectionResult<Post>> GetCollectionStreams()
63+
{
64+
var httpRequest = new HttpRequestMessage(HttpMethod.Get, new Uri("https://jsonplaceholder.typicode.com/posts"));
65+
var response = Factory.Client.SendAsync(httpRequest).Result;
66+
var content = response.Content.ReadAsStringAsync().Result;
67+
var items = JsonConvert.DeserializeObject<List<Post>>(content);
68+
var count = items.Count;
69+
Logger.LogDebug($"{nameof(GetPostsAsync)}, PostsCount:{items.Count}");
70+
return new List<CollectionResult<Post>>
71+
{
72+
new CollectionResult<Post>
73+
{
74+
Data = items,
75+
Draw = 1,
76+
TotalRecords = count,
77+
TotalRecordsFiltered = count
78+
}
79+
};
80+
}
81+
3682
[HttpGet(nameof(GetWithReferenceType))]
3783
public async Task GetWithReferenceType([FromQuery]SimpleModel model)
3884
{

0 commit comments

Comments
 (0)