ChannelFactory <t> .CreateChannel如何工作?</t>

时间:2013-09-12 09:20:13

标签: c# wcf

如果我有一个界面:

public interface ISomething
{
    void DoAThing();
}

然后我用ChannelFactory实例化它:

var channel = new ChannelFactory<ISomething>().CreateChannel

我得到了一个我可以使用的实例。

现在,关闭它我需要施展:

((IClientChannel)channel).Close

((IChannel)channel).Close

((ICommunicationObject)channel).Close

我的 ISomething 界面不会继承任何这些界面。

那么 CreateChannel 方法返回了什么样的对象?它是如何构造一个动态对象的,它能够实现一个它在运行时才知道的接口?

1 个答案:

答案 0 :(得分:1)

ChannelFactory.CreateChannel()返回RealProxy的实现,它是一组工具的一部分,通常称为TransparentProxy或“Remoting”,这是一种稍微过时的pre-wcf技术。为了创建实现接口的实际类,它归结为一个名为RemotingServices的内部框架级方法.CreveTransparentProxy(...),我没有看过,但很可能是一个类构建器/某些种类的发射器。

正如您所问,您可能想要自己做这样的事情。要在运行时实现接口,我建议Castle Dynamic Proxy实现接口或抽象类而不需要太多努力。

相关问题