Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Windows Service
Message
Information générale
Forum:
ASP.NET
Catégorie:
The Mere Mortals .NET Framework
Titre:
Versions des environnements
Environment:
C# 1.1
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01002324
Message ID:
01002510
Vues:
20
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:

  1. 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

  2. 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
  3. Add the top of your Windows Service class, add the following class-level variable.

    In C#:
    public class Service1 : System.ServiceProcess.ServiceBase
    {
    	/// <summary>
    	/// Static field that contains a reference to the application object
    	/// </summary>
    	public static OakLeaf.MM.Main.mmAppBase App;
    And in VB .NET:
    Public Class Service1
       Inherits System.ServiceProcess.ServiceBase
    
       ' Static field that contains a reference to the application object
       Public Shared App As OakLeaf.MM.Main.mmAppBase
  4. 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
  5. Right-click your Windows Service project and select Add | Add Class... from the shortcut menu

  6. In the Add New Item dialog's Name text box, change the name to Factory and click Open

  7. 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
  8. 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
  9. Right-click your Windows Service project and select Add | Add Class... from the shortcut menu

  10. In the Add New Item dialog's Name text box, change the name to AppBase and click Open

  11. 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
  12. 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
  13. Add the following factory method to the new AppBase class.

    In C#:
    /// <summary>
    /// Create the application-level factory
    /// </summary>
    /// <returns>Factory object</returns>
    public override mmFactory CreateFactory()
    {
    	return new Factory();
    }
    And in VB .NET:
    ' Create the application-level factory
    Public Overrides Function CreateFactory() As mmFactory
       Return New Factory()
    End Function
  14. 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.

  15. In Windows Explorer, navigate to your Windows Service project's root folder and paste the app.config file into the folder.

  16. In the Visual Studio Solution Explorer, right-click your Windows Service project and select Add | Add Existing Item from the shortcut menu.

  17. 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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform