Mike,
>I'm making a Windows Service app, and would like to use some stuff in MM. In particular, the GenericFactory and AppSettingsManager and other goodies within mmAppBase.
>
>Is there a method to wire up a Windows Service so it becomes a MM application?
This takes several steps, but it's not too difficult (we'll add a new Windows Service template to a future version of MM .NET to make this easier). But for now, here's what you need to do:
- Right-click your Windows Service project, and select Add Reference... from the shortcut menu. In the .NET tab of the Add Reference dialog, select the following assemblies from the list, click Select, then click OK to add them to your project.
- Mere Mortals Framework Interfaces
- Mere Mortals Framework
- Mere Mortals Framework Automation
- In the Solution Explorer, right-click your Windows Service class and select View Code from the shortcut menu. At the top of your Windows Service source code file, add a reference to the following namespace:
In C#:using OakLeaf.MM.Main;
And in VB.NET:Imports OakLeaf.MM.Main
- Add the top of your Windows Service class, add the following class-level variable.
In C#:public class Service1 : System.ServiceProcess.ServiceBase
{
public static OakLeaf.MM.Main.mmAppBase App;
And in VB .NET:Public Class Service1
Inherits System.ServiceProcess.ServiceBase
Public Shared App As OakLeaf.MM.Main.mmAppBase
- At the top of your Windows Service Main() method, add the following code.
In C#:App = new AppBase();
And in VB . NET:App = New AppBase
- Right-click your Windows Service project and select Add | Add Class... from the shortcut menu
- In the Add New Item dialog's Name text box, change the name to Factory and click Open
- Add the following namespace references to the top of the new source code file.
In C#:using OakLeaf.MM.Main.Security;
using OakLeaf.MM.Main.Managers;
And in VB .NET:Imports OakLeaf.MM.Main.Security
Imports OakLeaf.MM.Main.Managers
- Change the base class of the new Factory class to OakLeaf.MM.Main.Patterns.mmFactory.
In C#:public class Factory : OakLeaf.MM.Main.Patterns.mmFactory
{
}
And in VB .NET:Public Class Factory
Inherits OakLeaf.MM.Main.Patterns.mmFactory
End Class
- Right-click your Windows Service project and select Add | Add Class... from the shortcut menu
- In the Add New Item dialog's Name text box, change the name to AppBase and click Open
- Add the following namespace references to the top of the new class source code file.
In C#:using OakLeaf.MM.Main;
using OakLeaf.MM.Main.Windows.Forms;
using OakLeaf.MM.Main.Patterns;
And in VB .NET:Imports OakLeaf.MM.Main
Imports OakLeaf.MM.Main.Windows.Forms
Imports OakLeaf.MM.Main.Patterns
- Change the base class of the new AppBase class to mmAppDesktop.
In C#:public class AppBase: mmAppBase
{
}
And in VB .NET:Public Class Factory
Inherits mmAppBase
End Class
- Add the following factory method to the new AppBase class.
In C#:
public override mmFactory CreateFactory()
{
return new Factory();
}
And in VB .NET:
Public Overrides Function CreateFactory() As mmFactory
Return New Factory()
End Function
- Finally, go to Windows Explorer (not Solution Explorer), and navigate to the C:\Program Files\Mere Mortals .NET Framework 2003\VC#\Templates\Windows Forms Template folder. Right-click the app.config file and select Copy from the shortcut menu.
- In Windows Explorer, navigate to your Windows Service project's root folder and paste the app.config file into the folder.
- In the Visual Studio Solution Explorer, right-click your Windows Service project and select Add | Add Existing Item from the shortcut menu.
- At the bottom of the Add Existing Item dialog, set the Files of type combo box to All Files (*), then select the app.config file and click Open.
To test that your setup works, go to your Windows Service Main() method and add the following code directly below the line that instantiates the application object.
In C#:
App = new mmAppBase();
string Connection = mmAppBase.DatabaseMgr.GetConnectionString("Northwind");
And in VB .NET:
App = New mmAppBase()
Dim Connection As String = mmAppBase.DatabaseMgr.GetConnectionString("Northwind")
Set a breakpoint on this new line of code and run your Windows Service. When you step into the test code in the debugger, it should store the Northwind database connection string found in the app.config file into the Connection variable.
Regards,
Kevin McNeish
Eight-Time .NET MVP
VFP and iOS Author, Speaker & Trainer
Oak Leaf Enterprises, Inc.
Chief Architect, MM Framework
http://www.oakleafsd.com