使用SQLHelper类来自动化存储过程调用的方式与XmlRpc.Net库中的方法类似,在运行从IL代码手动生成的方法时遇到了一个非常奇怪的问题.
我把它缩小到一个简单的生成方法(可能它可以简化得更多).我创建了一个新的程序集和类型,包含两个符合的方法
public interface iTestDecimal
{
void TestOk(ref decimal value);
void TestWrong(ref decimal value);
}
Run Code Online (Sandbox Code Playgroud)
测试方法只是将十进制参数加载到堆栈中,装箱,检查它是否为NULL,如果不是,则将其拆箱.
TestOk()方法的生成如下:
static void BuildMethodOk(TypeBuilder tb)
{
/* Create a method builder */
MethodBuilder mthdBldr = tb.DefineMethod( "TestOk", MethodAttributes.Public | MethodAttributes.Virtual,
typeof(void), new Type[] {typeof(decimal).MakeByRefType() });
ParameterBuilder paramBldr = mthdBldr.DefineParameter(1, ParameterAttributes.In | ParameterAttributes.Out, "value");
// generate IL
ILGenerator ilgen = mthdBldr.GetILGenerator();
/* Load argument to stack, and box the decimal value */
ilgen.Emit(OpCodes.Ldarg, 1);
ilgen.Emit(OpCodes.Dup);
ilgen.Emit(OpCodes.Ldobj, typeof(decimal));
ilgen.Emit(OpCodes.Box, typeof(decimal));
/* Some things were …Run Code Online (Sandbox Code Playgroud) 我创建一个WCF SOAP服务器,其操作需要一些时间来执行:
[ServiceContract]
public interface IMyService
{
[OperationContract]
string LongRunningOperation();
}
[ServiceBehavior(
ConcurrencyMode = ConcurrencyMode.Multiple,
UseSynchronizationContext = false,
InstanceContextMode = InstanceContextMode.Single)]
class MyService : IMyService
{
public string LongRunningOperation()
{
Thread.Sleep(20000);
return "Hey!";
}
}
class Program
{
static void Main(string[] args)
{
MyService instance = new MyService();
ServiceHost serviceHost = new ServiceHost(instance);
BasicHttpBinding binding = new BasicHttpBinding();
serviceHost.AddServiceEndpoint(typeof(IMyService), binding, "http://localhost:9080/MyService");
serviceHost.Open();
Console.WriteLine("Service running");
Thread.Sleep(10000);
serviceHost.Close();
Console.WriteLine("Service closed");
Thread.Sleep(30000);
Console.WriteLine("Exiting");
}
}
Run Code Online (Sandbox Code Playgroud)
ServiceHost打开,10秒后关闭它.
调用serviceHost.Close()时,当前连接的所有客户端(等待LongRunningOperation完成)将立即断开连接.
是否等待以更清洁的方式关闭ServiceHost?也就是说,我想要禁用服务侦听器,还要等待所有当前连接的客户端完成(或指定最大超时).