WCF, factory channel and exceptions

Using vs2008, vb.net, C #, fw 3.5

I use the service in my client

Service hosted in IIS

Client (winforms MDI) is generated using svcutil using the / l, / r, / ct and / n switches

Service and client use MyEntities.dll

I use nettcp with TransportWithMessageCredential I cache proxies in the main form

if Membership.ValidateUser(UsernameTextBox.Text, PasswordTextBox.Text) _proxy = new MyServiceClient _proxy.ClientCredentials.UserName.UserName = "username" _proxy.ClientCredentials.UserName.Password = "password" 

Then I pass _proxy to all child forms / plugins that should use them ex

 List(of Orders) = _proxy.ChannelFactory.CreateChannel.GetOrders(customer) 

Everything works fine, but my questions are:

What happens to channels after a call? Are they magically tuned?

How can I control this with the profiler?

Is there a way I can handle errors in one place, or do I need to place try / catch in all calls, such as What is the best workaround for WCF client `block issue?

 try { ... client.Close(); } catch (CommunicationException e) { ... client.Abort(); } catch (TimeoutException e) { ... client.Abort(); } catch (Exception e) { ... client.Abort(); throw; } 

Can I sign up for _proxy.InnerChannel.Faulted and do it there?

Hi

_Eric

+4
source share
2 answers

I use two different things, depending on the option used:

  • In a client scenario, where I know only one instance of a channel, it is used at a time, I'm lazy - I create a channel and reuse the created instance. In case of failure, closing or deletion, the channel is recreated.
  • In scenarios where multiple channels may be requested at the same time, I think it’s best to do exception handling dances. To avoid bloating the code, you can centralize it in a method that accepts a delegate for the actual work that he has done so that he creates an exoskeleton with write-once around your payload code.
+2
source

Additional test results / notes

It seems I partially answered my question, I started this loop for 500 X

  List(of Orders) = _proxy.ChannelFactory.CreateChannel.GetOrders(customer) 

This is very bad, and at the beginning of the 11th iteration, a timeout error was received, which is the maximum user of my service (10). Does this mean that someone can implement any wcf client and open as many channels as the wcf server allows?

I found that it gave me the expected results and completed all 500 iterations

  Dim channel = _proxy.ChannelFactory.CreateChannel e.result = Channel.GetOrders(customer) Dim Ich = DirectCast(channel, ServiceModel.IClientChannel) Ich.Close() Ich.Dispose() 

My question now is whether I can start the channel, close and delete inside the _proxy.InnerChannel.Faulted event, or for every call I make, just terminate it in an attempt, and then it catches a timeout / comm / exception exception, leaving a proxy server , and deleting the channel? If in the latter case there is a way to encapsulate this?

Hi

_Eric

+1
source

Source: https://habr.com/ru/post/1300095/


All Articles