Reputation: 47
I have to develop something like a ASP.NET C# console application or web service on a local machine which is able to get data from a website and also send some back. I have the website on a server far from me. I have also a local machine which is connected to printers. The task is the following: someone puts some data on the website in textboxes, chooses the right printer and presses print (therefore the website needs the current list of the available printers connected to the local machine). The local machine gets and prints out the data, then sends some confirmation back.
I am using a Web Api to communicate between the Web Site and the Console Application.
This is how the Web API looks like now:
public class LabelController : UmbracoApiController
{
Label[] labels = new Label[]
{
new Label { Product="Black Top", Location="T1", VariantSKU="1111" }
};
[HttpGet]
[ActionName("SendLabel")]
public Label SendLabel(Label currentLabel)
{
Label lbl = currentLabel;
return lbl;
}
[HttpGet]
[ActionName("SendAllLabels")]
public IEnumerable<Label> SendAllLabels()
{
return labels;
}
[HttpPost]
[ActionName("GetPrinters")]
public void GetPrinters([FromBody]string value)
{
//Here I should get the list of printers from the local machine
}
}
This is how I try to call from the website to send the label to the local machine:
Label printlabel = new Label { Product = "Awesome T-Shirt", Location = "T1", VariantSKU = "0000000" };
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:49423/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var webapi = new LabelController();
webapi.SendLabel(printlabel);
}
And this is how I consume the Web API in the console application:
public class Label
{
public string Product { get; set; }
public string Location { get; set; }
public string VariantSKU { get; set; }
}
class Program
{
static HttpClient client = new HttpClient();
static void Main(string[] args)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:49423/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
PrintData();
PrinterList();
}
}
static void PrintData()
{
try
{
HttpResponseMessage resp = client.GetAsync("umbraco/api/Label/SendLabel").Result;
resp.EnsureSuccessStatusCode();
var label = resp.Content.ReadAsAsync<Label>().Result;
Console.WriteLine(label.Product);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
private static async void PrinterList()
{
var printerQuery = new ManagementObjectSearcher("SELECT * from Win32_Printer");
foreach (var printer in printerQuery.Get())
{
var name = printer.GetPropertyValue("Name");
var status = printer.GetPropertyValue("Status");
var isDefault = printer.GetPropertyValue("Default");
var isNetworkPrinter = printer.GetPropertyValue("Network");
// Here I should send the parameters back to the website
}
}
}
My problem is that I can not put my label to the SendLabel action and I do not know how to send data from the console application to the website.
Thanks!
Upvotes: 1
Views: 1999
Reputation: 1368
You can host a WCF service in your console application. This service will return the list of printers. Your web api can consume this WCF service to get the printers. The WCF Service can have methods:
After printing successfully, your console app can call Web API for confirmation.
Upvotes: 3
Reputation: 607
Here your required the background process , Background process will check your database after every one second , When background process find any new data in your table you can send your message to your User.but the question how it is possible? first of all create window services using visual studio
Upvotes: 0