copyright ©2004 virtusa corporation | confidential distributed application development prepared by:...

38
Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

Upload: augustus-watts

Post on 03-Jan-2016

218 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Distributed Application Development

Prepared By: Ruwan Wijesinghe

Page 2: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

2Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Web Services

Windows Application

Client Machine

Web Server

Web Application

Web Server

Web Service 1

Web Method 1

Web Method 2

Web Service 2

Web Method 1

Web Method 2

Method Call (XML)

Result (XML)

Method Call (XML)

Result (XML)

Page 3: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

3Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Introduction

• Use XML based messages to make method calls and receive the results.

• Uses http protocol to send and receive these messages.• This make it possible to send data over firewalls.

• Data is encapsulated using standard SOAP protocol.• SOAP is an XML based messaging protocol.

• Web Services are defined using text files with the extension asmx and there accompanied code-behind files

• Web service is uniquely identified using the URL to this web service definition file (asmx file).

• Interface definition of the web service can be obtained by appending ?WSDL at the end of the URL.

E.g.: www.aaa.com/WebService1/Service1.asmx?WSDL

Page 4: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

4Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Basic Web Service

A Basic Web Service

using System.Web.Services;

[WebService(Namespace="www.virtusa.com/training")]

public class Service1 : System.Web.Services.WebService

{ private string GetHello() {

return "Hello World"; }

[WebMethod]public string HelloWorld(){

return GetHello( );}

}

This method will not be exposed as a web service method although it is public

This method will be exposed as a web service method

Defines a unique namespace identifier for XML Schemas used in WSDL. This can be anything unique. Since URL Gives a unique name for a company or an organization, it is preferred as a namespace identifier

Page 5: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

5Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Accessing a Web Service

• Web service can be accessed in three ways.• Using web browser (for testing purposes)

• Using a proxy class

• Directly using SOAP messages

• Using Web browser• Type the URL of the web service

• E.g.: http://localhost/WebService1/Services1.asmx

• This will give you an html file with links to pages which can be used to invoke each web method of the web service

Page 6: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

6Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Creating a Proxy Class

• A proxy class to use a web service can be created very easily using Visual Studio IDE.

• Right click the Reference node of Project Explorer window

• Select “Add Web Reference” menu option

• This will open the “Add Web Reference” dialog box

• Type the URL of the web service at “URL” textbox

• Type the required namespace name, into which the proxy class should be added, in “Web Reference Name’ textbox

• Click on the “Add Reference” button, once the page is loaded.

• This will create a proxy class to access the selected web service.

Page 7: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

7Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Using The Proxy Class

• We can use the proxy generated in previous slide with any windows form, web form or console application as follows.

(Lets Assume that the name of the proxy class is localhost.Service1)

localhost.Service1 s = new localhost.Service1();string st = s.HelloWorld();

Page 8: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

8Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Maintaining State

• Each request to the web method, creates a new instance of the web-service class in the server side.

• Therefore we have to use session objects to save the state information across requests

E.g.Web Methods

[WebMethod(EnableSession=true)]public int Mul(int n){ int m = (int) Session["M"]; return n*m;}

[WebMethod(EnableSession=true)]public void SetM(int m){ Session["M"] = m;}

Using Proxy

//Create a new instance of the proxylocalhost.Service1 s =

new localhost.Service1();

// Add a cookies collection to maintain the // state informations.CookieContainer = new

System.Net.CookieContainer();

// Invoke methods that use sessionss.SetM (2);int res = s.Mul (5);

Page 9: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

9Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Returning a Class 1

• Web Services can return classes as output parameters

• Visual Studio.Net IDE generates a corresponding class as client side to receive this information, together with the proxy class.

• Only the public member variables and properties of a class will be returned through web service

• The corresponding client side class will contain public member variables for both public member variables and properties (it will not contain any method).

Page 10: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

10Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Returning Classes

Class to be Returned using a web method

public class Student{

private int _id;private string _name;

public int ID {get{return _id;}set{_id = value;}

}public string Name {

get{return _name;}set{_name = value;}

}public Students() {

_id = 0; _name = "";}public Students(int id,string name){

_id = id; _name = name;}

}

Returning an instance of a class using a web method

[WebMethod]public Student GetFirstStudent(){ return new Student (5, "Ajith");}

Using the class returned

localhost.Service1 s = new localhost.Service1();localhost.Student st = s. GetFirstStudent();

Page 11: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

11Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Returning Arrays

[WebMethod]

public Students[] GetAllStudents(){

ArrayList lst = new ArrayList();lst.Add(new Student(1,"Saman"));lst.Add(new Student(2,"Ajith"));lst.Add(new Student(3,"Kamal"));return (Student[]) lst.ToArray(typeof(Student));

}

Using Arrays

localhost.Service1 s = new localhost.Service1();localhost.Student[] sts = s. GetAllStudents();

Page 12: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

12Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Returning DataSets

• Return data using DataSets

[WebMethod]

public DataSet GetAllStudents(){

DataSet ds = new DataSet("StudentInfo");oleDbDataAdapter1.Fill(ds,"Students");return ds;

}

• Receiving data from a web method

localhost.Service1 s = new localhost.Service1();

DataSet ds = s.GetAllStudents();dataGrid1.DataMember = "Students";dataGrid1.DataSource = ds;

Page 13: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

13Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Using Custom Headers

Create a class for the headerpublic class UserInfoHeader:

System.Web.Services.Protocols.SoapHeader

{public long UserID;public string UserName;

}

Add a member variable to the web service class

public UserInfoHeader _ui;

Write the methods to use this header[WebMethod][SoapHeader("UserInfo",

Direction=SoapHeaderDirection.In)]public string TestHeader(){ return _ui.UserName;}

Writing Client to access custom headers

localhost.Service1 s = new localhost.Service1();localhost.MyHeader h = new localhost.MyHeader();h.UserID = 4;h.UserName = "Ajith";s. UserInfoHeaderValue = h;string name = s.TestHeader();

Page 14: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

14Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

.Net Remoting

• Xml web services is a flexible, interoperable method of developing distributed applications which can communicate over fire walls

• Binary serialization is greatly compact and efficient compared to the xml serialization.

• Web services uses xml serialization to transfer information between computers.

• The HTML protocol (4th layer) used by web services requires sending lots of additional information over the tcp protocol (3rd layer)

• Therefore, when efficiency is an issue in distributed application development, we have to use a method which can use binary serialization and tcp protocol to transfer information.

• .Net Remoting is for designed for that

• Remote objects can be transferred over the network as value (using binary serialization) or reference

• When objects are transferred as reference, a proxy class which has the similar methods will be created in the client side, to invoke the corresponding methods in the object in the server.

• All the classes related to .Net Remoting is defined in "System.Runtime.Remoting" namespace, and namespaces inside that

Page 15: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

15Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Categorization

• Remoting objects are of two types

• Client activated objects - objects whose life time is decided by the client

• Server activated objects - objects whose life time is controlled by the server. These objects are published using a unique URL. When they published using IIS, they must have a URL with the extension ".rem" or ".soup"

• Server activated objects are of two types

• Singalton - only a one instance of the class will be created and all the method calls of all the client objects will be served by this single instance

• SingalCall - A new instance of the class will be created to serve for each request of the class (This is similar to the web services).

Page 16: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

16Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Creating Server Activated Objects

• Create a library with the name "HelloServer.dll" containing the following class.namespace HelloNS

{

public class HelloServer: MarshalByRefObject

{

public string GetHello (string your_name)

{

return string.Format( "Hello " + your_name);

}

}

}

• Create a console application assembly with the name "Server.exe" with the reference to "HelloServer.dll" and "System.Runtime.Remoting.dll" and add the following code to main method of that class.

ChannelServices.RegisterChannel (new TcpServerChannel (8028));

RemotingConfiguration.RegisterWellKnownServiceType ( typeof (HelloNS.HelloServer), "HelloServer.rem", WellKnownObjectMode.Singleton);

Console.WriteLine ("The server is listening. Press Enter to exit....");

Console.ReadLine ();

Page 17: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

17Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Using Server activated objects

• Create a console application with reference to "System.Runtime.Remoting.dll" and "HelloServer.dll"

• Add the following code to the main function.

ChannelServices.RegisterChannel(new TcpClientChannel());RemotingConfiguration.RegisterWellKnownClientType

( typeof(HelloNS.HelloServer),"tcp://localhost:8028/HelloServer.rem");

HelloNS.HelloServer obj = new HelloNS.HelloServer();string msg = obj.GetHello("Saman");Console.WriteLine(msg);Console.ReadLine();

Page 18: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

18Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Using configuration files

• Hard cording the remoting information in the program cause the code to be recompiled, whenever the system settings, server name, etc. get changed

• Therefore having this settings in a configuration file makes the configuration process easier

• We can save these settings either in the application configuration file or a separate configuration file

• Saving them in application configuration file makes the file management easier

• Application configuration file is an xml file stored in the same directory as the assembly and have the file name

<assembly name>.confige.g. if the assembly name is "ass1.exe" then the configuration file will be "ass1.exe.config". However, when working with Visual Studio.Net we have to create the file with the name "app.config" and the IDE will copy it to the correct place with the correct name, when the project is being compiled.

Page 19: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

19Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Server Configuration File

<configuration> <system.runtime.remoting> <application> <service> <wellknown mode="Singleton" type="HelloNS.HelloServer, HelloServer" objectUri="HelloServer.rem" /> </service> <channels> <channel ref="tcp" port="8028"/> </channels> </application> </system.runtime.remoting></configuration>

Page 20: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

20Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Client Configuration File

<configuration> <system.runtime.remoting> <application> <client> <wellknown type="HelloNS.HelloServer, HelloServer" url="tcp://localhost:8028/HelloServer.rem" /> </client> </application> </system.runtime.remoting></configuration>

Page 21: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

21Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Code to use Configuration files

• Server Code

RemotingConfiguration.Configure("ConsoleApplication1.exe.config");Console.WriteLine ("The server is listening. Press Enter to exit....");Console.ReadLine();

• Client Code

RemotingConfiguration.Configure("ConsoleApplication2.exe.config");

HelloServer.HelloServer obj = new HelloServer.HelloServer();string msg = obj.GetHello("Saman");Console.WriteLine(msg);Console.ReadLine();

Page 22: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

22Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Remoting using Interfaces

• The basic problem with the previous approach is that, both the client and the server should have a copy of the remoting class ("HelloServer.dll" in this case).

• This will make the development and deployment process more complicated.

• We can solve this using interfaces

Page 23: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

23Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Create IHello Interface

• Create the interface in a separate assembly, say "HelloInterface.dll"namespace HelloInterface{

public interface IHello{

string GetHello (string your_name);}

}

• Create a reference to "HelloInterface.dll" assembly in HelloServer project and implement that interface in "HelloServer" class.

public class HelloServer :MarshalByRefObject, HelloInterface.IHello{

}

Page 24: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

24Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Using IHello Interface

• Create a reference to "HelloInterface.dll" in client project (remove the reference to "HelloServer.dll" to make sure that it is not being used) and the following code to the main function

IHello obj = (IHello) Activator.GetObject ( typeof(IHello),"tcp://localhost:8028/HelloServer.rem");

string msg = obj.GetHello ("Saman");Console.WriteLine(msg);Console.ReadLine();

Page 25: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

25Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Client Activated Objects

• When we are using client activated objects, a separate instance of the object for each client (for each request for a new instance) will be created in the server.

• The lifetime of the object can be controlled by the client

• Client activated objects can maintain the sate information as they live across different requests

• Client activated objects are not published using a unique url

Page 26: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

26Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Server configuration for Client Activated object

<configuration> <system.runtime.remoting> <application> <service> <activated type = "HelloNS.HelloServer, HelloServer"/> </service> <channels> <channel ref="tcp" port="8028"/> </channels> </application> </system.runtime.remoting></configuration>

Page 27: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

27Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Using Client Activated Objects

• Client Configuration File<configuration> <system.runtime.remoting> <application> <client url="tcp://localhost:8028">

<activated type="HelloNS.HelloServer, HelloServer" /> </client> </application> </system.runtime.remoting></configuration>

• Add this code to the main function of the client projectRemotingConfiguration.Configure("ConsoleApplication2.exe.config");HelloServer.HelloServer obj = new HelloServer.HelloServer();string msg = obj.GetHello("Client");Console.WriteLine(msg);Console.ReadLine();

Page 28: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

28Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Controlling lifetime of Client Activated Objects

• Life time of an client activated control is controlled using a life time lease object.

• Life time of the object is initially set for a specific value (given in "InitialLeaseTime" property of life time lease)

• This life time can be increased by

• A method call to the object (each method call will increase the life time by the value given by "RenewOnCall" property of the life time lease)

• A call to "Renew" method of the life time lease by the client, giving the time period to extend

• Using a sponsor object provided by the client (sponsor, if exsists will be notified by the server when the life time lease expired, then the sponsor can decided whether to expand the life time some more, or not depending on the requirements. If the sponsor did not respond in the time period specified by the "SponsershieTimeout" property of the life time lease then the object will be destroyed.

• Lifetimie lease is defined in "System.Runtime.Remoting.Lifetime" namespace

Page 29: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

29Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Initialize Lifetime Lease

• Overload the Remote classes "" ("HelloServer") in this example, to initialize the lifetime lease

public override object InitializeLifetimeService(){ ILease lease = (ILease) base.InitializeLifetimeService (); if (lease.CurrentState == LeaseState.Initial) {

lease.InitialLeaseTime = TimeSpan.FromSeconds(1);lease.RenewOnCallTime = TimeSpan.FromSeconds(1);lease.SponsorshipTimeout = TimeSpan.FromSeconds(1);

} return lease;}

Page 30: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

30Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Using COM+

• COM+ is an application server which comes with Windows operating systems (Windows 2000 and above)

• COM+ supports the services like

• Distributed Transactions

• Object Pooling

• Loosely Coupled Events

• COM+ is originally designed for DCOM (Distributed COM)

• However .Net Framework class can use these COM+ services by deriving themselves from the ServicedComponent class of "System.EnterpriceServices" namespace.

• The assembly must be signed with a strong name, in order to be installed as in COM+

• Any class derived from ServicedComponent class can be marked to use COM+ services using attributes

• If a class is marked to use COM+ service, .Net Framework will automatically register it with COM+

• However, we can use "regsvcs" command line utility to register a .Net class library in COM+

• Components registered in COM+ can be managed using "Component Service" snap-in given in "Administrative Tools"

Page 31: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

31Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Serviced Component which uses Transactions

[assembly: ApplicationName ("MyComPluseApp")]

[Transaction (TransactionOption.Required)]public class Students: ServicedComponent{ private static int gObID=0; private int ObID; const string stConn = "workstation id=\"CL-WIJESINGHE\";integrated

security=SSPI;data source=\"CL-WIJESINGHE\";initial catalog=TrainingDb"; [AutoComplete (true)] public void AddStudents (string[] names) {

string stSQL = "Insert into tblStudents(Name) values(@name)";SqlConnection conn = new SqlConnection (stConn);SqlCommand cmd = new SqlCommand (stSQL,conn);cmd.Parameters.Add ("@name", SqlDbType.VarChar);conn.Open ();foreach (string nm in names){

cmd.Parameters["@name"].Value = nm;cmd.ExecuteNonQuery();

}conn.Close();

}}

Page 32: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

32Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Using Serviced Components

• We can simply use any serviced component just as we use any other class

try{

ClassLibrary1.Students st = new ClassLibrary1.Students(); st.AddStudents(new string[]{"AAA345","BBB345"});

}catch(Exception ex){

Console.WriteLine("Error: "+ex.Message);}

Page 33: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

33Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Object Pooling

Serviced Component[ObjectPooling (Enabled=true, MinPoolSize=3, MaxPoolSize=4)]public class Students: ServicedComponent{ private static int gObID=0; private int ObID;

public Students() {

Console.WriteLine("Constructor"); ObID = gObID++; }

public int GetObID() {return ObID; } protected override bool CanBePooled() { return true; }}

Code in Main Functionwhile (true){ ClassLibrary1.Students st = new ClassLibrary1.Students(); Console.WriteLine("ObID = {0}",st.GetObID()); Console.ReadLine(); st.Dispose();}

Page 34: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

34Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Windows Services

• Windows Service is a windows application which is designed to run in back ground and can be controlled using Windows Service Manager (using "Services" snap-in of "Administrative Tools").

• They are used to implement back ground services, which listens to sockets (e.g IIS, ftp), monitor changes to the files, etc.

• They can be used to host .Net Remoting components as well

• Windows Services has to be registered with Windows Service Manager before being used

• .Net installation utility "installutil" (This can be used to install any resource like, event log, Message Queue, Performance Counter etc.) can be used to register windows services.

• We have to create a project of type "Windows Service" in order to create a windows service using Visual Studio .Net

Page 35: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

35Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Creating a Windows Service 1

public class Service1 : System.ServiceProcess.ServiceBase{ private Timer tm; private EventLog elog;

public Service1() { tm = new Timer(1000); elog = new EventLog ("Application",".","My Svc"); tm.Elapsed += new ElapsedEventHandler(tm_Elapsed); }

static void Main() { System.ServiceProcess.ServiceBase.Run (new Service1()); }

Page 36: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

36Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Creating a Windows Service 2

protected override void OnStart (string[] args){ tm.Start(); elog.WriteEntry ("Starting my Service", EventLogEntryType.Information);}

protected override void OnStop (){ tm.Stop(); elog.WriteEntry ("Stopping my Service", EventLogEntryType.Information);}

private void tm_Elapsed (object sender, ElapsedEventArgs e){ elog.WriteEntry ("My Tic", EventLogEntryType.Information);}

Page 37: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

37Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Creating the Installer

• We have to create an installer class to install the windows service and the event log source

• We need two classes to install the windows service (ServiceInstaller and ServiceProcessInstaller) and another class to install event log soucre (EventLogInstaller)

public class MyInstaller: Installer{ private ServiceInstaller sIns; private ServiceProcessInstaller spIns; private EventLogInstaller elIns;

public MyInstaller():base() { sIns = new ServiceInstaller(); sIns.DisplayName = "My Test Service"; sIns.ServiceName = "MyService1"; sIns.StartType = ServiceStartMode.Manual; this.Installers.Add(sIns);

spIns = new ServiceProcessInstaller(); spIns.Account = ServiceAccount.LocalSystem; this.Installers.Add(spIns);

elIns = new EventLogInstaller(); elIns.Log = "Application"; elIns.Source = "My Svc"; this.Installers.Add(elIns); }}

Page 38: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Distributed Application Development Prepared By: Ruwan Wijesinghe

38Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Installing the Service

• Install the seviceinstallutil WindwsService1.exe

• Un-install the serviceinstallutil /u WindwsService1.exe

• Starting the service

• Open "Service" snap-in in the "Administrative Tools"

• Select the correct service name

• Right Click the service and select "Start" menu option