FlexitimeTracker/CardReaderService/CardReaderService/DataCenterHelper.cs
Chris.Watts90@outlook.com 9eed5e827a removed args argument, was annoying.
changed debug writelines to console writelines for desktop operation.
tidied code.
moved over to use PostAsync, to prevent app hanging when trying to post to DataCenter.
changed api endpoint to windows datacenter desktop endpoint.
--end Service1 changes.
remove args from Start method
--end Program changes
added try catch to stop errors when the data center endpoint isnt up.
--end DataCenterHelper changes.
removed topshelf configuration stuff.
--end ConfigureService changes.
2017-01-26 16:44:07 +00:00

49 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace CardReaderService
{
static class DataCenterHelper
{
public static void Post(CardDataPost postObject, string url)
{
var endpointConfig = ConfigurationManager.AppSettings["DataCenterServiceEndpoint"] ??
"http://localhost:8800";
using (var client = new HttpClient())
{
var jsonObject = JsonConvert.SerializeObject(postObject);
var content = new StringContent(jsonObject, Encoding.UTF8, "application/json");
try
{
Console.WriteLine("Writing");
var fullUrl = endpointConfig + url;
var response = client.PostAsync(fullUrl, content).Result;
Console.WriteLine("Written");
}
catch (Exception)
{
Console.WriteLine("exceptioning");
//ignore
}
}
}
public static Task PostAsync(CardDataPost postObject, string url)
{
return Task.Run(() => Post(postObject, url));
}
}
public class CardDataPost
{
public string CardUId { get; set; }
}
}