ok, here's the situation.
I am trying to write a class (DynamicServiceGenerator) that creates a dynamic assembly ("DynamicService.dll") which contains a dynamic module that defines an ObjectPooling Attributed type ("DynamicServicedComponent"), deriving from Windows.Enterprise.Services.ServicedComponent, where the "CreationTimeout", "MaxPoolSize" and "MinPoolSize" properties are set at runtime.
The point is, during initialization, I want the DynamicServiceGenerator to configure how many instances of DynamicServicedComponents are allowed to run in the Machines Pooled Object environment.
I would then get a Pooled object by calling
DynamicServiceFactory.GetDynamicServicedComponent()
This should instance a DynamicServicedComponent Type from the dynamically created assembly.
(Does any of this make sense???)
the problem is, when I call:
DynamicServer.DynamicServiceGenerator dsGen = new DynamicServiceGenerator(1,10,30000);
System.EnterpriseServices.ServicedComponent sc = dsGen.GetServicedComponent();
I get an error at 'dsGen.GetServicedComponent()':
The invoked member is not supported in a dynamic module.
Here is my complete code:
using System;
using System.EnterpriseServices;
using System.Reflection;
using System.Reflection.Emit;
using System.IO;
using System.Runtime.InteropServices;
namespace DynamicServer
{
public class DynamicServiceGenerator
{
Type m_DynamicServiceType;
private void CreateDynamicServicedComponentType(int MinObjects, int MaxObjects, int CreationTimeout)
{
// This should basically create an assembly containing the following simple class definition:
// [ ObjectPooling( MinPoolSize= [MinObjects] ,
// MaxPoolSize= [MaxObjects],
// CreationTimeout= [CreationTimeout],
// Enabled = true ) ]
// public class DynamicServicedComponent: ServicedComponent {}
//
// Get the domain
AppDomain myDomain = System.Threading.Thread.GetDomain();
// Load a strong name key pair needed for serviced component assemblies
FileStream fs = new FileStream("dynclass.snk", FileMode.Open);
StrongNameKeyPair kp = new StrongNameKeyPair(fs);
fs.Close();
// Set up the assembly
AssemblyName myAsmName = new AssemblyName();
myAsmName.Name = "DynamicService.dll";
myAsmName.KeyPair = kp;
AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly( myAsmName, AssemblyBuilderAccess.RunAndSave);
// Define the dynamic module
ModuleBuilder ServicedComponentModule = myAsmBuilder.DefineDynamicModule("DynamicServicedComponent","DynamicServicedComponent.mod", true);
// Define the dynamic type
TypeBuilder ServicedComponentBuilder = ServicedComponentModule.DefineType(
"DynamicService.DynamicServicedComponent",
TypeAttributes.Public ,
typeof(ServicedComponent) );
// Get a constructor for an ObjectPoolingAttribute
Type[] ctorParams = new Type[] {};
ConstructorInfo classCtorInfo = typeof(ObjectPoolingAttribute).GetConstructor(ctorParams);
// Build the ObjectPoolingAttribute Properties
Type myType = typeof(ObjectPoolingAttribute);
System.Reflection.PropertyInfo[] myProps = new System.Reflection.PropertyInfo[]
{
myType.GetProperty("CreationTimeout"),
myType.GetProperty("Enabled"),
myType.GetProperty("MaxPoolSize"),
myType.GetProperty("MinPoolSize") };
// Build the ObjectPoolingAttribute Property Values
object[] propVals = new object[] { CreationTimeout,true, MaxObjects, MinObjects};
// Use a CustomAttributeBuilder to construct the ObjectPoolingAttribute and set the properties
CustomAttributeBuilder myCABuilder = new CustomAttributeBuilder(classCtorInfo, new object[]{} , myProps, propVals);
// Apply the ObjectPoolingAttribute to the Dynamic Type
ServicedComponentBuilder.SetCustomAttribute(myCABuilder);
// Create the type in the assembly
m_DynamicServiceType = ServicedComponentBuilder.CreateType();
}
// Constructor initializes a DynamicService Type with ObjectPooling attribute configuresd and enabled
public DynamicServiceGenerator(int MinObjects, int MaxObjects, int CreationTimeout)
{
CreateDynamicServicedComponentType(MinObjects, MaxObjects, CreationTimeout);
}
// This should instance a DynamicService
public ServicedComponent GetDynamicServicedComponent()
{
Type[] types = new Type[0];
ConstructorInfo ci = m_DynamicServiceType.GetConstructor(types);
return (ServicedComponent) ci.Invoke(null);
}
}
}
|