Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
How to: Create a Serviced Component

How to: Create a Serviced Component 

.NET Framework 2.0

The following procedure describes how to create a new serviced component.

To create a serviced component

  1. Define a class that derives directly or indirectly from the ServicedComponent class. For example, the following code ensures that the Calculator class is hosted by a COM+ application.

    Imports System.EnterpriseServices
    
    Public Class Account 
    Inherits ServicedComponent
        Function Add (x as Integer, y as Integer) As Integer
            Add = x + y
        End Function
    End Class
    
    

    using System.EnterpriseServices;
    
    public class Calculator : ServicedComponent
    {
        public int Add (int x, int y)
        {
            return(x+y);
        }
    }
    
    
  2. Apply attributes to indicate how each assembly, class, or method should interact with COM+ functionality.

    NoteNote

    In .NET Framework version 1.1, the COM+ security configuration is enabled by default if the ApplicationAccessControlAttribute attribute is not present in the assembly. This is a change in behavior from .NET Framework version 1.0.

    <Assembly: ApplicationName(“Calculator”)>
    <Assembly: System.Reflection.AssemblyKeyFile(“Calculator.snk”)>
    
    <MustRunInClientContext> _
    Public Class Account 
    Inherits ServicedComponent
        ‘ Member definitions.
    End Class
    
    

    [assembly: ApplicationName("Calculator")]
    [assembly: System.Reflection.AssemblyKeyFile("Calculator.snk")]
    
    [MustRunInClientContext]
    public class Calculator : ServicedComponent
    {
        // Member definitions.
    }
    
  3. Generate a strong key and compile the following example:

    sn –k Calculator.snk
    vbc /t:library /r:System.EnterpriseServices.dll Calculator.vb
    

    sn –k Calculator.snk
    csc /t:library /r:System.EnterpriseServices.dll Calculator.cs
    
  4. Deploy the serviced component application by registering its assembly dynamically or manually.

  5. After a serviced component is registered, clients can create instances of the component the way they create instances of any other component. For a complete example, see Serviced Component Example .

    NoteNote

    On Windows 2000 platforms, COM+ always loads the most recent version of the common language runtime for the component you are creating. This means that on a computer with both .NET Framework version 1.0 and .NET Framework version 1.1 installed, .NET Framework version 1.1 is always loaded. As a workaround, you can create a COM+ dllhost.exe.configuration file that enables you to “lock” all applications to a specific version of .NET Framework. On Windows XP and Windows Server 2003 platforms, the Application Root Directory setting can be used to point COM+ to an appropriate directory for its configuration file.

Example

using System.EnterpriseServices;

[assembly: ApplicationName("Calculator")]
[assembly: System.Reflection.AssemblyKeyFile("Calculator.snk")]

[MustRunInClientContext]
public class Calculator : ServicedComponent
{
    public int Add (int x, int y)
    {
        return(x+y);
    }
}

See Also

Community Additions

ADD
Show:
© 2015 Microsoft