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:
parent
dbe1425a16
commit
9eed5e827a
@ -3,7 +3,6 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Topshelf;
|
|
||||||
|
|
||||||
namespace CardReaderService
|
namespace CardReaderService
|
||||||
{
|
{
|
||||||
@ -11,20 +10,28 @@ namespace CardReaderService
|
|||||||
{
|
{
|
||||||
internal static void Configure()
|
internal static void Configure()
|
||||||
{
|
{
|
||||||
HostFactory.Run(configure =>
|
//try
|
||||||
{
|
//{
|
||||||
configure.Service<Service1>(service =>
|
// HostFactory.Run(configure =>
|
||||||
{
|
// {
|
||||||
service.ConstructUsing(s => new Service1());
|
// configure.Service<Service1>(service =>
|
||||||
service.WhenStarted(s => s.Start(null));
|
// {
|
||||||
service.WhenStopped(s => s.Stop());
|
// service.ConstructUsing(s => new Service1());
|
||||||
});
|
// service.WhenStarted(s => s.Start());
|
||||||
//Setup Account that window service use to run.
|
// service.WhenStopped(s => s.Stop());
|
||||||
configure.RunAsLocalSystem();
|
// });
|
||||||
configure.SetServiceName("MyWindowServiceWithTopshelf");
|
// //Setup Account that window service use to run.
|
||||||
configure.SetDisplayName("MyWindowServiceWithTopshelf");
|
// configure.RunAsLocalSystem();
|
||||||
configure.SetDescription("My .Net windows service with Topshelf");
|
// configure.SetServiceName("MyWindowServiceWithTopshelf");
|
||||||
});
|
// configure.SetDisplayName("MyWindowServiceWithTopshelf");
|
||||||
|
// configure.SetDescription("My .Net windows service with Topshelf");
|
||||||
|
// });
|
||||||
|
//}
|
||||||
|
//catch (Exception ex)
|
||||||
|
//{
|
||||||
|
|
||||||
|
// throw;
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,10 +18,20 @@ namespace CardReaderService
|
|||||||
|
|
||||||
using (var client = new HttpClient())
|
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 content = new StringContent(jsonObject, Encoding.UTF8, "application/json");
|
||||||
|
try
|
||||||
var response = client.PostAsync(endpointConfig, content).Result;
|
{
|
||||||
|
Console.WriteLine("Writing");
|
||||||
|
var fullUrl = endpointConfig + url;
|
||||||
|
var response = client.PostAsync(fullUrl, content).Result;
|
||||||
|
Console.WriteLine("Written");
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
Console.WriteLine("exceptioning");
|
||||||
|
//ignore
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,14 +1,10 @@
|
|||||||
using PCSC;
|
using PCSC;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
|
||||||
using System.Data;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.ServiceProcess;
|
using System.ServiceProcess;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace CardReaderService
|
namespace CardReaderService
|
||||||
{
|
{
|
||||||
@ -26,34 +22,30 @@ namespace CardReaderService
|
|||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Start(string[] args)
|
public void Start()
|
||||||
{
|
{
|
||||||
OnStart(args);
|
OnStart(new string[] {});
|
||||||
}
|
}
|
||||||
|
//
|
||||||
protected override void OnStart(string[] args)
|
protected override void OnStart(string[] args)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("Starting.. Getting available readers");
|
Console.WriteLine("Starting.. Getting available readers");
|
||||||
var ctxFactory = ContextFactory.Instance;
|
var ctxFactory = ContextFactory.Instance;
|
||||||
using(var context = ctxFactory.Establish(SCardScope.System))
|
using(var context = ctxFactory.Establish(SCardScope.System))
|
||||||
{
|
{
|
||||||
var readerNames = context.GetReaders();
|
var readerNames = context.GetReaders();
|
||||||
if (NoReaderAvailable(readerNames))
|
if (NoReaderAvailable(readerNames))
|
||||||
{
|
{
|
||||||
Debug.WriteLine("No Card Reader is available, Exiting..");
|
Console.WriteLine("No Card Reader is available, Exiting..");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Debug.WriteLine("Choosing first available reader: " + readerNames.First());
|
Console.WriteLine("Choosing first available reader: " + readerNames.First());
|
||||||
_readerName = readerNames.First();
|
_readerName = readerNames.First();
|
||||||
_cardMonitor = new SCardMonitor(ctxFactory, SCardScope.System);
|
_cardMonitor = new SCardMonitor(ctxFactory, SCardScope.System);
|
||||||
_cardMonitor.CardInserted += _cardMonitor_CardInserted;
|
_cardMonitor.CardInserted += _cardMonitor_CardInserted;
|
||||||
_cardMonitor.Start(_readerName);
|
_cardMonitor.Start(_readerName);
|
||||||
|
|
||||||
StartWorkerThread();
|
StartWorkerThread();
|
||||||
//_reader = new SCardReader(context);
|
|
||||||
//var err = _reader.Connect( _readerName,
|
|
||||||
// SCardShareMode.Shared,
|
|
||||||
// SCardProtocol.T0 | SCardProtocol.T1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,14 +57,20 @@ namespace CardReaderService
|
|||||||
protected override void OnStop()
|
protected override void OnStop()
|
||||||
{
|
{
|
||||||
_stopMainWorkerThread = true;
|
_stopMainWorkerThread = true;
|
||||||
_mainWorkThread.Join(3000);
|
if (_mainWorkThread!= null && _mainWorkThread.IsAlive)
|
||||||
if (_mainWorkThread.IsAlive)
|
|
||||||
{
|
{
|
||||||
_mainWorkThread.Interrupt();
|
_mainWorkThread.Join(3000);
|
||||||
|
if (_mainWorkThread.IsAlive)
|
||||||
|
{
|
||||||
|
_mainWorkThread.Interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (_cardMonitor != null)
|
||||||
|
{
|
||||||
|
_cardMonitor.Cancel();
|
||||||
|
_cardMonitor.Dispose();
|
||||||
|
_cardMonitor = null;
|
||||||
}
|
}
|
||||||
_cardMonitor.Cancel();
|
|
||||||
_cardMonitor.Dispose();
|
|
||||||
_cardMonitor = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void StartWorkerThread()
|
private void StartWorkerThread()
|
||||||
@ -116,7 +114,7 @@ namespace CardReaderService
|
|||||||
Console.WriteLine("Card Inserted, ATR: " + atrString + ", and UID is: " + uid);
|
Console.WriteLine("Card Inserted, ATR: " + atrString + ", and UID is: " + uid);
|
||||||
|
|
||||||
CardDataPost postObj = new CardDataPost {CardUId = uid};
|
CardDataPost postObj = new CardDataPost {CardUId = uid};
|
||||||
DataCenterHelper.Post(postObj, "/postSwipeData");
|
DataCenterHelper.PostAsync(postObj, "/api/swipedata");
|
||||||
Console.WriteLine("Posted to Server");
|
Console.WriteLine("Posted to Server");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CardReaderService;
|
using CardReaderService;
|
||||||
|
|
||||||
namespace CardReaderServiceHost
|
namespace CardReaderServiceHost
|
||||||
@ -12,7 +8,7 @@ namespace CardReaderServiceHost
|
|||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Service1 service1 = new Service1();
|
Service1 service1 = new Service1();
|
||||||
service1.Start(null);
|
service1.Start();
|
||||||
Console.WriteLine("Service Running...");
|
Console.WriteLine("Service Running...");
|
||||||
Console.ReadLine();
|
Console.ReadLine();
|
||||||
service1.Stop();
|
service1.Stop();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user