English 中文(简体)
WCF channel lifetime with repeat calls
原标题:

Maybe this is an obvious question, maybe it isn t. Imagine a GUI control application where every button push calls a different function on a remote WCF service. Button usage is frequent at approximately every few seconds. In general is it best to open and close the WCF channel every function call or hold the channel open for the lifetime of the application? Assuming of course that there is only ever a few instantiations of this application.

NB. This question is not really about the example, but in helping me get my head around the best practice here.

最佳回答

It is not recommended to leave the channel open but leave WCF to decide when to physically closeopen channels. You can save some resources by using the same client object over and over again just make sure no 2 threads use it at the same time (in case 2 buttons can be pressed at once or one right after the other).

问题回答

If you can spare yourself from having to recreate the client proxy before every call, that would definitely be beneficial for your performance.

BUT: doing so, you must ensure that all exceptions on the server-side are handled properly (maybe by implementing the IErrorHandler interface on your service) and turned into SOAP faults, which can be transferred back to the client without faulting the channel.

If a single exception escapes you, and get sent back to the client as a "regular" .NET exception, the channel (the communication link between your client and your server) will be faulted --> i.e. rendered useless.

In such a case, you need to first check for that state (the channel has a .State property), and if you encounter a faulted channel, there s nothing you can do except re-create the client proxy again before calling methods on it.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签