ChannelFactory.CreateChannel 的反义词是什么?
我刚刚按照这个教程操作了一些代码。我几乎可以肯定我在某处读到该通道有超时,因此它最终可能会自动关闭。因此,我尝试简单地在客户端中为我想要调用的每个方法打开一个新通道,最终(经过多次调用后)我收到了错误。
似乎我可以同时打开的频道数量有限制。但由于通道是自定义对象的实例,我不知道如何关闭它或杀死它,或者我需要用它做什么来摆脱它,以便我可以创建其他通道。
然后我在 CreateChannel
文档 上注意到我的 TChannel
应该实现 IChannel
(我上面链接的教程没有实现)。那么,这就是我关闭频道的方式吗?如果是这样,我将如何关闭它或者我应该在 Close
方法的实现中做什么?如果我必须实现该接口,我应该如何实现其他所有方法?
或者我应该只使用单个通道,只要它持续有效?不管怎样,如果我拥有的只是我自己的类的一个实例,我该如何知道是有故障的还是打开的还是关闭的?
正如你所看到的,我对这个主题很迷茫,所以我希望你能为我指明正确的方向。
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). 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? 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ChannelFactory.CreateChannel
创建并返回您指定服务类型的通道。返回的对象已经实现了IChannel
。您(通常?)不需要实现自己的Close
方法,也不需要实现IChannel
的任何其他方法。通常,您不会为每个呼叫创建一个新通道,而只需重复使用它。 (仅在某些特定情况下,为每个调用创建一个新通道可能会更好)。
您可以通过将其强制转换为
IClientChannel
来关闭通道。使用此模式:您可以使用
((IClientChannel)channel).State
来获取通道的状态(即CreatedOpened
、Faulted
、<代码>关闭)。ChannelFactory<TChannel>.CreateChannel
creates and return a channel of your specified service type. The returned object already implementsIChannel
. You (normally?) don't need to implement your ownClose
method, nor any other methods ofIChannel
.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
. Use this pattern:You can use
((IClientChannel)channel).State
to get the state of the channel (i.e.CreatedOpened
,Faulted
,Closed
).Peladao 基本上一语中的。
为了澄清他所说的一些内容,CreateChannel 将创建一个(代理)对象,该对象实现您的自定义服务接口和 IClientChannel。
通常,您会保持通道打开并重用其调用。还要注意,一旦进入故障状态就无法恢复,必须打开新通道。正如 Peladao 提到的,可以通过 ((IClientChannel)channel).State 检测到故障状态,并且不要忘记,您通常也会遇到异常。
如果内存可用,则 WCF 的调试进程接受服务的 10 个并发通道。
Peladao basically hits the nail on the head.
To clarify some of what he is saying, CreateChannel will create a (proxy) object which implements both your custom service interface and 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.
If memory serves, the debug process for WCF accepts 10 simultaneous channels for a service.