Oh forgotten this: the config of the Cassini 'Service' is stored in the registry in my case. I'm using the Cassini server on my machine in a custom application which secures downloads etc. It's fairly complex but based on the Cassini source code. I've extended
Cassini because the development time to do this was much lower than creating a C++ ISAPI for IIS which can do almost the same.
Basically you create a windows service and import the cassini dll which has all the function of the server. You can just create the server in the service onstart method with about two line of code. Server server = new Server("80", "/", "C:\\wwwroot"); server.Start();
This should work assuming you have imported the namespace. You can of course modify this to load the settings from somewhere rather then having them hardcoded into the service David Legg
Here is my code for the service. just complie it as a regular exe file and make sure to reference the dll with the cassini functionality in it.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.ServiceProcess;
using System.Resources;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
namespace Cassini
{
///
/// Summary description for Class.
///
public class Service : System.ServiceProcess.ServiceBase
{
private static bool HTTPauto;
private static string HTTPvroot;
private static string HTTPproot;
private static int HTTPport;
private Server httpserver;
private System.ComponentModel.Container components = null;
public Service()
{
InitializeComponent();
}
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Cassini";
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
protected override void OnStart(string[] args)
{
readconfig();
if (HTTPauto)
{
HTTPStart();
}
}
private void readconfig()
{
HTTPauto = bool.Parse(System.Configuration.ConfigurationSettings.AppSettings["autostarthttp"].ToString());
HTTPproot = System.Configuration.ConfigurationSettings.AppSettings["HTTPproot"].ToString();
HTTPvroot = System.Configuration.ConfigurationSettings.AppSettings["HTTPvroot"].ToString();
HTTPport = int.Parse(System.Configuration.ConfigurationSettings.AppSettings["HTTPport"].ToString());
}
private void HTTPStart()
{
try
{
httpserver = new Server(HTTPport, HTTPvroot, HTTPproot);
httpserver.Start();
}
catch
{
}
}
private System.ServiceProcess.ServiceController serviceController1;
private void Stop()
{
this.serviceController1 = new System.ServiceProcess.ServiceController();
this.serviceController1.ServiceName = "Cassini";
this.serviceController1.Stop();
}
protected override void OnStop()
{
try
{
if (httpserver != null)
{
httpserver.Stop();
httpserver = null;
}
}
catch
{
}
}
}
}
You will need to create an appropriate configuration file to match the data read from it in the above code.
You will need to install the service to test it by using the InstallUtil program. If you have any problems just ask me. David Legg
Thanks David... I didn't think it would be that simple !!! I figured there would be more gotchas than that... I have ported it to VB, I'll post that version of the code back for the community in case someone else needs it. Thanks again
imports System
imports System.Collections
imports System.ComponentModel
imports System.Data
imports System.Diagnostics
imports System.Globalization
imports System.ServiceProcess
imports System.Resources
imports System.IO
imports System.Reflection
imports System.Text.RegularExpressions
imports System.Threading
imports System.Xml
imports System.Xml.Serialization
namespace Cassini
public class Service
inherits System.ServiceProcess.ServiceBase
Dim HTTPauto As boolean
Dim HTTPvroot As string
Dim HTTPproot As string
Dim HTTPport As integer
Dim httpserver As server
Private components As New System.ComponentModel.Container
public sub new()
InitializeComponent()
End Sub
Public Shared Sub Main()
Dim ServicesToRun As System.ServiceProcess.ServiceBase()
ServicesToRun = new System.ServiceProcess.ServiceBase() { new Service() }
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
private sub InitializeComponent()
components = new System.ComponentModel.Container()
ServiceName = "Cassini"
End Sub
Public Overloads Sub Dispose()
if Not components Is Nothing Then
components.Dispose()
End If
End Sub
Public Overloads Sub OnStart()
readconfig
if (HTTPauto) Then
HTTPStart
End If
End Sub
private sub readconfig
HTTPauto = boolean.Parse(System.Configuration.ConfigurationSettings.AppSettings("autostarthttp").ToString())
HTTPproot = System.Configuration.ConfigurationSettings.AppSettings("HTTPproot").ToString()
HTTPvroot = System.Configuration.ConfigurationSettings.AppSettings("HTTPvroot").ToString()
HTTPport = integer.Parse(System.Configuration.ConfigurationSettings.AppSettings("HTTPport").ToString())
End Sub
private Sub HTTPStart
try
httpserver = new Server(HTTPport, HTTPvroot, HTTPproot)
httpserver.Start()
catch
End try
End Sub
private serviceController1 As System.ServiceProcess.ServiceController
public Sub Stopsvc()
serviceController1 = new System.ServiceProcess.ServiceController()
serviceController1.ServiceName = "Cassini"
serviceController1.Stop()
End Sub
Public Overloads Sub OnStop()
try
if Not (httpserver Is Nothing) Then
httpserver.Stop()
httpserver = nothing
End if
catch
End try
End Sub
End Class
End namespace
I'm a bit concerned about having to rename the stop sub, when i compiled it it said something about it being a reserved keyword. I hope this does not make it impossible to stop the service!! Try it and tell me. David
Legg
you beat me to the punch !!! Thanks.. I will try yours and let you know how it works... it will be later this evening before I get some time on this project ... thanks again.
paul1664
Member
170 Points
36 Posts
Cassini as a Windows Service
Aug 08, 2003 08:28 AM|LINK
bdesmet
Star
8255 Points
1651 Posts
Re: Cassini as a Windows Service
Aug 14, 2003 02:11 AM|LINK
Visit www.msdn.be, www.bartdesmet.net
bdesmet
Star
8255 Points
1651 Posts
Re: Cassini as a Windows Service
Aug 14, 2003 02:13 AM|LINK
Visit www.msdn.be, www.bartdesmet.net
geekmeister
Member
87 Points
21 Posts
Re: Cassini as a Windows Service
Apr 16, 2004 05:26 PM|LINK
imleggy
Contributor
2680 Points
536 Posts
Re: Cassini as a Windows Service
Apr 17, 2004 06:15 PM|LINK
Imsoccrman
Contributor
5220 Points
1044 Posts
Re: Cassini as a Windows Service
Apr 23, 2004 06:56 PM|LINK
Senior Consultant
SharePoint Forums
SharePoint Architecture
imleggy
Contributor
2680 Points
536 Posts
Re: Cassini as a Windows Service
Apr 23, 2004 08:10 PM|LINK
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Globalization; using System.ServiceProcess; using System.Resources; using System.IO; using System.Reflection; using System.Text.RegularExpressions; using System.Threading; using System.Xml; using System.Xml.Serialization; namespace Cassini { /// /// Summary description for Class. /// public class Service : System.ServiceProcess.ServiceBase { private static bool HTTPauto; private static string HTTPvroot; private static string HTTPproot; private static int HTTPport; private Server httpserver; private System.ComponentModel.Container components = null; public Service() { InitializeComponent(); } static void Main() { System.ServiceProcess.ServiceBase[] ServicesToRun; // More than one user Service may run within the same process. To add // another service to this process, change the following line to // create a second service object. For example, // // ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()}; // ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service() }; System.ServiceProcess.ServiceBase.Run(ServicesToRun); } private void InitializeComponent() { components = new System.ComponentModel.Container(); this.ServiceName = "Cassini"; } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } protected override void OnStart(string[] args) { readconfig(); if (HTTPauto) { HTTPStart(); } } private void readconfig() { HTTPauto = bool.Parse(System.Configuration.ConfigurationSettings.AppSettings["autostarthttp"].ToString()); HTTPproot = System.Configuration.ConfigurationSettings.AppSettings["HTTPproot"].ToString(); HTTPvroot = System.Configuration.ConfigurationSettings.AppSettings["HTTPvroot"].ToString(); HTTPport = int.Parse(System.Configuration.ConfigurationSettings.AppSettings["HTTPport"].ToString()); } private void HTTPStart() { try { httpserver = new Server(HTTPport, HTTPvroot, HTTPproot); httpserver.Start(); } catch { } } private System.ServiceProcess.ServiceController serviceController1; private void Stop() { this.serviceController1 = new System.ServiceProcess.ServiceController(); this.serviceController1.ServiceName = "Cassini"; this.serviceController1.Stop(); } protected override void OnStop() { try { if (httpserver != null) { httpserver.Stop(); httpserver = null; } } catch { } } } }You will need to create an appropriate configuration file to match the data read from it in the above code. You will need to install the service to test it by using the InstallUtil program. If you have any problems just ask me. David LeggImsoccrman
Contributor
5220 Points
1044 Posts
Re: Cassini as a Windows Service
Apr 26, 2004 03:44 PM|LINK
Senior Consultant
SharePoint Forums
SharePoint Architecture
imleggy
Contributor
2680 Points
536 Posts
Re: Cassini as a Windows Service
Apr 26, 2004 06:14 PM|LINK
imports System imports System.Collections imports System.ComponentModel imports System.Data imports System.Diagnostics imports System.Globalization imports System.ServiceProcess imports System.Resources imports System.IO imports System.Reflection imports System.Text.RegularExpressions imports System.Threading imports System.Xml imports System.Xml.Serialization namespace Cassini public class Service inherits System.ServiceProcess.ServiceBase Dim HTTPauto As boolean Dim HTTPvroot As string Dim HTTPproot As string Dim HTTPport As integer Dim httpserver As server Private components As New System.ComponentModel.Container public sub new() InitializeComponent() End Sub Public Shared Sub Main() Dim ServicesToRun As System.ServiceProcess.ServiceBase() ServicesToRun = new System.ServiceProcess.ServiceBase() { new Service() } System.ServiceProcess.ServiceBase.Run(ServicesToRun) End Sub private sub InitializeComponent() components = new System.ComponentModel.Container() ServiceName = "Cassini" End Sub Public Overloads Sub Dispose() if Not components Is Nothing Then components.Dispose() End If End Sub Public Overloads Sub OnStart() readconfig if (HTTPauto) Then HTTPStart End If End Sub private sub readconfig HTTPauto = boolean.Parse(System.Configuration.ConfigurationSettings.AppSettings("autostarthttp").ToString()) HTTPproot = System.Configuration.ConfigurationSettings.AppSettings("HTTPproot").ToString() HTTPvroot = System.Configuration.ConfigurationSettings.AppSettings("HTTPvroot").ToString() HTTPport = integer.Parse(System.Configuration.ConfigurationSettings.AppSettings("HTTPport").ToString()) End Sub private Sub HTTPStart try httpserver = new Server(HTTPport, HTTPvroot, HTTPproot) httpserver.Start() catch End try End Sub private serviceController1 As System.ServiceProcess.ServiceController public Sub Stopsvc() serviceController1 = new System.ServiceProcess.ServiceController() serviceController1.ServiceName = "Cassini" serviceController1.Stop() End Sub Public Overloads Sub OnStop() try if Not (httpserver Is Nothing) Then httpserver.Stop() httpserver = nothing End if catch End try End Sub End Class End namespaceI'm a bit concerned about having to rename the stop sub, when i compiled it it said something about it being a reserved keyword. I hope this does not make it impossible to stop the service!! Try it and tell me. David LeggImsoccrman
Contributor
5220 Points
1044 Posts
Re: Cassini as a Windows Service
Apr 26, 2004 06:53 PM|LINK
Senior Consultant
SharePoint Forums
SharePoint Architecture