简体   繁体   English

ChannelFactory有什么反面 <TChannel> .CreateChannel?

[英]What's the opposite to ChannelFactory<TChannel>.CreateChannel?

I just followed this tutorial and played a little bit with the code. 我只是遵循了教程,并使用了一些代码。 I'm almost sure I read somewhere that there is a timeout for the channel, so it might get automatically closed eventually. 我几乎可以肯定,我在某处读到该频道有超时,因此它最终可能会自动关闭。 So I tried simply opening a new channel in my client for each method I wanted to invoke, and eventually (after lots of calls) I got errors. 因此,我尝试仅在客户端中为要调用的每种方法打开一个新通道,最终(在多次调用后)出现错误。

Seems like there is a limit on how many channels I can have open at the same time. 似乎我可以同时打开多少个通道受到限制。 But since the channel is an instance of a custom object, I don't see how can I close it or kill it or whatever I need to do with it to get rid of it so I can create other channels. 但是,由于通道是自定义对象的实例,因此我看不到如何关闭或杀死它,或者需要做些什么来摆脱它,以便创建其他通道。

Then I noticed on the CreateChannel documentation that my TChannel should implement IChannel (which the tutorial I linked above doesn't do). 然后我在CreateChannel 文档中注意到我的TChannel应该实现IChannel (我上面链接的教程没有实现)。 So, is this how I would close my channel? 那么,这就是我要关闭频道的方式吗? If so, how would I close it or what should I do on my implementation of the Close method? 如果是这样,我将如何关闭它,或者在执行Close方法时应该怎么做? And what should I do on the implementation of every other method if I do have to implement the interface? 如果必须实现接口,我应该在其他方法的实现上做什么?

Or should I just use a single channel for as long as it lasts? 还是我应该只使用一个频道就可以持续多久? Anyway, how am I supposed to know whether is faulted or open or closed if all I have is an instance of my own class? 无论如何,如果我所拥有的只是我自己的类的一个实例,我应该如何知道是故障还是打开或关闭?

As you can see I'm pretty lost on the subject so I hope you can point me in the right direction. 如您所见,我对这个主题很迷茫,希望您能指出正确的方向。

ChannelFactory<TChannel>.CreateChannel creates and return a channel of your specified service type. ChannelFactory<TChannel>.CreateChannel创建并返回您指定服务类型的通道。 The returned object already implements IChannel . 返回的对象已经实现IChannel You (normally?) don't need to implement your own Close method, nor any other methods of IChannel . 您(通常吗?)不需要实现自己的Close方法,也不需要实现IChannel任何其他方法。

Normally you don't create a new channel for every call, you just re-use it. 通常,您不会为每个通话都创建一个新频道,而只是重新使用它。 (Only in some specific cases it may be better to create a new channel for every call). (仅在某些特定情况下,为每个呼叫创建一个新渠道可能更好)。

You can close the channel by casting it to IClientChannel . 您可以通过将通道强制转换为IClientChannel来关闭通道。 Use this pattern: 使用以下模式:

try
{
  ((IClientChannel)channel).Close();
}
catch (Exception ex)
{
  ((IClientChannel)channel).Abort();
}

You can use ((IClientChannel)channel).State to get the state of the channel (ie CreatedOpened , Faulted , Closed ). 您可以使用((IClientChannel)channel).State来获得信道(即状态CreatedOpenedFaultedClosed )。

Peladao basically hits the nail on the head. Peladao基本上打在了头上。

To clarify some of what he is saying, CreateChannel will create a (proxy) object which implements both your custom service interface and IClientChannel. 为了澄清他在说些什么,CreateChannel将创建一个(代理)对象,该对象同时实现您的自定义服务接口 IClientChannel。

Typically you do keep the channel open and reuse its calls. 通常,您确实要保持通道打开并重用其调用。 Beware also that once it enters a fault state there is no recovery, you must open a new channel. 还请注意,一旦进入故障状态就无法恢复,则必须打开一个新通道。 As Peladao mentions a fault state can be detected via ((IClientChannel)channel).State, and also don't forget you'll generally get an exception too. 正如Peladao提到的那样,可以通过((IClientChannel)channel).State来检测故障状态,也不要忘记您通常也会得到一个异常。

If memory serves, the debug process for WCF accepts 10 simultaneous channels for a service. 如果有内存,则WCF的调试过程将接受10个同时进行的服务通道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM