Microsoft Knowledge Base Email Alertz

KBAlertz.com: (306296) - This step-by-step article demonstrates how to create a serviced .NET component that uses transactions. This article also demonstrates how to create a client that tests your serviced component. Microsoft Enterprise Services provides Microsoft COM+...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
KBAlertz referrals get
** SIX MONTHS FREE **


Community Site



We Send hundreds of thousands of emails using ASP.NET Email


ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 306296 - Last Review: September 18, 2003 - Revision: 4.1

HOW TO: Create a Serviced .NET Component in Visual C# .NET

This article was previously published under Q306296

On This Page

SUMMARY

This step-by-step article demonstrates how to create a serviced .NET component that uses transactions. This article also demonstrates how to create a client that tests your serviced component. Microsoft Enterprise Services provides Microsoft COM+ services to .NET components.

Important Notes

  • Serviced components require strong names.
  • Serviced components should be registered in the Global Assembly Cache (GAC) because they are system-level resources. Server applications require installation in the GAC, but library applications do not (although it is recommended).
  • You can register serviced components with COM+ either automatically through lazy registration or manually through the Regsvcs.exe utility. Regsvcs.exe is located in the following folder:
    \WINNT\Microsoft.NET\Framework\<Framework Version>
    For more information about Regsvcs.exe, refer to the Microsoft .NET Framework Software Development Kit (SDK) documentation.
  • This sample assumes that Microsoft SQL Server has been installed on the local computer.
  • This sample is intended only for illustration purposes. Strictly speaking, this sample's select query is a good candidate to run outside of a COM+ transaction because COM+ uses the highest isolation level for the transaction. To improve database throughput, it is good programming practice to consider read queries for lower transaction levels.

Create the Serviced .NET Component

  1. Create a new Visual C# Class Library project named ServicedCOM.
  2. Rename your default class and the filename from Class1.cs to SimpleTrans.cs. to do this, follow these steps:
    1. In the Class View window, right-click Class1, and then click Properties.
    2. In Properties, change the Name property to SimpleTrans.
  3. Add a reference to the System.EnterpriseServices namespace.
  4. Add the following statement, as the top line, to both SimpleTrans.cs and AssemblyInfo.cs:
    using System.EnterpriseServices;
    At the top of SimpleTrans.cs add the following statement:
    using System.Data.SqlClient;
  5. Inherit your class from ServicedComponent (fully qualified name: System.EnterpriseServices.ServicedComponent).
  6. Add the following code just before the *public class* declaration:
    [Transaction(TransactionOption.RequiresNew)]
    					
  7. Add the following recommended attributes to AssemblyInfo.cs:
    [assembly: ApplicationActivation(ActivationOption.Library)]
    [assembly: ApplicationName("SimpleTrans")]	
    					
    • The ActivationOption attribute indicates whether the component will be activated within the caller's process. You can set Activation.Option to Library or to Server.
    • The ApplicationName attribute is the name that appears for the COM+ application in the COM+ Catalog and the Component Services Administration console.
  8. Add the following optional attribute to SimpleTrans.cs, just after the using statements:
    [assembly: Description("Simple Transactional application to show Enterprise Services")]
    						
    This attribute provides a description for the COM+ application in the COM+ Catalog and Component Services Administration console.
  9. Add the following method to SimpleTrans.cs:
    // Demos Explicit SetComplete/SetAbort
            public string DoTrans()
            {
                SqlConnection	connection;
                SqlCommand		command; 
                SqlDataReader	reader;
                string		name;
                string		query;
    		
                try
                {
                    query = "SELECT au_lname, au_fname FROM authors";
                    connection = new SqlConnection("data source = localhost;
                                                    initial catalog = pubs;
                                                    UID=sa;PWD=");
                    command = new SqlCommand(query, connection);
    
                    connection.Open();
                    reader = command.ExecuteReader();
    				
                    reader.Read();
                    name = reader.GetString(0) + ", " + reader.GetString(1);
                }
                catch(Exception exc)
                {
                    ContextUtil.SetAbort();
                    throw exc;
                }
                return name;
        }
    // Demo implicit SetComplete/SetAbort
    [AutoComplete]
    public void DoTxAuto()
    {
        // Do stuff
    }
    					
  10. Modify the SqlConnection string as appropriate for your environment.

Give Your Assembly a Strong Name

  1. Click Start, point to Programs, point to Microsoft Visual Studio .NET, point to Visual Studio .NET Tools, and then click Visual Studio .NET Command Prompt.
  2. At the command prompt, type sn.exe -k ServicedCOM.snk to give your assembly a strong name.

    For information about signing assemblies with strong names, refer to the .NET Framework Software Development Kit (SDK) documentation.
  3. Copy ServicedCOM.snk to the project folder.
  4. In AssemblyInfo.cs, replace the AssemblykeyFile code with the following code:
    [assembly: AssemblyKeyFile("..\\..\\ServicedCOM.snk")]
    					

Add Your Serviced Component to COM+

You can allow the component to register dynamically when the first instance is created, or you can manually register the component with Regsvcs.exe. To use Regsvcs.exe, follow these steps:
  1. Click Start, point to Programs, point to Microsoft Visual Studio .NET, point to Visual Studio .NET Tools, and then click Visual Studio .NET Command Prompt.
  2. At the command prompt, type regsvcs servicedcom.dll. This creates a COM+ library application with the same name as your class name. Ignore the warning message.

Test Your Component

  1. In Notepad, open a text file.
  2. Paste the following code in the file:
    set o =createobject("ServicedCOM.SimpleTrans")
    
    MsgBox o.DoTrans()
    					
  3. From the File menu, click Save.
  4. In the Save As dialog box, in the File name text box, type Test.vbs. In the Save as type list, click All Files, and then click Save.
  5. Double-click the file to run the sample.

APPLIES TO
  • Microsoft Enterprise Services (included with the .NET Framework) 1.0
  • Microsoft Visual C# .NET 2002 Standard Edition
Keywords: 
kbhowtomaster KB306296
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please