Wednesday, September 9, 2015

Using Windows Scheduler and NET START/STOP to Restart a Windows Service

I recently encountered a client with a Windows Service that was slowly leaking memory. The Windows Service was developed in .NET (hence managed code) but it used third-party, unmanaged C++ DLLs from another vendor and hence the leak. There was no way to upgrade to the latest version of the vendor's unmanaged DLLs so the simply way to address the memory leak was to restart the service every three days.

.NET provides a method so an application can stop a Windows service and a method so an application can start a Windows service. .NET does not provide a Windows service restart method. A Windows service can stop itself but once stopped it does not have a means by which to start itself. Specifically the ServiceController class (assembly: System.ServiceProcess.dll, namespace: System.ServiceProcess) can manage a specific Windows Service and this class contains a Start and Stop method.

Since the service cannot restart itself, Windows Scheduler will be used to run a task that stops and starts the service, hence a Windows service restart. The command used to stop and start the server will be NET STOP and NET START respectively. Windows Scheduler will also be configured to run the task every three days.

For demonstration purposes, the Windows service acted against will be "SQL Server VSS Writer". This service was selected because it is only used when a SQL Server database is backup and and a backup device takes a backup snapshot (hence not used on my development machine). The SQL Server VSS Writer is show below where the service is started and set to automatically start:


NET START/STOP

To start the service using NET START:
1) Run a console window as an administrator:

1.1) In the Windows 10 search field enter CMD (this step will differ depending on the version of Windows you are running) and right click on "Command Prompt" and select "Run as administrator":



1.2) In the Windows 7 search field enter CMD and right click on "cmd.exe" and select "Run as administrator":



2) In order to stop the Windows service, NET STOP will be invoked.In the Console Window enter NET STOP <windows service name> which for the SQL Server VSS Writer service is as follows:



3) When NET STOP is run successfully it behaves as follows:



Starting a service is simply a matter of invoking NET START <windows service name>:



To make creating a restart task simpler, create a batch file names LeakRestart.bat and include the following two commands:


Windows Scheduler

To run Windows Task Schedule type "Task" in the Windows' search field select "Task Scheduler" or to be exact type in Taskschd.msc and select "Task Scheduler":



Additionally Task Scheduler can be reached from  Control Panel | System and Maintenance | Administrative Tools | Task Scheduler.

Task Scheduler appears as follows:



In order to create a task, click on "Create Task" from the Actions panel:


The Windows service needs to restart even if a user is not logged in. Additionally, administrative privileges are required to stop and start a service.

The user "When running the task" should be changed to a local local administrators as opposed to an active user. In order to change the user account for the task click on "Change User or Group..."



Select Object Types which displays:


In the Object Types dialog insure that only Groups is checked the click on OK which closes the "Object Types" dialog and displays the Select User or Group dialog but with "Object Types" set to Groups (see below):



From the "Select User or Group" dialog select the "Advanced" button:


The "Advanced" button displays the "Select User or Group" with advanced options showing. Click on the "Find Now" button:


Notice the local Administrators group was selected for the local machine. This is the local Administrators group because the "In Folder" column is the local machine as opposed to the domain. The selected group is the exact group we want to select. Click on "OK" which displays the less-advanced version of "the Select User or Group" dialog:



Click on OK to close the dialog thus returning to the "Create Task" dialog:



Notice in the previous screenshot that the "use the following account' is set to BUILTIN\Administrators.

Under the General tab enter:

  • Name: set value to Restart to Handle Leak
  • Checked "Run with highest privileges.

  • Under Configure for set to the current operating system (which for this example is Windows 7)

The final Create Task | General tab screen shout appear as follows:


Note: the task will fail if "Run with highest priveleges" is not set.

Select the Triggers tab from the Create Task dialog:



Select the New button to create a trigger that will run the task once every three days:


In order to create a once every three days trigger select "Daily" under "Settings":


Set "Recur every" to 3 day. Note the "Start" date/time can specified to a convenient start date and a time when it is appropriate to restart the Windows service.

Once the recurrence has be specified to Daily, recur every 3 days, click on "OK" from the New Trigger dialog which closes the New Trigger dialog:


Closing the New Trigger dialog displays the "Create Task" dialog again which now contains the newly created trigger (see below):



Select the Actions tab which is where we will specify the action which invokes the LeakRestart.bat batch file. The Create Task | Actions tab is as follows:



Click on New to create a new action via the New Action dialog:



Select "Browse..." which displays the Open dialog and from this dialog (see below) navigate to the location of the LeakRestart.bat batch file and select said file:


Click on Open closes the Open dialog return to the Create Task | Actions screen which now includes the newly created Action:




Click OK to create the task return to Create Task | Actions:

Testing

It is possible to run a task manually from Window Scheduler.in order to test of said task works. The "gotch ya" with this task is that is stops and restarts a Windows service so simply running the task does not show a change. In order to test, the batch file should be tweaked to only stop the Windows service (for testing purposes only) because this will verify if the batch file can run and has the correct permissions, etc.

The steps to test are:

1) Edit the LeakRestart.bat such that it only stops the Windows service and then save this batch file. This edit is simply a matter of placing a REM in front of the NET START command as follows:

NET STOP "SQL Server VSS Writer"
REM NET START "SQL Server VSS Writer"

2) Open Windows Scheduler and under "Task Scheduler (Local)"select "Task Scheduler Library":


Notice that the "Restart to Handle Leak" task is listed.

3) Right click on the "Restart to Handle Leak" task and select Run:



4) To verify that the task execute successfully check the Control Panel | System and Security | Administrative Tools | Services. The Windows Service should be stopped:



Notice in the Service windows that the selected Windows Service is not "Started" hence it is stopped.

In order to restore normal behavior post testing:

1) Started the Windows service from the Services dialog.

2) Remote the REM before NET START from LeakRestart.bat. The correct version of the batch file is as follows:

NET STOP "SQL Server VSS Writer"
NET START "SQL Server VSS Writer"

Monday, September 7, 2015

WPF/XAML: Specifying a Namespace in a Referenced Assembly

In the previous post it was shown how to create a class library that contained a publicly accessible resource file: Visual Studio: Making a Resource File (resx) Public Versus Internal (default), The premise of the previous post was to share a class library (assembly) containing resources with multiple projects. In order for a WPF page to make use of the resources from another assembly, the page's XAML file must contain a reference to the namespace of the assembly containing resource file.

The solution used in the previous blog posting is as follows where the below screenshot is from Visual Studio 2015's Solution Explorer:



The WPF application's MainWindow.xaml page is as follows:

<Window x:Class="EpicWPFApplication.MainWindow"
  xmlns=
      "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc=
    "http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:local="clr-namespace:EpicWPFApplication"
  xmlns:epicclasslib=
    "clr-namespace:EpicClassLibrary;assembly=EpicClassLibrary"
  mc:Ignorable="d"
  Title="MainWindow" Height="350" Width="525">
  <Grid Height="319" VerticalAlignment="Bottom">

    <TextBlock Text="{x:Static epicclasslib:ResourcesThatWouldBeSwellToShare.MotherlyAdvice}" 
                   HorizontalAlignment="Center" 
                   VerticalAlignment="Center"/>

  </Grid>
</Window>

In the previous code snippet the top line in bold face (shown below) specifies EpicClassLibrary assembly and the EpicClassLibrary assembly:

xmlns:epicclasslib=
  "clr-namespace:EpicClassLibrary;assembly=EpicClassLibrary"

A TextBox control in the application XAML makes use of a text resource exposed by EpicClassLibrary:

    <TextBlock Text="{x:Static epicclasslib:ResourcesThatWouldBeSwellToShare.MotherlyAdvice}" 
                   HorizontalAlignment="Center" 
                   VerticalAlignment="Center"/>

When run the WPF applications, it displays the following:


Sunday, September 6, 2015

Visual Studio: Making a Resource File (resx) Public Versus Internal (default)

I was developing a project that was a WPF desktop application which included various resources (text, icons, images, etc.). I knew that there was potential to develop another application that could also make use of the resources. The issue is that by default the class generated by a resource file is created with the access modifier set to internal.  Setting the class to be internal means that only files within the same assembly can see the data type defined by the class.

The project was organized as follows:



The EpicApplicationMaster solution contains two projects:
  • EpicClassLibrary: a class library (a.k.a. an assembly) to potentially be shared by multiple applications.
    • This class library contains a resource file named, ResourcesThatWouldBeSwellToShare.resx
  • EpicWPFApplication: a stock WPF application that references the EpicClassLibrary project.

The previous screenshot demonstrates two resources (ScaryPurpleDinosaur and MotherlyAdvice) that could be shared with other applications.

Within Solution Explorer it is possible to double click on the C# file, ResourcesThatWouldBeSwellToShare.designer.cs, associated with resource file, ResourcesThatWouldBeSwellToShare.resx. This reveals the C# code generated based on the resources defined:



The class defined in the previous image is specified as internal (internal class ResourcesThatWouldBeSwellToShare) which means the WPF project does not have access to any of the defined resources.

The ResourcesThatWouldBeSwellToShare.designer.cs is generated by a custom build tool that takes as input the resource file, ResourcesThatWouldBeSwellToShare.resx, and generates the corresponding C# code. To see this tool right click on ResourcesThatWouldBeSwellToShare.resx from within Solution Explorer and select the Properties item from the context menu:



Selecting Properties from the ResourcesThatWouldBeSwellToShare.resx context menu displays the following:



The value for the "Custom Tool" property is ResXFileCodeGenerator which is the tool responsible for generating the C# class with the internal access modifier. There is no way to pass a command-line value to ResXFileCodeGenerator in order to tell it to generate public as the class' access modifier.

To create a resource files with the class generated contains public as the access modifier replace the ResXFileCodeGenerator custom tool with PublicResXFileCodeGenerator:



The name of PublicResXFileCodeGenerator indicates that it creates public rather than internal classes from resx files. Building the code following the aforementioned change generates a public class:




Saturday, August 22, 2015

Visual Studio 2015: Error Adding "Shared Project" to Solution

This entry presents an error I encountered and how I solved it.

My standard development Virtual Machine (as of August 2015) is Windows 10 Enterprise and Visual Studio 2015 Professional. As part of a desktop project (a solution containing a WPF project), I attempted to add a project of type "Shared Project" and received the following error dialog:



The text of the error is as follows:
The imported project "C:\Program Files (x86)\MSBuild\Microsoft\WindowsXaml\v14.0\8.1\Microsoft.Windows.UI.Xaml.CSharp.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.C:\Program Files (x86)\MSBuild\Microsoft\WindowsXaml\v14.0\Microsoft.Windows.UI.Xaml.CSharp.targets


Notie the 8.1 in the previous path meaning Visual Studio 2015 is categorizing the Shared Project as Windows 8.1.

I found a stackoverflow.com article that pointed to the following as a way to solve the problem and it looked promising: Why can't I create Shared Project in Visual Studio 2015? and the answer that caught my attention is as follows:


To clarify what Mr. Kondrasovas is recommending, look at the following example found under the New Project dialog (from Visual Studio, File | New | Project) under Templates | Visual C# | Windows | Windows 8:


In a perfect world where developers "grow apple trees and honey bees and snow-white turtle doves" this would have solved the problem by on my machine the following error was displayed:



Then I thought back to the Visual Studio 2015 install process. I had selected the default instalation. There must be a set of Windows 8.1 categorized objects that did not install.

I was coding a desktop application that could run on Windows 8.1 or Windows 10. There was nothing that tied my project to only run on Windows 8.1. So Microsoft's categorization is odd.

So under Control Panel | Programs | Uninstall a program, I selected Visual Studio 2015. Most savvy Windows users realize to add features to a previously installed program to select "Uninstall a program" in order to display the "Uninstall or change a program" dialog as follows:


Clicking on "Microsoft Visual Studio Professional" display the following dialog:


To add the missing project templates, select "Modify" which displays:


Rather than guess at what items to check to remedy the situation, I clicked on "Select All":


Since the whole nine yards was selected, I clicked on Next:


After this I was able to create and add a Shared Project to a solution.

Revisiting the "New Project" dialog (from Visual Studio, File | New | Project) under Templates | Visual C# | Windows | Windows 8 the following is now displayed (not the missing templates have been installed):



The irony is that Shared Project template is not event listed as a Windows 8 template. The Shared Project template is founder under Templates | Visual C# | Windows:



Tuesday, August 11, 2015

VSOL: Visual Studio Online versus GitHub

Visual Studio Online (visualstudioonline.com) and GitHub (GitHub.com) both support Git repositories. For those not familiar with Git, it is a distributed version control system (see: Git (software). So the question is, which is better, Microsoft's Visual Studio Online (VSOL) or the open source community's darling, GitHub? Although both provide access to Git-based revision control the model is different -- it is like comparing apples to oranges.

If your primary goal is to share a project with the hoi polloi (such as an open source project) then GitHub makes sense. The free personal plans get unlimited collaborators and unlimited public repositories while the free organizational plans get unlimited members and unlimited public repositories. The downside to the free plans offered by GitHub (whether individual or organizational) is that private repositories are not supported. Individuals and organizations are required to spend money in order make their repositories private.

Visual Studio Online (VSOL) supports unlimited Git repositories for up to five developers at no cost (Signing up for Visual Studio Online (cloud-based Team Foundation Server)). VSOL does not have a public option such as that supported by GitHub and hence there is no way to share a Visual Studio Online project with the common people (see Pulp's "Common People", a great music video). Recall also that in addition to five free source code licenses, Visual Studio Online offers unlimited licenses to stake holders (Visual Studio Online (cloud hosted TFS): Unlimited (no cost) users can Create/Access Bugs/Tasks) which allows product managers, Q.A. engineers and tech support to take advantage of the bug tracking and project management 
This blog has previously presented the cost structure of Visual Studio Online (The cost of Visual Studio Online (cloud based TFS) and 10 Visual Studio Premium Licences for a great price). The previous post is from November 14, 2013 so it is highly likely the prices have changed. It is important to note that Visual Studio places no size restrictions on projects and does not limit the number of projects that can be created. All-in-all the prices associated with Visual Studio Online were reasonable.

GitHub pricing can be found at Plans and pricing. An individual can purchase five private repositories for $7 a month (Micro plan). An organization can purchase ten private repositories for $25 a month with unlimited members and unlimited public repositories (Bronze Plan). 

With regard to the size limitations of GitHub see "What is my disk quota?"  which is shown below:


A 1GB limit is reasonable for most software projects so the previous screenshot is not meant as a criticism but is simply information for those evaluating Visual Studio Online versus GitHub.com.

To put the two in their proper perspective consider World Vision a charity that allows people to sponsor a child for $35 a month, a child that might otherwise go hungry. For every five GitHub personal plans subscribers who select the Micro Plan ($7 a month), a child goes hungry (5 * $7 = $35). Okay, that was joke.

GitHub and Visual Studio Online are both reasonable options and are reasonably priced. My choice is Microsoft because I have access to licenses as a Gold Partner and through Microsoft's Biz Spark program. I also have confidence in Microsoft's ability to manage cloud based applications including backup. This does not mean GitHub does not do a stellar job of protecting data. Microsoft is one of the main cloud players which is an incredibly high bar.

Monday, June 8, 2015

Visual Studio 2015: Displaying Line Numbers for Files

When talking about code with another develop, line numbers are extremely handy. Visual Studio (regardless of the version) does not display line numbers in source files by default. This is demonstrated below:



In order to enable line numbers, select Options from the Tools menu:


From the Options dialog navigate to Text Editor | All Languages | General.


Check the Line Numbers check box and click OK.Voila! the files displayed in Visual Studio have line numbers as demonstrated below:



Monday, June 1, 2015

Windows 7: Finding Version of Windows and System Memory

I have a client that needs to run a virtual machine (Hyper-V) in order to solve a problem running legacy software. Before making the on-site visit to configure Hyper-V, it is necessary to know the version of Windows on the client computer and the amount of physical memory.  Hyper-V can be run on Windows 7 Professional, Enterprise and Ultimate editions. The client's computer should have at least 8 GB of physical memory. The client literally needs to open a browser Window in the operating system running via Hyper-V so 8 GB is sufficient memory. 

The steps needed to determine the version of Windows 7 running on the client's computer and the physical memory are as follows:

1) Click in the Windows button

2) This reveals the Start Menu (note Control Panel on the upper right of the Start menu):



3) Click on Control Panel revealing:



4) Click on "System and Security" which displays:



5) From the "System" category select "View amount of RAM and processor speed":


The previous screen shows the version of Windows is "Windows 7 Enterprise" and the amount of "Installed memory (RAM)" is 16.0 GB.