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.
This commit is contained in:
Chris.Watts90@outlook.com 2017-01-26 16:44:07 +00:00
parent dbe1425a16
commit 9eed5e827a
4 changed files with 55 additions and 44 deletions

View File

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf;
namespace CardReaderService
{
@ -11,20 +10,28 @@ namespace CardReaderService
{
internal static void Configure()
{
HostFactory.Run(configure =>
{
configure.Service<Service1>(service =>
{
service.ConstructUsing(s => new Service1());
service.WhenStarted(s => s.Start(null));
service.WhenStopped(s => s.Stop());
});
//Setup Account that window service use to run.
configure.RunAsLocalSystem();
configure.SetServiceName("MyWindowServiceWithTopshelf");
configure.SetDisplayName("MyWindowServiceWithTopshelf");
configure.SetDescription("My .Net windows service with Topshelf");
});
//try
//{
// HostFactory.Run(configure =>
// {
// configure.Service<Service1>(service =>
// {
// service.ConstructUsing(s => new Service1());
// service.WhenStarted(s => s.Start());
// service.WhenStopped(s => s.Stop());
// });
// //Setup Account that window service use to run.
// configure.RunAsLocalSystem();
// configure.SetServiceName("MyWindowServiceWithTopshelf");
// configure.SetDisplayName("MyWindowServiceWithTopshelf");
// configure.SetDescription("My .Net windows service with Topshelf");
// });
//}
//catch (Exception ex)
//{
// throw;
//}
}
}
}

View File

@ -18,10 +18,20 @@ namespace CardReaderService
using (var client = new HttpClient())
{
var jsonObject = JsonConvert.SerializeObject(url);
var jsonObject = JsonConvert.SerializeObject(postObject);
var content = new StringContent(jsonObject, Encoding.UTF8, "application/json");
var response = client.PostAsync(endpointConfig, content).Result;
try
{
Console.WriteLine("Writing");
var fullUrl = endpointConfig + url;
var response = client.PostAsync(fullUrl, content).Result;
Console.WriteLine("Written");
}
catch (Exception)
{
Console.WriteLine("exceptioning");
//ignore
}
}
}

View File

@ -1,14 +1,10 @@
using PCSC;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CardReaderService
{
@ -26,34 +22,30 @@ namespace CardReaderService
InitializeComponent();
}
public void Start(string[] args)
public void Start()
{
OnStart(args);
OnStart(new string[] {});
}
//
protected override void OnStart(string[] args)
{
Debug.WriteLine("Starting.. Getting available readers");
Console.WriteLine("Starting.. Getting available readers");
var ctxFactory = ContextFactory.Instance;
using(var context = ctxFactory.Establish(SCardScope.System))
{
var readerNames = context.GetReaders();
if (NoReaderAvailable(readerNames))
{
Debug.WriteLine("No Card Reader is available, Exiting..");
Console.WriteLine("No Card Reader is available, Exiting..");
return;
}
Debug.WriteLine("Choosing first available reader: " + readerNames.First());
Console.WriteLine("Choosing first available reader: " + readerNames.First());
_readerName = readerNames.First();
_cardMonitor = new SCardMonitor(ctxFactory, SCardScope.System);
_cardMonitor.CardInserted += _cardMonitor_CardInserted;
_cardMonitor.Start(_readerName);
StartWorkerThread();
//_reader = new SCardReader(context);
//var err = _reader.Connect( _readerName,
// SCardShareMode.Shared,
// SCardProtocol.T0 | SCardProtocol.T1);
}
}
@ -65,15 +57,21 @@ namespace CardReaderService
protected override void OnStop()
{
_stopMainWorkerThread = true;
if (_mainWorkThread!= null && _mainWorkThread.IsAlive)
{
_mainWorkThread.Join(3000);
if (_mainWorkThread.IsAlive)
{
_mainWorkThread.Interrupt();
}
}
if (_cardMonitor != null)
{
_cardMonitor.Cancel();
_cardMonitor.Dispose();
_cardMonitor = null;
}
}
private void StartWorkerThread()
{
@ -116,7 +114,7 @@ namespace CardReaderService
Console.WriteLine("Card Inserted, ATR: " + atrString + ", and UID is: " + uid);
CardDataPost postObj = new CardDataPost {CardUId = uid};
DataCenterHelper.Post(postObj, "/postSwipeData");
DataCenterHelper.PostAsync(postObj, "/api/swipedata");
Console.WriteLine("Posted to Server");
}
}

View File

@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CardReaderService;
namespace CardReaderServiceHost
@ -12,7 +8,7 @@ namespace CardReaderServiceHost
static void Main(string[] args)
{
Service1 service1 = new Service1();
service1.Start(null);
service1.Start();
Console.WriteLine("Service Running...");
Console.ReadLine();
service1.Stop();