Control Your Windows Service from a Asp.NET Web App, Start, Stop, Run A Method

 

Maybe you find the need to; or you think of the possibilities of controlling a Windows Service from a Web App might be useful.  For me I like the idea of writing a Windows Service that does some server function but can be controlled or called by a user. This is a more detailed post on code examples from the previous post with some other considerations.

Examples Reasons?:

  1. Maybe a Administrator needs to start and stop services but doesn’t have access to the server by normal methods.
  2. A user that calls a Windows Service that performs some type of Emailing functionality such as a notifying users when they have a new Task.  Maybe this table is populated by many means. Win service would check the database every 30min and send off emails.  In your web app, your users are special, so every time they assign a task, you fire off the windows service right there and then. Or maybe sometimes you have the need to fire off a manual send and have written redundant code to do it.
  3. Windows service control center for developers? Or Support users?

 

Anyway, the process is quite easy.

 

Windows Service code, somewhere have a method like this…

Code Snippet
  1. Protected Overrides Sub OnCustomCommand(ByVal command As Integer)
  2.     MyBase.OnCustomCommand(command)
  3.     Select Case command
  4.         Case 130
  5.             'Call one of your other methods from here
  6.         Case 150
  7.             'Call some other method or command
  8.         Case Else
  9.             'Do Nothing
  10.     End Select
  11. End Sub

You can only call one CustomCommand, so you send in Command Integers and from there decided what you want to do.

 

In your ASP.Net app on a button click event..

Code Snippet
  1. Using sc As System.ServiceProcess.ServiceController = New System.ServiceProcess.ServiceController()
  2.     sc.MachineName = "Mastro-PC"
  3.     sc.ServiceName = "ComplyTrackMainTimer"
  4.     sc.ExecuteCommand(130)
  5.     'sc.Stop()
  6.     'sc.Start()
  7. End Using

This will work on a web.app on a button click event. You have to set the MachineName and the ServiceName. You can also Stop() and Start() the Service or look up the Status.

You can get the MachineName of the server by opening a command prompt from the server and typing in “HostName”

image

You can get the ServiceName by opening up the server and looking at the name in Services. Don’t use the Display Name use the Service Name.

image

I have a previous post on this with more detailed information on valid Command Integers and Security.

Cyclomatic and Maintenance Complexity using CodeRush per Mark Miller

 

I’ve been using DevExpress’s Visual Studio Tool, CodeRush, for awhile now and I love it.

There’s been a feature though I was never familiar with, and that’s the little digits next to my methods. Choices of Cyclomatic Complexity, Maintenance Complexity and Line Count. Granted I figured out what Line Count was. 

image

So I did some research and found that Mike Miller from DevExpress has a Blog over there and goes over some CodeRush features that I found helpful.  I don’t know where his post on the Complexity count went but he did mention to another user on a forum post that there was this PDF which he describes the Complexity numbers.

Attachment Reference:

 

This has been helpful in alerting to me when I need to refractor my code and make changes, and break it out.

Here’s the charts from the PDF, which is the meat of it.

Cyclomatic Complexity

Probably the simplest measure of complexity. CC is the number of decision points for a method plus one. It also happens to represent the minimum number of test cases needed to travel through all branches of a method.

image

 

Maintenance Complexity

The purpose of Maintenance Complexity is to give you a picture of how much code you have in a given member.

 

image

 

Maintenance Complexity Points

Breakdown of how the Maintenance is counted

image

image

image

Configure TFS 2010 Email Server, Enable Email Alerts

 

If you get the error, “Sending email is not enabled”, its because you haven't configured this yet.

You can configure email settings using following command. 

TFSConfig ConfigureMail 
/FromEmailAddress:NoReplyTeamSystem@domain.com
/SmtpHost:DomainMailServer

 

Or just do it via the TFS Administrator Tool which you can also Enable Email Alerts.

 

image

$35 laptop prototype or something from India coming soon! (Update)

 

India is about to put out a computer that’s only $35!! Crazy!  Then drop the price to $10 a unit as it sells. Don’t know what the stats are other then it’s a Linux based machine but I’d buy 10 of them.

From the picture it kind of looks like a wallet and flexible.  Who’s been smudging up my screen!

“India has unveiled a $35 computer prototype as part of its program to provide connectivity to its students and teachers at affordable prices.”

“The ministry said the price would gradually fall to $10 a piece.”

“The Linux-based computer is equipped with an Internet browser, a PDF reader and several other facilities…”

Source: http://www.cnn.com/2010/WORLD/asiapcf/07/23/india.thirty.five.dollar.laptop/

 

Indian government minister Kapil Sibal unveils a prototype of the $35 computer.

 

Update:

Looks like it’s still in progress and more legit then the original post. http://news.cnet.com/8301-17938_105-20013370-1.html

 

  • OS: It was changed from Linux to Android.
  • Virtual Keyboard
  • Camera
  • Full Video Capability
  • Wi-Fi
  • 2GB Ram
  • Scheduled for Mid-2011

When Faced with the Facts, You’ll hold on to your misbelieves even more.

 

This interesting study, I wanted to post. As when I talk to someone about politics or religion, it’s interesting how when faced with factual information, someone’s views will not change.

"In a series of studies in 2005 and 2006, researchers at the University of Michigan found that misinformed people, particularly political partisans, rarely changed their minds when exposed to corrected facts in news stories. In fact, they often became even more strongly set in their beliefs. Facts, they found, were not curing misinformation. Like an underpowered antibiotic, facts could actually make misinformation even stronger."

 

“…we base our opinions on beliefs and when presented with contradictory facts, we adhere to our original belief even more strongly. “

nCore.cc Invite? Anyone?

Does anyone have an nCore.cc invite they can send me?  I don’t speak Hungarian, but I love BitTorrent sites.  Seems this one is invite only, and of course you have to know someone in the circle to get an invite, so no access for me.  Reminds me of Oink.cd in the old days before they were taken down, was invite only and a great site.  Demonoid is as well, but at least I was able to get in eventually.  Though lately I find myself using Kickasstorrents.com for everything. I do like RLSLog, I’m glad they are back online. 

Silverlight Timeout Issues from WCF… exceeded the allotted timeout.

 

So you’re getting error message…

The HTTP request to <URL To WCF Service> has exceeded the allotted timeout. The time allotted to this operation may have been a portion of a longer timeout.

 

Three places to set time values to fix this issue…

 

1) Web.Config

<httpRuntime executionTimeout="600" />

(this is seconds, so here it’s 10min).   More info on httpRuntime here.

2) On your Web.Config Binding Elements

<binding name="customBinding123" 
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
openTimeout="00:10:00"
closeTimeout="00:10:00" />

3) On your ServerReferences.ClientConfig binding elements within the system.serviceModel

<binding name="CustomBinding" 
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
openTimeout="00:10:00"
closeTimeout="00:10:00" />

All your comments are belong to us

 

Yes, I can speak english.  That statement was a play off a famous game that was poorly localized to english and that was the phrase they used, “All your base are belong to us

I had so many spam/flood comments in my blog that I’ve wipe them ALL!! So I’m sorry for those of you that had valid comments.  I’ve added captcha so hopefully that will slow that process down a whole lot.

So again, sorry for those of you that had your real comments wiped. Clean slate time.