An Enterprise Component Server:
using System;
using System.Collections.Generic;
using System.Text;
using System.EnterpriseServices;
namespace MSDN_dagene
{
[Description("My component")]
public interface MineMetoder
{
int Kalkuler(int tall);
}
[ObjectPooling(MinPoolSize=3, MaxPoolSize=10, CreationTimeout=20)]
[JustInTimeActivation(true)]
[Transaction(TransactionOption.Required)]
public class MSDN_2006 : System.EnterpriseServices.ServicedComponent,
MineMetoder
{
public MSDN_2006()
{
}
public int Kalkuler(int tall)
{
try
{
// Do transaction work etc...
ContextUtil.MyTransactionVote = TransactionVote.Commit;
}
catch
{
ContextUtil.MyTransactionVote = TransactionVote.Abort;
}
return tall + 10;
}
protected override void Activate()
{
base.Activate();
}
protected override void Deactivate()
{
base.Deactivate();
}
protected override bool CanBePooled()
{
return true;
}
}
}
Make a strong name file. Set the project to be COM visible.
Eg. a AssemblyInfo.cs file:
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.EnterpriseServices;
[assembly: ApplicationName("MSDN_dagene")]
[assembly: ApplicationActivation(ActivationOption.Server)]
[assembly: Description("MSDN dagene 2006")]
[assembly: AssemblyKeyFile(@"C:\Lars-Inge\Microsoft dagene 2006\Enterprise
components\MSDN_dagene\MSDN_dagene\strongName.snk" )]
[assembly: AssemblyTitle("MSDN_dagene")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MSDN_dagene")]
[assembly: AssemblyCopyright("Copyright © 2006")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(true)]
[assembly: Guid("d0ba9290-fb59-4065-886c-5b62f8d80625")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Install the dll with the command line:
c:\> regsvcs theDll.dll
Open the "control panel" + "Administrative tools" + "Component Services"
You should see it under "COM+ Applications".
The Client.
Make a reference to the server dll-file.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace KlientTrheim
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// Remember to use the interface here, or it will not work....
MSDN_dagene.MineMetoder dag = new MSDN_dagene.MSDN_2006();
this.textBox2.Text = dag.Kalkuler( 23 ).ToString();
}
}
}
A Win XP Pro can do about 10 client connections to its server object. If you
want more connections the server must be installed on a Win2003
(application) Server.
Regards,
Lars-Inge Tønnessen
http://www.larsinge.com