Skip to content

Commit

Permalink
IAsyncEnumerable of .net core
Browse files Browse the repository at this point in the history
  • Loading branch information
zijianhuang committed Feb 14, 2020
1 parent 9c69173 commit 33918a1
Show file tree
Hide file tree
Showing 14 changed files with 436 additions and 310 deletions.
32 changes: 32 additions & 0 deletions DemoCoreWeb.ClientApi/WebApiClientAuto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,38 @@ public void Delete(long id)
responseMessage.EnsureSuccessStatusCode();
}

/// <summary>
/// GET api/Heroes/asyncHeroes
/// </summary>
public async Task<DemoWebApi.Controllers.Client.Hero[]> GetAsyncHeroesAsync()
{
var requestUri = new Uri(this.baseUri, "api/Heroes/asyncHeroes");
var responseMessage = await client.GetAsync(requestUri);
responseMessage.EnsureSuccessStatusCode();
var stream = await responseMessage.Content.ReadAsStreamAsync();
using (JsonReader jsonReader = new JsonTextReader(new System.IO.StreamReader(stream)))
{
var serializer = new JsonSerializer();
return serializer.Deserialize<DemoWebApi.Controllers.Client.Hero[]>(jsonReader);
}
}

/// <summary>
/// GET api/Heroes/asyncHeroes
/// </summary>
public DemoWebApi.Controllers.Client.Hero[] GetAsyncHeroes()
{
var requestUri = new Uri(this.baseUri, "api/Heroes/asyncHeroes");
var responseMessage = this.client.GetAsync(requestUri).Result;
responseMessage.EnsureSuccessStatusCode();
var stream = responseMessage.Content.ReadAsStreamAsync().Result;
using (JsonReader jsonReader = new JsonTextReader(new System.IO.StreamReader(stream)))
{
var serializer = new JsonSerializer();
return serializer.Deserialize<DemoWebApi.Controllers.Client.Hero[]>(jsonReader);
}
}

/// <summary>
/// Get a hero.
/// GET api/Heroes/{id}
Expand Down
11 changes: 11 additions & 0 deletions DemoCoreWeb/Controllers/HeroesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.AspNetCore.Mvc;
using System.Runtime.Serialization;
using System.Collections.Concurrent;
using System.Threading.Tasks;

namespace DemoWebApi.Controllers
{
Expand Down Expand Up @@ -93,6 +94,16 @@ public Hero[] Search(string name)
d.Name.Contains(name)).ToArray();
}

[HttpGet("asyncHeroes")]
public async IAsyncEnumerable<Hero> GetAsyncHeroes()
{
foreach (var item in HeroesData.Instance.Dic.Values)
{
await System.Threading.Tasks.Task.Delay(1);
yield return item;
}
}

///// <summary>
///// This should triger error: System.ArgumentException: Web API Heroes/GetSomethingInvalid is defined with invalid parameters: Not support ParameterBinder FromQuery or FromUri with a class parameter.
///// </summary>
Expand Down
93 changes: 49 additions & 44 deletions DemoCoreWeb/Scripts/ClientApi/HttpClient.js
Original file line number Diff line number Diff line change
@@ -1,98 +1,103 @@
/// <reference path="../typings/jquery/jquery.d.ts" />
class HttpClient {
var HttpClient = /** @class */ (function () {
function HttpClient() {
}
/**
**/
get(url, callback, errorCalback, statusCodeCallback) {
HttpClient.prototype.get = function (url, callback, errorCalback, statusCodeCallback) {
this.executeAjax(url, null, "GET", callback, errorCalback, statusCodeCallback);
}
post(url, dataToSave, callback, errorCalback, statusCodeCallback) {
};
HttpClient.prototype.post = function (url, dataToSave, callback, errorCalback, statusCodeCallback) {
this.executeAjax(url, dataToSave, "POST", callback, errorCalback, statusCodeCallback);
}
put(url, dataToSave, callback, errorCalback, statusCodeCallback) {
};
HttpClient.prototype.put = function (url, dataToSave, callback, errorCalback, statusCodeCallback) {
this.executeAjax(url, dataToSave, "PUT", callback, errorCalback, statusCodeCallback);
}
delete(url, callback, errorCalback, statusCodeCallback) {
};
HttpClient.prototype["delete"] = function (url, callback, errorCalback, statusCodeCallback) {
this.executeAjax(url, null, "DELETE", callback, errorCalback, statusCodeCallback);
}
executeAjax(url, dataToSave, httpVerb, callback, errorCallback, statusCodeCallback) {
};
HttpClient.prototype.executeAjax = function (url, dataToSave, httpVerb, callback, errorCallback, statusCodeCallback) {
//http://api.jquery.com/jquery.ajax/
$.ajax(url, {
data: JSON.stringify(dataToSave),
type: httpVerb,
success: (data, textStatus, jqXHR) => {
success: function (data, textStatus, jqXHR) {
if (callback !== null) {
callback(data);
}
},
error: (xhr, ajaxOptions, thrown) => {
error: function (xhr, ajaxOptions, thrown) {
if (errorCallback != null) {
errorCallback(xhr, ajaxOptions, thrown);
}
},
statusCode: statusCodeCallback,
contentType: 'application/json; charset=UTF-8',
headers: {
Accept: 'text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8',
Accept: 'text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8'
}
});
};
/**
location.origin may not be working in some releases of IE. And locationOrigin is an alternative implementation
**/
HttpClient.locationOrigin = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '') + '/';
return HttpClient;
}());
var AuthHttpClient = /** @class */ (function () {
function AuthHttpClient() {
}
}
/**
location.origin may not be working in some releases of IE. And locationOrigin is an alternative implementation
**/
HttpClient.locationOrigin = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '') + '/';
class AuthHttpClient {
/**
**/
get(url, callback, errorCalback, statusCodeCallback) {
AuthHttpClient.prototype.get = function (url, callback, errorCalback, statusCodeCallback) {
this.executeAjax(url, null, "GET", callback, errorCalback, statusCodeCallback);
}
post(url, dataToSave, callback, errorCalback, statusCodeCallback) {
};
AuthHttpClient.prototype.post = function (url, dataToSave, callback, errorCalback, statusCodeCallback) {
this.executeAjax(url, dataToSave, "POST", callback, errorCalback, statusCodeCallback);
}
put(url, dataToSave, callback, errorCalback, statusCodeCallback) {
};
AuthHttpClient.prototype.put = function (url, dataToSave, callback, errorCalback, statusCodeCallback) {
this.executeAjax(url, dataToSave, "PUT", callback, errorCalback, statusCodeCallback);
}
delete(url, callback, errorCalback, statusCodeCallback) {
};
AuthHttpClient.prototype["delete"] = function (url, callback, errorCalback, statusCodeCallback) {
this.executeAjax(url, null, "DELETE", callback, errorCalback, statusCodeCallback);
}
executeAjax(url, dataToSave, httpVerb, callback, errorCallback, statusCodeCallback) {
};
AuthHttpClient.prototype.executeAjax = function (url, dataToSave, httpVerb, callback, errorCallback, statusCodeCallback) {
//http://api.jquery.com/jquery.ajax/
$.ajax(url, {
data: JSON.stringify(dataToSave),
type: httpVerb,
success: (data, textStatus, jqXHR) => {
success: function (data, textStatus, jqXHR) {
if (callback !== null) {
callback(data);
}
},
error: (xhr, ajaxOptions, thrown) => {
error: function (xhr, ajaxOptions, thrown) {
if (errorCallback != null) {
errorCallback(xhr, ajaxOptions, thrown);
}
},
statusCode: statusCodeCallback,
contentType: 'application/json; charset=UTF-8',
headers: {
Accept: 'text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8',
Accept: 'text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8'
},
beforeSend: (xhr, settings) => {
beforeSend: function (xhr, settings) {
xhr.setRequestHeader('Authorization', 'bearer ' + sessionStorage.getItem('access_token'));
}
});
}
};
/**
* Get oAuth token through username and password. The token will be saved in sessionStorage.
*/
getToken(url, username, password, callback, errorCallback, statusCodeCallback) {
AuthHttpClient.prototype.getToken = function (url, username, password, callback, errorCallback, statusCodeCallback) {
$.ajax(url + 'token', {
data: {
'grant_type': 'password',
'username': username,
'password': password
},
type: 'POST',
success: (data, textStatus, jqXHR) => {
success: function (data, textStatus, jqXHR) {
if (data != null && data != '') {
sessionStorage.setItem("access_token", data.access_token);
sessionStorage.setItem("expires_in", data.expires_in);
Expand All @@ -101,21 +106,21 @@ class AuthHttpClient {
callback(data);
}
},
error: (xhr, ajaxOptions, thrown) => {
error: function (xhr, ajaxOptions, thrown) {
if (errorCallback != null) {
errorCallback(xhr, ajaxOptions, thrown);
}
},
statusCode: statusCodeCallback,
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
headers: {
Accept: 'text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8',
Accept: 'text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8'
}
});
}
}
/**
location.origin may not be working in some releases of IE. And locationOrigin is an alternative implementation
**/
AuthHttpClient.locationOrigin = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '') + '/';
//# sourceMappingURL=HttpClient.js.map
};
/**
location.origin may not be working in some releases of IE. And locationOrigin is an alternative implementation
**/
AuthHttpClient.locationOrigin = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '') + '/';
return AuthHttpClient;
}());
Loading

0 comments on commit 33918a1

Please sign in to comment.