Using a Windows service in your .NET project part 4: the consumer layer

Introduction

In the previous post of this series we built the application service of the demo application. We’re now ready to build the consumer layer.

For demo purposes we’ll only build a Console-based consumer where the user can insert the URLs to run and monitor the progress. In reality the consumer layer would most likely be an MVC application which can then be refreshed periodically to get the latest status. Alternatively you can turn to web sockets with SignalR to push the updates to the screen. In our example, however, a console app will suffice to reach the goal of using a Windows service.

We’ll build upon the demo app we’ve been using so far so have it ready in Visual Studio

The consumer layer

Add a new Console application to the demo solution called Demo.DemoConsumer. Add a reference to all other layers in the solution:

  • Repository
  • ApplicationService
  • Domain
  • Infrastructure

First we’ll need a method that builds an IHttpJobService that the Console can work with:

private static IHttpJobService BuildHttpJobService()
{
	IConfigurationRepository configurationRepository = new ConfigFileConfigurationRepository();
	IDatabaseConnectionSettingsService dbConnectionSettingsService = new HttpJobDatabaseConnectionService(configurationRepository);
	IJobRepository jobRepository = new JobRepository(dbConnectionSettingsService);
	IHttpJobService httpJobService = new HttpJobService(jobRepository);
	return httpJobService;
}

There’s nothing magic here I hope. We simply build an IHttpJobService from various other ingredients according to their dependencies.

Then we’ll build a simple console UI to type in the URLs to run. Insert the following method to Program.cs where the user can enter URLs and break the loop with an empty string:

private static List<Uri> EnterUrlJobs()
{
	HttpJob httpJob = new HttpJob();
	List<Uri> uris = new List<Uri>();
	Console.WriteLine("Enter a range of URLs. Leave empty and press ENTER when done.");
	Console.Write("Url 1: ");
	string url = Console.ReadLine();
	uris.Add(new Uri(url));
	int urlCounter = 2;
	while (!string.IsNullOrEmpty(url))
	{
		Console.Write("Url {0}: ", urlCounter);
		url = Console.ReadLine();
		if (!string.IsNullOrEmpty(url))
		{
			uris.Add(new Uri(url));
			urlCounter++;
		}
	}		
			
	return uris;
}

The following method will insert a new HttpJob and return the insertion response:

private static InsertHttpJobResponse InsertHttpJob(List<Uri> uris, IHttpJobService httpJobService)
{
	InsertHttpJobRequest insertRequest = new InsertHttpJobRequest(uris);
	InsertHttpJobResponse insertResponse = httpJobService.InsertHttpJob(insertRequest);
	return insertResponse;
}

Finally we need a method to monitor the job and print the job status on the screen:

private static void MonitorJob(Guid correlationId, IHttpJobService httpJobService)
{
	GetHttpJobRequest getJobRequest = new GetHttpJobRequest() { CorrelationId = correlationId };
	GetHttpJobResponse getjobResponse = httpJobService.GetHttpJob(getJobRequest);
	bool jobFinished = getjobResponse.Job.Finished;
	while (!jobFinished)
	{
		Console.WriteLine(getjobResponse.Job.ToString());
		getjobResponse = httpJobService.GetHttpJob(getJobRequest);
		jobFinished = getjobResponse.Job.Finished;
		if (!jobFinished)
		{
			Thread.Sleep(2000);
			Console.WriteLine();
		}
	}
	getjobResponse = httpJobService.GetHttpJob(getJobRequest);
	Console.WriteLine(getjobResponse.Job.ToString());
}

We keep extracting the updated HttpJob until it has reached the Finished status. We wait for 2 seconds between each iteration. After the loop we want to print the final job status.

The methods can be called as following from Main:

static void Main(string[] args)
{
	List<Uri> uris = EnterUrlJobs();
	IHttpJobService httpJobService = BuildHttpJobService();
	InsertHttpJobResponse insertResponse = InsertHttpJob(uris, httpJobService);
	MonitorJob(insertResponse.JobCorrelationId, httpJobService);

	Console.WriteLine("Main finishing, press any key to exit...");
	Console.ReadKey();
}

Run this code to see it in action. You’ll be able enter a number of URLs first. Note that there’s no extra validation so be precise and enter the full address, like “https://www.google.com&#8221;. When you’re done just press enter without entering a URL. The monitoring phase will begin:

Initial Program output with no runner yet

There’s of course nothing that carries out the job yet so the while loop never exits in MonitorJob.

We’ll start building the Windows service in the next post.

View the list of posts on Architecture and Patterns here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

2 Responses to Using a Windows service in your .NET project part 4: the consumer layer

  1. Pingback: Architecture and patterns | Michael's Excerpts

  2. Pingback: Architecture and patterns | Michael's Excerpts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: