Google
WWW Yariv Hammer's Code Site

Sunday, February 26, 2006

Getting Started With Enterprise Services

Introduction
COM+ components are called Serviced Components in .NET.
In this article I will show how to create a very simple Serviced Component, how to use this component from a .NET application, and how to configure the component from the COM+ Configuration Tool.
In addition, I will discuss some of the aspects of the usage of COM+ from whithin the .NET framework.

Your First Enterprise Service
- Open VS.NET 2003.
- Create a new class library (I will use C# in this tutorial), called HelloEnterpriseServices.
- Add a reference to System.EnterpriseServices.dll
- We must sign the assembly with a strong name (it is not mandatory to place it in the GAC):
In the command prompt of .NET, in the foder of the class library, type "sn -k keys.snk"
In the AssemblyInfo.cs file change the following:
[assembly: AssemblyKeyFile("..\\..\\keys.snk")]
Build the class library.
- Add a class called HelloService as follows:
--------------------------------------------------------------
using System.EnterpriseServices;
namespace HelloEnterpriseServices
{
public class HelloService:ServicedComponent
{
public string SayHello(string name)
{
return "Hello " + name;
}
}
}
---------------------------------------------------------
- Build the project.

Congratulations. You have just created your first enterprise service.

Some Explanations:
In our class library we can place as many classes as we want. Some of the classes may inherit from the ServicedComponent class. All the classes are managed, and act the same as any other .NET class. The classes which derive from ServicedComponent will be registered as COM+ components, and the class library will be registered as a COM+ application.

If you will check the COM+ Configuration Tool you still can't see the new application.

Next we will fix some security issues. COM+ provide a service for role-based security. It is a good practice to use it, but if you do not wish to do so, you should open the AssemblyInfo file and add the following:
- Add using System.EnterpriseServices in the beginning of the file.
- Add [Assembly: ApplicationAccessControl(false)] at the end.
- Build again.
In the COM+ Configuration Tool, in the application properties, in the Security tab, you can see a checkbox "Enforce access checks for this application". You don't want this to be checked, unless you set up the security, because the application will throw an exception when loaded by clients.

Consuming the Serviced Component
Add a new Windows Application to the solution, called HelloEnterpriseServicesApp.
First add a reference to the previous HelloEnterpriseServices library. You must also add a reference to System.EnterpriseServices.

Next we will use our HelloService class. It is used as any other .NET class.
- Add a button to Form1. Double-click on it.
- Add using
HelloEnterpriseServices
to the form code.
- Add the following code to the button click event handler:

private void button1_Click(object sender, System.EventArgs e)
{
HelloService service = new HelloService();
MessageBox.Show(service.SayHello("Jack"));
}

- Set the windows application project to be the startup project of the solution.

Run the solution. The form shows right away. Clicking on the button will take a couple of seconds. At this time the COM+ component is first registered. Clicking again on the button shows the message box much faster. This kind of registration is called "Lazy Registration". I will show another method soon.

If you check the COM+ Configuration Tool (right click and select Refresh), you will see HelloEntrpriseServices. It is a Library application (as you can see by the icon). If you expand the application, and the Components, you will see the HelloService component. Expnad it further, and the Interfaces and you will not find the SayHello method. That is because we did not define a .NET interface for the service.
If you right-click on the HelloService component, and select properties, you will see 2 GUIDs (in the General tab) - one is CLSID and the other is for the Application. You must understand that by using COM+ from whithin .NET you go back to the time of COM and "DLL Hell" - The classes are registered to the registry, you cannot do side-by-side deployment, and cannot easily version the component. The .NET assembly, however, has all the benefits of .NET assemblies.

Improving the Component
Consider the following steps a MUST-DO. You will save yourself some headaches.
First we will add an interface to our component:
------------------------------------------------------
using System.EnterpriseServices;
namespace HelloEnterpriseServices
{
public interface IHello
{
string SayHello(string name);
}
public class HelloService:ServicedComponent,IHello
{
public string SayHello(string name)
{
return "Hello " + name;
}
}
}
---------------------------------------------------
If you wish to do so, you can place the interface in a different assembly. You can also define multiple interfaces to the assembly.

Next step will be to put the GUIDs ourselves. It is a good practice to force COM+ to use our GUIDs, in order to make sure that the applications that are installed are indeed ours.
In Visual Studio .NET, select Tools menu, and Create GUID tool. Select Registry Format, and Copy.
Add the following attribute to the interface:
[Guid("B906C925-D72F-4d8d-B8D5-672D7C2E595A")] .
The GUID here is just an example. Be sure to remove the curly brackets ({})
You can do the same process to add a GUID to the class (A different GUID).

Generate another GUID, and in the AssemblyInfo file add the following attribute:
[assembly: ApplicationID("Guid here")]
This will set the Application GUID.

Other useful attributes you can use:
[assembly: ApplicationName("Hello Application")] - for a user friendly name.
[assembly: ApplicationActivation(ActivationOption.Library)] - To set Library or Server application
[assembly: Description("Here is a nice description you will see in the COM+ Configuration Tool")]

Rebuild the class library.
Before we continue, delete the previous HelloEnterpriseServices application (right-click on it and select "delete").

I will show you how to register the application from command prompt:
go to the folder of the dll (using "cd fullpath") and type
regsvcs .dll
Later, when you update your component, you can call
regsvcs .dll /reconfig
The regsvcs tool wrapps the .NET dll with COM, register it in the registry and creates a tlb file you can call from a native language (as COM)

In the COM+ Configuration Tool (after you refresh) you will see in the application properties the new application ID, the description, and the use-friendly application name. When you expand the interface of the HelloService component, you will see the IHello interface, and underneath the SayHello method.

The attribute we placed in the AssemblyInfo file (such as ApplicationActivation to set Server/Library) are only relevant when we deploy. After deployment anyone can reconfig the application to their choosing (that's the whole point of COM+).

When you create newer versions you do not need to create new GUIDs. Unless of course you change the interface (which is not recommended as you will need to support old clients). Remember that there is no side-by-side activation of com components (unlike .NET assemblies).

Contexts
Each ServicedComponent inherits from ContextBoundObject. When an object tries, for example, to participate in a transaction, it must know the transaction ID. This parameter is a part of the context of the object. Each feature we use in COM+, we make the context of the object more heavy, with more parameters. The object context is the set of services provided by COM+ to the object.
For some objects the context is more similar than other objects. In this case the interaction will be more smooth. In cases where the contexts differ significantly, there will be an interception - a context switch. Your goal is to keep the contexts similar as much as possible, which will cause less interception, and improve performance.

The handling of contexts is completely transparent in .NET. Any interaction you will do inside your code with COM+ is done through a class called ContextUtil. This stateless class has only static methods and properties. You will need to use this class when you will program transactions, object pooling and other COM+ services.

COM+ Applications vs. .NET Assemblies
The COM+ application is a frame for logical management of components. It is not a single unit of deployment, as opposed to the .NET dll. You can create new COM+ applications with components from different .NET assemblies. If you design your component to be reusable blackboxes correctly, you will have a very powerful way of using the same components in different COM+ applications.
In the COM+ Configuration Tool, right click on COM+ Applications, and select New Application. This will start the COM+ Application Installation Wizard. You can install a pre-built application, so you can choose an msi file you exported using the Export option. Or you could create an empty application. You will enter a name "HelloEnterpriseServices2" and select the type of application (server or library). Select InteractiveUser at this point. You will see the empty application under COM+ application. Now you can add a Component (select New Component to show another wizard). You can install new components from dll or tlb files, or you can use one of the components already installed in your computer. When you are done, you can Export it all, and create an msi file to install it on one shot in another computer. Note that you will need to do proper ID management.

There are very few services that will force you to use a Server application (for example: Queued Component must be in a different process). It is usually more efficient to use Library applications.

There might be some issues when using several versions or copies of .NET assemblies. Although it is not mandatory, placing the .NET assembly in the GAC will tell everybody to use the same copy of the assembly, which help might help you when debugging and deploying. You will always know the exact copy of the assembly the COM+ application is using.

Another issue you should consider is the usage of properties. COM does not support properies, as .NET does. So each property will be translated into a get and a set method. I would suggest using Getter and Setter methods in the first lace, so you will not have different interfaces to managed and native components.

Summary
In this article I showed how to build the minimum enterprise service possible. I showed the usage of GUID and interfaces. I showed how to consume the enterprise service in managed code. I discussed some deployment issues.

In the next article I will show how to use transactions in managed code.

1 Comments:

At 2:45 AM, June 27, 2017, Blogger kingrani said...

Flipkart is Offering Cashback Offers Flipkart SBI Cashback Offer You will get upto 75% Discount

Flipkart is Offering Cashback Offers Flipkart AXIS Cashback Offer You will get upto 75% Discount

Flipkart is Offering Cashback Offers Flipkart YES Cashback Offer You will get upto 75% Discount

Flipkart is Offering Cashback Offers Flipkart Phonepe Cashback Offer You will get upto 75% Discount

Flipkart is one of the biggest world wide eCommerce Site flipkart cashback offersso

Flipkart is one of the biggest world wide eCommerce Site Flipkart hdfc offersso

 

Post a Comment

<< Home

Feel free to use everything here. Add links to my site if you wish.

Do not copy anything to other sites without adding link to here.

All the contents of the site belong to Yariv Hammer.