"The day you stop learning SharePoint is the day you stop using it."

How to debug Windows Service in Visual Studio 2010

Most of the developers are unaware of the Visual Studio capability to debug Windows Service. On most of the blogs and forums I found common solutions speaking about attaching a debugger/process to the Windows Service.

While working on a SharePoint project my team came across a requirement to develop a Windows Service that consumes a SharePoint Web Service “Lists.asmx”. This was basically required to access the SharePoint list and its items via Windows Service.


As Windows Service does not have an UI, it’s a bit tricky to debug it. In this article I have explained how you can debug a Windows Service in Visual Studio 2010.


Steps:
Right click on the method that you want to debug and select Create Unit Test.
This will automatically create a Test Project in the solution explorer. Open the UnitTest1.cs and have a look at the code. This code can be manually developed but if Visual Studio can do it for you at a click of a button, it’s great! You may save your precious time for development, isn’t it?

Code Snippet generated by Visual Studio 2010 in UnitTest1.cs file:

using System;
using ServiceToUpdateList;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestProject1
{
    [TestClass()]
    public class Service1Test
    {
        private TestContext testContextInstance;
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes

        #endregion

        [TestMethod()]
        public void ReadingFromEmployeeDetailsListTest()
        {
Service1 target = new Service1 ();                 target.ReadEmployeeDetails ();          
        }

Service1 is my class name and ReadEmployeeDetails is my function name that I want to debug.

Attach breakpoint in your code file. Build your solution and click on Debug Tests in Current Context icon on the Test Tools toolbar. This will execute your code in debug mode in Visual Studio IDE.

Ensure that Test Tools is displayed on the toolbar, if not displayed go to View Menu -> Toolbars -> Test Tools.

Note: If you have created an App.config file in Windows Service then that App.config file must be copied to the Test Project as well. Because the Test Project will not read App.config of your Service Project, instead it reads from the Test Project.
Share:

SharePoint-StackExchange Moderator

Author's Profile

My photo
India
A SharePoint Enthusiast working as a Lead Solution Architect for an IT Software & Consulting Company in Mumbai. I believe in giving back to the community through which I also learn and develop and eventually grow as an individual and professional. This blog is a small contribution to the community where I live in and may help someone who is seeking knowledge like me.

Popular Posts

Powered by Blogger.

Contact Form

Name

Email *

Message *

Total Pageviews