小编Álv*_*ier的帖子

C#JIT优化器中可能存在的错误?

使用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)

.net c# jit cil

23
推荐指数
1
解决办法
1290
查看次数

WCF关闭ServiceHost时等待操作完成

我创建一个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?也就是说,我想要禁用服务侦听器,还要等待所有当前连接的客户端完成(或指定最大超时).

.net c# wcf soap

5
推荐指数
1
解决办法
5197
查看次数

标签 统计

.net ×2

c# ×2

cil ×1

jit ×1

soap ×1

wcf ×1