So I have recently built at ExceptionFilter which handles all errors except Api Errors.
The ExceptionFilter looks like this:
public class ExceptionAttribute : IExceptionFilter
{
/// <summary>
/// Handles any exception
/// </summary>
/// <param name="filterContext">The current context</param>
public void OnException(ExceptionContext filterContext)
{
// If our exception has been handled, exit the function
if (filterContext.ExceptionHandled)
return;
// If our exception is not an ApiException
if (!(filterContext.Exception is ApiException))
{
// Set our base status code
var statusCode = (int)HttpStatusCode.InternalServerError;
// If our exception is an http exception
if (filterContext.Exception is HttpException)
{
// Cast our exception as an HttpException
var exception = (HttpException)filterContext.Exception;
// Get our real status code
statusCode = exception.GetHttpCode();
}
// Set our context result
var result = CreateActionResult(filterContext, statusCode);
// Set our handled property to true
filterContext.ExceptionHandled = true;
}
}
/// <summary>
/// Creats an action result from the status code
/// </summary>
/// <param name="filterContext">The current context</param>
/// <param name="statusCode">The status code of the error</param>
/// <returns></returns>
protected virtual ActionResult CreateActionResult(ExceptionContext filterContext, int statusCode)
{
// Create our context
var context = new ControllerContext(filterContext.RequestContext, filterContext.Controller);
var statusCodeName = ((HttpStatusCode)statusCode).ToString();
// Create our route
var controller = (string)filterContext.RouteData.Values["controller"];
var action = (string)filterContext.RouteData.Values["action"];
var model = new HandleErrorInfo(filterContext.Exception, controller, action);
// Create our result
var view = SelectFirstView(context, string.Format("~/Views/Error/{0}.cshtml", statusCodeName), "~/Views/Error/Index.cshtml", statusCodeName);
var result = new ViewResult { ViewName = view, ViewData = new ViewDataDictionary<HandleErrorInfo>(model) };
// Return our result
return result;
}
/// <summary>
/// Gets the first view name that matches the supplied names
/// </summary>
/// <param name="context">The current context</param>
/// <param name="viewNames">A list of view names</param>
/// <returns></returns>
protected string SelectFirstView(ControllerContext context, params string[] viewNames)
{
return viewNames.First(view => ViewExists(context, view));
}
/// <summary>
/// Checks to see if a view exists
/// </summary>
/// <param name="context">The current context</param>
/// <param name="name">The name of the view to check</param>
/// <returns></returns>
protected bool ViewExists(ControllerContext context, string name)
{
var result = ViewEngines.Engines.FindView(context, name, null);
return result.View != null;
}
}
As you can see, if the error is not an ApiException then I route the user to the error controller.
The ApiException is just an error that happens when I make an API call from within MVC.
When these errors happen I would like to return the error as JSON back to the client so that the JavaScript can handle it.
I thought not handling the error would do this, but instead it generates a server error (albeit with the JSON error in it) like so:
Server Error in '/' Application.
{"message":"validateMove validation failure:\r\nThe item is despatched and cannot be moved"}
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: SapphirePlus.Web.ApiException: {"message":"validateMove validation failure:\r\nThe item is despatched and cannot be moved"}
Source Error:
Line 181: if (response.StatusCode != HttpStatusCode.OK)
Line 182: throw new ApiException(result);
So my question is, can I get the Application_Error method to get errors that ARE ApiExceptions and return the error as JSON?
So my question is, can I get the Application_Error method to get
errors that ARE ApiExceptions and return the error as JSON?
Of course:
protected void Application_Error()
{
var apiException = Server.GetLastError() as ApiException;
if (apiException != null)
{
Response.Clear();
Server.ClearError();
Response.StatusCode = 400;
Context.Response.ContentType = "application/json";
Context.Response.Write("YOUR JSON HERE");
}
}
In the end I didn't need to use Global.asax at all, I was able to handle it all inside my ExceptionAttribute class like this:
public class ExceptionAttribute : IExceptionFilter
{
/// <summary>
/// Handles any exception
/// </summary>
/// <param name="filterContext">The current context</param>
public void OnException(ExceptionContext filterContext)
{
// If our exception has been handled, exit the function
if (filterContext.ExceptionHandled)
return;
// Set our base status code
var statusCode = (int)HttpStatusCode.BadRequest;
// If our exception is an http exception
if (filterContext.Exception is HttpException)
{
// Cast our exception as an HttpException
var exception = (HttpException)filterContext.Exception;
// Get our real status code
statusCode = exception.GetHttpCode();
}
// Set our context result
var result = CreateActionResult(filterContext, statusCode);
// Set our handled property to true
filterContext.Result = result;
filterContext.ExceptionHandled = true;
}
/// <summary>
/// Creats an action result from the status code
/// </summary>
/// <param name="filterContext">The current context</param>
/// <param name="statusCode">The status code of the error</param>
/// <returns></returns>
protected virtual ActionResult CreateActionResult(ExceptionContext filterContext, int statusCode)
{
// Create our context
var context = new ControllerContext(filterContext.RequestContext, filterContext.Controller);
var statusCodeName = ((HttpStatusCode)statusCode).ToString();
// Create our route
var controller = (string)filterContext.RouteData.Values["controller"];
var action = (string)filterContext.RouteData.Values["action"];
var model = new HandleErrorInfo(filterContext.Exception, controller, action);
// Create our result
var view = SelectFirstView(context, string.Format("~/Views/Error/{0}.cshtml", statusCodeName), "~/Views/Error/Index.cshtml", statusCodeName);
var result = new ViewResult { ViewName = view, ViewData = new ViewDataDictionary<IError>(this.Factorize(model)) };
// Return our result
return result;
}
/// <summary>
/// Factorizes the HandleErrorInfo
/// </summary>
/// <param name="error"></param>
/// <returns></returns>
protected virtual IError Factorize(HandleErrorInfo error)
{
// Get the error
var model = new Error
{
Message = "There was an unhandled exception."
};
// If we have an error
if (error != null)
{
// Get our exception
var exception = error.Exception;
// If we are dealing with an ApiException
if (exception is ApiException || exception is HttpException)
{
// Get our JSON
var json = JObject.Parse(exception.Message);
var message = json["exceptionMessage"] != null ? json["exceptionMessage"] : json["message"];
// If we have a message
if (message != null)
{
// Update our model message
model.Message = message.ToString();
}
}
else
{
// Update our message
model.Message = exception.Message;
}
}
// Return our response
return model;
}
/// <summary>
/// Gets the first view name that matches the supplied names
/// </summary>
/// <param name="context">The current context</param>
/// <param name="viewNames">A list of view names</param>
/// <returns></returns>
protected string SelectFirstView(ControllerContext context, params string[] viewNames)
{
return viewNames.First(view => ViewExists(context, view));
}
/// <summary>
/// Checks to see if a view exists
/// </summary>
/// <param name="context">The current context</param>
/// <param name="name">The name of the view to check</param>
/// <returns></returns>
protected bool ViewExists(ControllerContext context, string name)
{
var result = ViewEngines.Engines.FindView(context, name, null);
return result.View != null;
}
}
This handled any Mvc error and for my Api calls, I did this:
/// <summary>
/// Used to handle the api response
/// </summary>
/// <param name="response">The HttpResponseMessage</param>
/// <returns>Returns a string</returns>
private async Task<string> HandleResponse(HttpResponseMessage response)
{
// Read our response content
var result = await response.Content.ReadAsStringAsync();
// If there was an error, throw an HttpException
if (response.StatusCode != HttpStatusCode.OK)
throw new ApiException(result);
// Return our result if there are no errors
return result;
}
This allowed me to capture the ApiError and handle the response differently than with any other exception.
Related
Having the below class
/// <summary>
/// An unsuccessful HTTP result.
/// </summary>
public class UnsuccessfulActionResult : IActionResult, IHttpStatusCodeResult, IErrors
{
/// <inheritdoc />
/// <summary>
/// The HTTP status code.
/// </summary>
public HttpStatusCode StatusCode { get; }
/// <summary>
/// The corresponding error messages, if any.
/// </summary>
public IReadOnlyCollection<string> Errors { get; }
/// <summary>
/// The request Transaction ID.
/// </summary>
public string TransactionId { get; }
/// <summary>
/// Default ctor.
/// </summary>
/// <param name="transactionId">The request Transaction ID.</param>
/// <param name="errors">The corresponding error messages, if any.</param>
/// <param name="httpStatusCode"></param>
public UnsuccessfulActionResult(
string transactionId,
HttpStatusCode httpStatusCode,
IReadOnlyCollection<string> errors = null)
{
TransactionId = transactionId;
StatusCode = httpStatusCode;
Errors = errors ?? new List<string>();
}
/// <summary>
/// Convenient method to create an unsuccessful response model.
/// </summary>
/// <param name="statusCode"></param>
/// <returns></returns>
protected UnsuccessfulResponseModel CreateResponseModel(int statusCode)
{
return new UnsuccessfulResponseModel(TransactionId, statusCode, Errors);
}
/// <inheritdoc />
public virtual Task ExecuteResultAsync(ActionContext context)
{
var objectResult = new ObjectResult(CreateResponseModel((int)StatusCode))
{
StatusCode = (int)StatusCode
};
return objectResult.ExecuteResultAsync(context);
}
}
I need to test this class to make sure that ActionContext has the response provided in overridden ExecuteResultAsync.
Just started going deep into ASP.NET Core 2.0 and missing some parts, in Web API 2 I would just check the HttpResponseMessage returned in the equivalent method of IHttpActionResult.
Task<HttpResponseMessage> ExecuteAsync(
CancellationToken cancellationToken
)
I am trying to build a FilterAttribute that will handle my errors for my Web API. Currently it logs the errors to the database, but it always returns a 500 Internal Server error no matter what I do.
My filter looks like this:
public class LogExceptionFilterAttribute : ExceptionFilterAttribute
{
// Private properties
private readonly ILogProvider _logger;
/// <summary>
/// Our default constructor
/// </summary>
/// <param name="logger"></param>
public LogExceptionFilterAttribute(ILogProvider logger)
{
_logger = logger;
}
/// <summary>
/// Invoked when an exception has been thrown
/// </summary>
/// <param name="context">The context</param>
public override async void OnException(HttpActionExecutedContext context)
{
// Get our user
var requestContext = context.Request.GetRequestContext();
var user = requestContext.Principal.Identity;
// Create our response
var message = await _logger.ErrorAsync(context.Exception, user);
var statusCode = GetStatusCodeFromException(context);
var content = new HttpResponseMessage(statusCode);
//{
// Content = new StringContent(message),
// StatusCode = statusCode
//};
// Assign our response to our context
context.Response = content;
}
/// <summary>
/// Gets a status code from an error
/// </summary>
/// <param name="context">The context</param>
/// <returns></returns>
private static HttpStatusCode GetStatusCodeFromException(HttpActionExecutedContext context)
{
// Cast the exception as an HttpException
var exception = context.Exception as HttpException;
// If there is still an exception, return the code
if (exception != null)
return (HttpStatusCode)exception.GetHttpCode();
// Switch on the exception type
switch (context.Exception)
{
// Not found
case ObjectNotFoundException ex:
return HttpStatusCode.NotFound;
// Not implemented
case NotImplementedException ex:
return HttpStatusCode.NotImplemented;
// Internal server error
default:
return HttpStatusCode.InternalServerError;
}
}
}
I have put a breakpoint in the GetStatusCodeFromException method and verified that the actual status code I should be getting is 501 (Not Implemented) but the return is always 500. I have tried with other status codes too and it is always a 500.
Does someone know how I can actually get it to return a different status code?
Update
I have found out more.
If I change my OnException method to this:
public override async void OnException(HttpActionExecutedContext context)
{
// Get our user
var request = context.Request;
//var requestContext = request.GetRequestContext();
//var user = requestContext.Principal.Identity;
//// Log our error and get the status code
//var message = await _logger.ErrorAsync(context.Exception, user);
//var statusCode = GetStatusCodeFromException(context);
// Create our response
context.Response = request.CreateResponse(HttpStatusCode.NotImplemented);
}
I get a nice 501 error when using postman, but as soon as I uncomment the requestContext line, it get an error 500. I think that invoking GetRequestContext somehow changes the HttpRequestMessage so that when I invoke CreateResponse it always generates a 500.
Anyone know why? Or how to get around it?
I have been here before, but this time I have managed to get further than I have before.
I have been following this guide and done everything it has said.
If I navigate to http://localhost:61589/help I actually see the help page, but there is only the introduction, there are no descriptions.
In my controller I have comments (always do) like this:
/// <summary>
/// For all answer related endpoints
/// </summary>
[RoutePrefix("answers")]
public class AnswersController : ApiController
{
// Readonly properties
private readonly IUnitOfWork _unitOfWork;
private readonly AnswerService _service;
private readonly StateService _stateService;
/// <summary>
/// Default constructor
/// </summary>
public AnswersController()
{
// Map our properties
this._unitOfWork = new UnitOfWork<DatabaseContext>();
this._service = new AnswerService(this._unitOfWork);
this._stateService = new StateService(this._unitOfWork);
}
/// <summary>
/// Get a list of answers
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("")]
public async Task<IHttpActionResult> GetAllAsync()
{
try
{
// Return all our answers
return Ok(await this._service.GetAllAsync("States.Filters"));
// If there is an error
}
catch (Exception ex)
{
// Return our error
return BadRequest(ex.Message.ToString());
}
}
/// <summary>
/// Get a answer by id
/// </summary>
/// <param name="id">The answer id</param>
/// <returns></returns>
[HttpGet]
[Route("")]
public async Task<IHttpActionResult> GetAsync(int id)
{
try
{
// Return all our answers
return Ok(await this._service.GetAsync(id, "States.Filters"));
// If there is an error
}
catch (Exception ex)
{
// Return our error
return BadRequest(ex.Message.ToString());
}
}
/// <summary>
/// Create a answer
/// </summary>
/// <param name="model">The answer model</param>
/// <returns></returns>
[HttpPost]
[Route("")]
public async Task<IHttpActionResult> CreateAsync(Answer model)
{
try
{
// Get our states
var all = await this._stateService.GetAllAsync("Filters");
var states = all.Where(m => model.States.Any(s => s.Id == m.Id)).ToList();
// Create our model
var answer = new Answer
{
Text = model.Text,
QuestionId = model.QuestionId,
Order = model.Order,
States = states
};
// Save our model
this._service.Create(answer);
// Save the database changes
await this._unitOfWork.SaveChangesAsync();
// Return our updated model
return Ok(answer);
// If there is an error
}
catch (Exception ex)
{
// Return our error
return BadRequest(ex.Message.ToString());
}
}
/// <summary>
/// Update a answer
/// </summary>
/// <param name="model">The answer model</param>
/// <returns></returns>
[HttpPut]
[Route("")]
public async Task<IHttpActionResult> UpdateAsync(Answer model)
{
try
{
// Create our model
var answer = new Answer
{
Id = model.Id,
QuestionId = model.QuestionId,
Order = model.Order,
Text = model.Text
};
// Save our model
this._service.Update(answer);
// Save the database changes
await this._unitOfWork.SaveChangesAsync();
// Return our updated model
return Ok(model);
// If there is an error
}
catch (Exception ex)
{
// Return our error
return BadRequest(ex.Message.ToString());
}
}
/// <summary>
/// Delete a answer
/// </summary>
/// <param name="id">The answer id</param>
/// <returns></returns>
[HttpDelete]
[Route("")]
public async Task<IHttpActionResult> DeleteAsync(int id)
{
try
{
// Get our model
var model = await this._service.GetAsync(id);
// Save our model
this._service.Remove(model);
// Save the database changes
await this._unitOfWork.SaveChangesAsync();
// Return Ok
return Ok();
// If there is an error
}
catch (Exception ex)
{
// Return our error
return BadRequest(ex.Message.ToString());
}
}
}
In the generated XmlDocument.xml I have these memebers:
<member name="T:Piiick.Api.Controllers.AnswersController">
<summary>
For all answer related endpoints
</summary>
</member>
<member name="M:Piiick.Api.Controllers.AnswersController.#ctor">
<summary>
Default constructor
</summary>
</member>
<member name="M:Piiick.Api.Controllers.AnswersController.GetAllAsync">
<summary>
Get a list of answers
</summary>
<returns></returns>
</member>
<member name="M:Piiick.Api.Controllers.AnswersController.GetAsync(System.Int32)">
<summary>
Get a answer by id
</summary>
<param name="id">The answer id</param>
<returns></returns>
</member>
<member name="M:Piiick.Api.Controllers.AnswersController.CreateAsync(Piiick.Data.Models.Answer)">
<summary>
Create a answer
</summary>
<param name="model">The answer model</param>
<returns></returns>
</member>
<member name="M:Piiick.Api.Controllers.AnswersController.UpdateAsync(Piiick.Data.Models.Answer)">
<summary>
Update a answer
</summary>
<param name="model">The answer model</param>
<returns></returns>
</member>
<member name="M:Piiick.Api.Controllers.AnswersController.DeleteAsync(System.Int32)">
<summary>
Delete a answer
</summary>
<param name="id">The answer id</param>
<returns></returns>
</member>
But they are not appearing on the actual help page.
Does anyone have any idea why this might be happening?
Turns out I only had to add this line:
GlobalConfiguration.Configure(WebApiConfig.Register);
to my Startup.cs file in the Configuration method.
Once this was added, it started working.
For anyone else, that looks like this:
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
// Get our http configuration
var config = new HttpConfiguration();
// Register all areas
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
// Use our web api
app.UseWebApi(config);
}
}
According to http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages
You can use XML documentation comments to create the documentation. To enable this feature, open the file Areas/HelpPage/App_Start/HelpPageConfig.cs and uncomment the following line:
config.SetDocumentationProvider(new XmlDocumentationProvider(
HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
I'm building a Tic-Tac_toe api game. The game work ok, until one of the players press on a square that finish the game(either he wins or fill the board).
When I enabled all CLR exceptions for the last move, this is the exception I saw for line var user = await UserManager.FindByIdAsync(userId);
"Additional information: A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe."
Importent to know when a player click a square, it is a 'POST' method.
Here's my code:
public class GamesApiController : ApiController
{
ApplicationDbContext context = new ApplicationDbContext();
private ApplicationUserManager _userManager;
public IEnumerable<ApplicationUser> Get()
{
return context.Users;
}
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? System.Web.HttpContext.Current.Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
#region Methods
/// <summary>
/// update the server data by reciving the model and square and returns the new model
/// </summary>
/// <param name="_model"></param>
/// <param name="squareId"></param>
/// <returns></returns>
//square clicked via post
[Route("api/gamesapi/{squareId}")]
public async Task<HttpResponseMessage> Post([FromBody]GameModel model, int squareId)
{
HttpResponseMessage response;
if (model == null)
{
//Utilites.CreateMsgCookie(Response, "Error", "Sorry, an unknown error has occurred");
response = Request.CreateErrorResponse(HttpStatusCode.NotFound, "model wasn't found");
return response;
}
//GameModel model = JsonConvert.DeserializeObject<GameModel>(_model);
Game game = GetGameById(model.Game.GameId);
if (game == null)
{
response = Request.CreateErrorResponse(HttpStatusCode.NotFound, "game wasn't found");
}
else
{
if (game.UserIdTurn == game.User1Id) //pressing user is user1
{
ChangeSquareState(game, squareId, true);
game.UserIdTurn = game.User2Id;
}
else //game.UserIdTurn == game.User2Id - pressing user is user2
{
ChangeSquareState(game, squareId, false);
game.UserIdTurn = game.User1Id;
}
SquareState[] board = new SquareState[] {game.Square1,game.Square2,game.Square3,game.Square4,
game.Square5,game.Square6,game.Square7,game.Square8,game.Square9};
if (didPlayerWin(board))
{
game.WinnerId = model.User.Id;
await UpdateUserGameState(1, game.User1Id);
await UpdateUserGameState(2, game.User2Id);
game.IsGameOver = true;
}
else
{
bool isBoardFull = true;
for (int i = 0; i < board.Length; i++)
{
if (board[i] == SquareState.Blank)
{
isBoardFull = false;
break;
}
}
if (isBoardFull)
{
await UpdateUserGameState(3, game.User1Id);
await UpdateUserGameState(3, game.User2Id);
game.IsGameOver = true;
}
}
context.SaveChanges();
response = Request.CreateResponse(HttpStatusCode.OK, game);
}
return response;
}
/// <summary>
/// When a game is over, recive a gameState and update the user. 1 for a win, 2 for loss, 3 for aa draw
/// </summary>
/// <param name="gameState"></param>
private async Task UpdateUserGameState(int gameState, string userId)
{
var user = await UserManager.FindByIdAsync(userId);
switch (gameState)
{
case 1:
user.GamesWon++;
break;
case 2:
user.GamesLost++;
break;
case 3:
user.GamesDraw++;
break;
default:
break;
}
UserManager.UpdateAsync(user);
}
[HttpGet]
[Route("api/gamesapi/{gameId}")]
/// <summary>
/// method to bring the latest game's state from the context and send it back in a GameModel
/// </summary>
/// <param name="_model"></param>
/// <returns></returns>
public HttpResponseMessage Get(int gameId)
{
HttpResponseMessage response;
Game game = GetGameById(gameId);
if (game == null)
{
response = Request.CreateErrorResponse(HttpStatusCode.NotFound, "game wasn't found");
}
else
{
response = Request.CreateResponse(HttpStatusCode.OK, game);
}
return response;
}
/// <summary>
/// method that check if the board have a line(3 squars in a row)
/// of the same element of user1 or user2 , defult - returns fault
/// </summary>
/// <param name="board"></param>
/// <returns></returns>
private bool didPlayerWin(SquareState[] board)
{
}
/// <summary>
/// change the SquareState of a specific square of the sent game according to the pressing user
/// </summary>
/// <param name="game"></param>
/// <param name="SquareId"></param>
/// <param name="_isUser1"></param>
private void ChangeSquareState(Game game, int SquareId, bool _isUser1)
{
}
/// <summary>
/// get game from context by gameId , Defult result - null
/// </summary>
/// <param name="gameId"></param>
/// <returns></returns>
private Game GetGameById(int gameId)
{
}
#endregion
}
I think you missed to wait for UpdateUserAsync() in method UpdateUserGameState():
private async Task UpdateUserGameState(int gameState, string userId)
{
var user = await UserManager.FindByIdAsync(userId);
// ...
// add missing await
await UserManager.UpdateAsync(user);
}
I'm new to this forum and to have a question about await/async use in Xamarin (Also the first time I work with).
I am working for my internship on a project using Xamarin, PCL, MvvmCross.
In my PCL im do a postrequest to a WCF service to login in my application. In WP8 everything just works fine, but when I am running my application on Android the response is always null.
Below you can find my httpclient class. The method with the post is InternalPostAsync
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using Anton.Mobile.Shared.Infrastructure;
using System.Net;
using Newtonsoft.Json.Linq;
using System.Net.Http.Headers;
namespace Anton.Mobile.Shared.Data
{
public class AntonClient
{
#region static
/// <summary>
/// Base Uri of the Ria service (e.g. http://example.com/)
/// </summary>
private static readonly Uri _baseUri = new Uri(Config.BaseUri);
/// <summary>
/// Last cookie response header (Session, authentication, ...)
/// </summary>
private static string _cookieHeader = null;
/// <summary>
/// Lock object for read/write <para>_cookieHeader</para>
/// </summary>
private static object _lockObj = new object();
#endregion
#region protected
/// <summary>
/// Creates a client
/// </summary>
/// <param name="container">Cookie container to use</param>
/// <returns>HttpClient</returns>
protected virtual HttpClient CreateClient(CookieContainer container)
{
//set container on handler for tracking cookies between request-response
var handler = new HttpClientHandler()
{
CookieContainer = container,
UseCookies = true,
UseDefaultCredentials = false,
};
//Create client and set the base address
var cl = new HttpClient(handler)
{
BaseAddress = _baseUri
};
if (!string.IsNullOrEmpty(_cookieHeader))
{
cl.DefaultRequestHeaders.Add("Cookies", _cookieHeader);
}
return cl;
}
/// <summary>
/// Creates a JSON content request
/// </summary>
/// <param name="jsonContent">JSON value</param>
/// <returns>JSON content</returns>
protected virtual HttpContent CreateRequestContent(string jsonContent)
{
var content = new StringContent(jsonContent,Encoding.UTF8,"application/json");
//content.Headers.Add("ContentType", "application/json");
return content;
}
/// <summary>
/// Save cookies <para>_cookieHeader</para>
/// </summary>
/// <param name="container">cookie container</param>
protected void ParseCookies(HttpResponseMessage msg)
{
IEnumerable<string> values;
if (!msg.Headers.TryGetValues("Set-Cookie", out values) || !values.Any())
return;
//var cookies = container.GetCookieHeader(_baseUri);
var cs = new List<string>();
foreach (var v in values)
{
string[] vs = v.Split(new char[] { ';' });
string[] value = vs[0].Split(new char[] { '=' });
container.Add(new Uri("Http://initesting"), new Cookie(value[0], value[1]));
cs.Add(string.Format("{0}={1}", value[0], value[1]));
}
lock (_lockObj)
{
_cookieHeader = string.Join(";", cs.ToArray());
}
}
private static CookieContainer container = new CookieContainer();
/// <summary>
/// Create a new cookie container from <para>_cookieHeaders</para>
/// </summary>
/// <returns>Cookie container</returns>
protected CookieContainer CreateCookieContainer()
{
//lock (_lockObj)
//{
// if (!string.IsNullOrEmpty(_cookieHeader))
// {
// foreach (var header in _cookieHeader.Split(new char[] { ';' }))
// {
// container.SetCookies(_baseUri, header);
// }
// }
//}
return container;
}
/// <summary>
/// Executes a POST HTTP Request
/// </summary>
/// <param name="jsonContent">POST JSON content</param>
/// <param name="uri">Service uri</param>
/// <returns>Response content as string (JSON)</returns>
protected virtual async Task<string> InternalPostAsync(string jsonContent, Uri uri)
{
var container = CreateCookieContainer();
using (var client = CreateClient(container))
{
var content = CreateRequestContent(jsonContent);
var response = await client.PostAsync(uri, content);
if (response.StatusCode != HttpStatusCode.OK)
{
return null; //todo
}
ParseCookies(response);
return await response.Content.ReadAsStringAsync();
}
}
/// <summary>
/// Executes a GET HTTP Request
/// </summary>
/// <param name="uri">Service uri</param>
/// <returns>Response content as string (JSON)</returns>
protected virtual async Task<string> InternalRequestAsync(Uri uri)
{
var container = CreateCookieContainer();
using (var client = CreateClient(container))
{
HttpResponseMessage response = await client.GetAsync(uri);
if (response.StatusCode != HttpStatusCode.OK)
{
return null;
}
ParseCookies(response);
return await response.Content.ReadAsStringAsync();
}
}
#endregion protected
#region public
/// <summary>
/// Executes a POST HTTP Request for a given Request key
/// </summary>
/// <typeparam name="TRequest">Request Type</typeparam>
/// <typeparam name="TResult">Result Type</typeparam>
/// <param name="request">Request POST value to JSON serializing</param>
/// <param name="key">Unique Request Key</param>
/// <returns>Deserialized POST response content of type TResult</returns>
public async Task<TResult> PostAsync<TRequest, TResult>(TRequest request, RequestKey key)
where TRequest : class
where TResult : class
{
try
{
var uri = RequestMap.GetUri(key);
string jsonResult = await InternalPostAsync(request.SerializeJson(), uri);
return jsonResult.DeserializeJson<TResult>();
}
catch (Exception)
{
//todo
}
return default(TResult);
}
/// <summary>
/// Executes a POST HTTP Request for a given service uri
/// </summary>
/// <typeparam name="TRequest">Request Type</typeparam>
/// <param name="request">Request POST value to JSON serializing</param>
/// <param name="uri">Service URI</param>
/// <returns>Deserialized POST response content of type dynamic</returns>
public async Task<dynamic> PostAsync<TRequest>(TRequest request, string uri)
{
try
{
string jsonResult = await InternalPostAsync(request.SerializeJson(), new Uri(uri, UriKind.Absolute));
return jsonResult.DynamicJson();
}
catch (Exception)
{
//todo
}
return null;
}
/// <summary>
/// Executes a GET HTTP Request for a givin key and query string parameter info
/// </summary>
/// <typeparam name="TResponse">Response Type</typeparam>
/// <param name="key">Unique request key</param>
/// <param name="queryString">Querystring info</param>
/// <returns>Deserialized POST response content of type TResult</returns>
public async Task<TResponse> RequestAsync<TResponse>(RequestKey key, IDictionary<string, string> queryString = null)
{
try
{
string jsonResult = await InternalRequestAsync(RequestMap.GetUri(key, queryString));
var dynamicResult = jsonResult.DynamicJson();
var item = (dynamicResult as JObject)[RequestMap.GetValue(key) + "Result"]["RootResults"].First; //todo: better solution for this
return item.ToObject<TResponse>();
}
catch (Exception)
{
//todo
}
return default(TResponse);
}
#endregion public
}
}
Regarding the website of Xamarin async/await is supported, but I did not find any similar problems. I hope you can help me.
I have solved my problem.
In Android, when you add the NuGet package with the HTTP Libraries, something went wrong when adding the references. You have to add the references mannualy to solve it. I also editted my Uri, now it works with the IP-address in the Uri.