Question

I'm trying to create a windows service that will take some information from a website every 5 minutes and place in a database.

I'm using Visual Studio 2013, writing the code in C# and the database is located in SQL Server 2008 r2.

In my service, I've written in a text document that updates with the website information for testing purposes. I know the code I've written for it is fine and will do what I want because I put it into an online C# compiler and I'm getting exactly what I'm looking for. The problem is that when I try to replicate this in Visual Studio and start my service it will either:

A) Start, but will not add any information what-so-ever
B) Start and Stop immediately

The code I'm using is:

WebClient web = new WebClient();
String html = web.DownloadString("http://www.nalcorenergy.com/hydro/scrape1.asp");


Using the above code, I'm able to get the exact number I'm looking for and it appears in the online compiler. Once I plug this code in the service it doesn't have the same effect.
I'm not sure if this matters or not, but I have tried putting this code in both the OnStart() method and ElapsedEventHandler() portions and it makes no difference.

If anyone would be able to give me any advice on this matter it would be greatly appreciated!

Thanks for your time!

<-----------------------------------------------------EDIT------------------------------------------------------->
Here's my entire code that I'm using:

using System.IO;
using System.Timers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Net;


namespace WinServiceSample
{
public partial class ScheduledService : ServiceBase
{

    Timer timer;
    WebClient web;
    String html;


    public ScheduledService()
    {
        InitializeComponent();

        web = new WebClient();
        timer = new Timer(60000);
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);

    }

    void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        html = web.DownloadString("http://nalcorenergy.com/hydro/scrape1.asp");
        TraceService(html + DateTime.Now);
    }

    protected override void OnStart(string[] args)
    {

        TraceService("start service " + DateTime.Now);

        timer.Enabled = true;



    }

    protected override void OnStop()
    {
        timer.Enabled = false;
        TraceService("stopping service " + DateTime.Now);
    }

    private void TraceService(string content)
    {

        FileStream fs = new FileStream(@"C:\Users\cbsfiander\Desktop\Services.txt", FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter sw = new StreamWriter(fs);
        sw.BaseStream.Seek(0, SeekOrigin.End);
        sw.WriteLine(content);
        sw.Flush();
        sw.Close();
    }
}
}
Was it helpful?

Solution 2

Well I feel like an idiot now lol.

I changed the original code a little bit and used an eventlog to store the information rather than output the data to a text file. I also changed the proxy settings in the App.config file for the service. Here is the new edited code:

using System.IO;
using System.Timers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Web;

namespace WinServiceSample
{
public partial class ScheduledService : ServiceBase
{

    Timer timer;

    public ScheduledService()
    {
        InitializeComponent();

        if (!System.Diagnostics.EventLog.SourceExists("MyLogSrc"))
        {
            System.Diagnostics.EventLog.CreateEventSource("MyLogSrc", "MyLog");
        }

        myEventLog.Source = "MyLogSrc";
        myEventLog.Log = "MyLog";
        timer = new Timer(10000);
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);

    }

    void timer_Elapsed(object sender, ElapsedEventArgs e)
    {

        WebClient wc = new WebClient();
        string reply = wc.DownloadString("http://www.nalcorenergy.com/hydro/scrape1.asp");
        myEventLog.WriteEntry(reply);

    }

    protected override void OnStart(string[] args)
    {
        myEventLog.WriteEntry("Its Started");


        timer.Enabled = true;

    }

    protected override void OnStop()
    {
        timer.Enabled = false;

        myEventLog.WriteEntry("She no go");
    }



}
}

And now it works like a charm. Thanks for all your responses!!

OTHER TIPS

You'll probably want to implement a timer and have that call the code to download the HTML and not in OnStart()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top