0

Hey guys, I'm trying to create a windows service that loads a website, navigates it, and pulls some information using javascript. This is all very easy to do in a windows forms application, but isn't working in a webservice (apparently because services can't access the registry WinInet Not Supported for Use in Services). Any ideas how to get it to work? Here's my code that outputs nothing:

volatile WebBrowser webBrowser2;

    protected override void OnStart(string[] args)
    {
        ThreadStart threadDelegate = new ThreadStart(myThread);
        Thread thread = new Thread(threadDelegate);
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
    }
    public void myThread()
    {
        webBrowser2 = new WebBrowser();
        webBrowser2.Navigate("http://www.google.com");
        webBrowser2.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webpage_loaded2);

        Thread.Sleep(60000);

        FileStream fileStream = new FileStream(@"c:\file1.txt", FileMode.Create);
        try
        {
            Byte[] info = new UTF8Encoding(true).GetBytes("services app output: " + webBrowser2.DocumentText);

            // Add some information to the file.
            fileStream.Write(info, 0, info.Length);
        }
        finally
        {
            fileStream.Close();
        }
    }

EDIT: I need a WebBrowser or WebKit.Net object because I need to execute javascript on the page, and I need to maintain a login (using cookies and post data). If there's another method to do this please let me know.

Dulini Atapattu
  • 2,735
  • 8
  • 33
  • 47
Rob
  • 7,028
  • 15
  • 63
  • 95

1 Answers1

2

You need to host your WebBrowser control in a Form and then inside your windows service start your form. WebBrowser can't do anything without a form.

protected override void OnStart(string[] args)
{
    ThreadStart threadDelegate = new ThreadStart(myThread);
    Thread thread = new Thread(threadDelegate);
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();

    //let the form start with some sleep time or whatever you want

    ActionsToExecuteInWebBrowser();
}

void myThread()
{
    Application.Run(new FormHostingWebBrowserControl());
}

void ActionsToExecuteInWebBrowser()
{
    //Whatever you want to do in the WebBrowser here
}
Arturo Martinez
  • 3,737
  • 1
  • 22
  • 35
  • This looks really interesting, do you think you could expand on this? Can I add a form to a windows service project? and How can I access the form from the services? – Rob May 24 '11 at 04:41
  • I don't remember very well, I think I created a separate Windows Form project just to create the Form and not go through the mess of generating the initialization code by my self; and then moved the files to my windows service project and changed the namespaces. After that I had all the designer benefits of Visual Studio Windows Forms inside my Windows Service project. Hope this helps. – Arturo Martinez May 24 '11 at 11:54