gpt4 book ai didi

c# - 为什么此 WCF 代理代码有效?

转载 作者:行者123 更新时间:2023-11-30 13:37:12 24 4
gpt4 key购买 nike

在调试应用程序时,我发现了以下代码。这显然是错误的,但出于某种奇怪的原因它起作用了,我似乎不明白为什么。在我看来,代理会在创建后立即被释放,但调用此代理上的方法可以正常工作以连接到 WCF 服务。

谁能解释为什么这段代码不会爆炸?

private static IMyService _proxy = null;
private static IMyService Proxy
{
get
{
if (_proxy == null)
{
using (_proxy as IDisposable)
{
ChannelFactory<IMyService> factory =
new ChannelFactory<IMyService>("MyService");
_proxy = factory.CreateChannel();
}
}
return _proxy;
}
}

最佳答案

这真的很有趣!

很明显,using()现有 变量的组合使用语句 and 重新分配 block 中的原始内容,导致传递给 Dispose() 的新引用,而不是现有的 _proxy ,按照下面的 IL。

事实上,LinqPad 发出一个 warning为此:

Possibly incorrect assignment to local 'variable' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local.

但是,此行为似乎需要转换为 as IDisposablestatic 也没有似乎有任何影响。

在 LINQPad 中将以下代码反汇编为 IL:

IMyService _proxy = null;
if (_proxy == null)
{
using (_proxy)
{
_proxy = new SomeService();
}
}

产量

IL_0001:  ldnull      
IL_0002: stloc.0 // _proxy
IL_0003: ldloc.0 // _proxy
IL_0004: ldnull
IL_0005: ceq
IL_0007: ldc.i4.0
IL_0008: ceq
IL_000A: stloc.1 // CS$4$0000
IL_000B: ldloc.1 // CS$4$0000
IL_000C: brtrue.s IL_002D
IL_000E: nop
** IL_000F: ldloc.0 // _proxy
** IL_0010: stloc.2 // CS$3$0001
IL_0011: nop
IL_0012: newobj UserQuery+SomeService..ctor
IL_0017: stloc.0 // _proxy
IL_0018: nop
IL_0019: leave.s IL_002B
$IL_001B: ldloc.2 // CS$3$0001
$IL_001C: ldnull
$IL_001D: ceq
IL_001F: stloc.1 // CS$4$0000
IL_0020: ldloc.1 // CS$4$0000
$IL_0021: brtrue.s IL_002A
** IL_0023: ldloc.2 // CS$3$0001
IL_0024: callvirt System.IDisposable.Dispose
IL_0029: nop
IL_002A: endfinally
IL_002B: nop

可以看出(通过 **),创建了一个新的 loc 2,传递给 Dispose() 的正是这个引用。 .但在此之前是对 null ($) 的检查,它绕过了 Dispose无论如何。

将此与明确的 try-finally 对比:

 if (_proxy == null)
{
try
{
_proxy = new SomeService();
}
finally
{
_proxy.Dispose();
}
}

IL_0001: ldnull
IL_0002: stloc.0 // _proxy
IL_0003: ldloc.0 // _proxy
IL_0004: ldnull
IL_0005: ceq
IL_0007: ldc.i4.0
IL_0008: ceq
IL_000A: stloc.1 // CS$4$0000
IL_000B: ldloc.1 // CS$4$0000
IL_000C: brtrue.s IL_0025
IL_000E: nop
IL_000F: nop
IL_0010: newobj UserQuery+SomeService..ctor
IL_0015: stloc.0 // _proxy
IL_0016: nop
IL_0017: leave.s IL_0023
IL_0019: nop
** IL_001A: ldloc.0 // _proxy
IL_001B: callvirt System.IDisposable.Dispose
IL_0020: nop
IL_0021: nop
IL_0022: endfinally
IL_0023: nop

哪里可以清楚的看出是"original" _proxy这是已处置的,没有在 using() 中完成额外的空检查, 确认 the docs .

不用说这段代码是邪恶的:

  • _proxy永远不会Disposed ,即 using是多余的。
  • It relies on passing null to using 没有失败。
  • 因为_proxyusing 之前定义 , 它可以在 block 内重新分配,不像变量 declared IN the using声明 - 例如using (var _proxy = ...)将是 read-only ,并且尝试改变这样的变量无论如何都会导致编译时错误。
  • 抛开静态单例 channel 的优点,_proxy 的惰性初始化也不是线程安全的(例如,没有双重检查锁),可以用 private static readonly Lazy<IMyService>(() => ...) 来简化和一个简单的 setter/getter 。

关于c# - 为什么此 WCF 代理代码有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24413078/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com