Click here to Skip to main content
Click here to Skip to main content

My First COM+ Serviced Component

, 1 Nov 2009
Rate this:
Please Sign up or sign in to vote.
Just sharing my experience from six years ago, in 2003, when I was working as a developer.

Introduction

In this Internet world of connected network applications, it is reasonable to expect that your Web applications scale to meet the needs of millions of users who will access your Web site. COM+ is a core element in the Windows DNA 2000 methodology to create scalable, extensible, and maintainable distributed applications. However, in the .NET world, COM+ components are referred to as serviced components. Due to the number of changes in the .NET strategy, the deployment model for serviced components is different from deploying traditional COM+ components. In this article, we will understand how to deploy serviced components using the various tools provided by the .NET Framework. Along the way, we will also highlight how the deployment model varies for different types of serviced components. Finally, we will wrap it up by understanding how the deployment of serviced components can be automated using a Windows installer project.

Component services in Windows 2000 has a wealth of enterprise functionality that provides all the plumbing required for creating distributed, Web-enabled applications. Although COM+ services were originally designed for use with COM components, it would be nice if .NET components could also use them because of services such as object pooling, database connection pooling, sharing resources, role based security, and distributed transaction processing monitoring that the COM+ runtime provides.

In the .NET Framework, there is a separate namespace called System.EnterpriseServices that contains all the classes required for providing COM+ services to applications. For an introduction to COM+, please take a look at the following link from TechNet: Introduction to COM and COM+.

The Problem

We have a web server that hosts a number of different applications, classic ASP websites, ASP.NET applications, and console applications scheduled in MS-Tasks. All of these applications use our SMTP server to send emails. Our SMTP server requires user authentication. We have a single email account dedicated for our web to be used as a sender email ID from all our applications.

Imagine that your development manager asked you to change that email account password for security reasons and he has a plan for changing it frequently every month. It is really a pain to go through all your .NET application web.config files and change the email setting and all your classic ASP websites also to change the include files.

The Solution

I am sure there are so many solutions now to overcome the problem mentioned above, but here I am just sharing my experience from six years ago, in 2003, when I was working as a developer in a software development company. At that time, I had very good experience working in Classic ASP, but only two years experience working in .NET. And we were using MS VS2003 and coding in VB.NET. I was not very familiar with C# Smile | :)

The first thing I thought about was to do a global DLL file which would read a single config file located in our web server; this DLL file could be used in any application, whether it is a classic ASP, .NET, or Java application.

I utilize the COM+ serviced component to develop a class library that has a public function for sending emails. The email setting would be read from an XML config file located in the web server.

Using the Code

Here I am going to explain my solution step-by-step (in VS2005):

  • In VS2005, create a Class Library project.
  • Add a reference to the System.EnterpriceServices DLL.
  • 01.jpg

    You will need to add the following line in the Imports section:

    Imports System.EnterpriseServices
  • You have to make your class inherit from the “ServicedComponent” class:
  • Public Class btnmail
       Inherits EnterpriseServices.ServicedComponent
  • I then create just a simple function (HelloWorld) for testing purposes only:
  • ' Shows Explicit SetComplete/SetAbort.
    Public Function HellowWorld() As String
        ContextUtil.SetComplete()
        Return "Hello World"
    End Function
  • This is the email function that is used to send emails:
  • Public Function SendEmail(ByVal sender, ByVal sname, ByVal recipient, _
                    ByVal cc, ByVal subject, ByVal body) As Boolean
        Dim retVal As Boolean = False
    
        Try
    
            'defining dataset 
            Dim ds As DataSet = New DataSet()
    
            'reading email setting config file. 
            'here i am hard-coding the location of the config.xml file.
            'refer to the source code to see the config file structure.
            ds.ReadXml("d:\smtpmailsetting\config.xml")
    
            ' assigning the values to the vars.
            Dim smtpserver As String = ds.Tables(0).Rows(0)("ServerName")
            Dim UserName As String = ds.Tables(0).Rows(0)("UserName")
            Dim Password As String = ds.Tables(0).Rows(0)("Password")
            Dim bcc As String = ds.Tables(0).Rows(0)("BCC")
            ds.Dispose()
    
            ' creating email message object
            Dim message As New MailMessage()
            message.IsBodyHtml = True
    
            message.From = New MailAddress(sender, sname)
            message.To.Add(recipient)
    
            If Not String.IsNullOrEmpty(cc) Then
                message.CC.Add(cc)
            End If
    
            If Not String.IsNullOrEmpty(bcc) Then
                message.Bcc.Add(bcc)
            End If
    
            message.Subject = subject
    
            message.Body = body
    
            Dim emailClient As New SmtpClient(smtpserver)
    
            'check to see if there are user name and password,
            ' then only i am doing the authentication part.
            If Not String.IsNullOrEmpty(UserName) And _
               Not String.IsNullOrEmpty(Password) Then
                Dim SMTPUserInfo As New _
                    System.Net.NetworkCredential(UserName, Password)
                emailClient.UseDefaultCredentials = False
                emailClient.Credentials = SMTPUserInfo
            End If
    
            emailClient.Send(message)
    
            retVal = True
            ContextUtil.SetComplete()
        Catch ex As Exception
    
            'logging the error to a log file if there is any.
            Dim sw As StreamWriter = _
                New StreamWriter("d:\smtpmailsetting\logs\" & _
                Now.ToString("ddMMyyyy-hhmmss") & ".log", True)
            Try
                sw.Write(ex.Message)
    
            Catch ex1 As Exception
    
            Finally
                sw.Flush()
                sw.Close()
            End Try
    
            retVal = False
            ContextUtil.SetAbort()
        End Try
    
        Return retVal
    
    End Function
  • Then, we will create a key file using the sn utility. To generate the key file, we will run the Visual Studio .NET command prompt and then enter the command as shown below:
  • sn -k btnmail.snk

    02.jpg

  • Copy “testkey.snk” to your project solution.
  • Add “AssemblyInfo.vb” and add the key info:
  • <Assembly: AssemblyTitle("")> 
    <Assembly: AssemblyCompany("")>
    <Assembly: AssemblyProduct("")> 
    <Assembly: AssemblyCopyright("")>
    <Assembly: AssemblyTrademark("")> 
    <Assembly: CLSCompliant(True)>
    <Assembly: AssemblyKeyFile("..\..\btnmail.snk")>
  • Now we need to install it in the Global Assembly Cache, “GAC”.
  • regsvcs (path of the dll)
  • Create a vbs file to test it. Create “test.vbs” which contains the following code:
  • set obj = CreateObject("smtpbtnmail.btnmail")
    msgbox(obj.HelloWord)
  • Double click on it to run it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

hussain.attiya
Architect Gulf air - Bahrain
Bahrain Bahrain
Current Position is : Information System Analyst
 
Leading a Team of Developers for developing ecommerce websites and systems integration using several integration methods such as XML.
 
Manage ISP infrastructure, web servers, database servers, application servers, email servers and networks. Design the architecture of complex web applications; build web applications in JAVA, asp, asp.Net; manage web sites including networking and database; experienced in MS Sql Server and Oracle;
 
stay current with emerging trends and technological advances in the industry.
 
MCTS,MCAD VB.NET, ASP.NET and XML Services,BTEC National Diploma in Computer Studies.

Comments and Discussions

 
General[comments] PinmemberMember 823782330-Jul-13 0:21 
QuestionExcellent PinmemberMusthafa (Member 379898)29-Oct-12 21:40 
Questionmy vote of 5 Pinmembermuhamad yousef28-May-12 20:24 
GeneralMy vote of 2 Pinmembergaurav_verma_mca1-Nov-09 3:36 
GeneralRe: My vote of 2 Pinmemberhussain.attiya1-Nov-09 20:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 1 Nov 2009
Article Copyright 2009 by hussain.attiya
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid