0

I have client server application, where server is WCF service. In Win8.1 client, I want to access this service, but only on few screens. What is the most effective approach?

1) By creating instance in app where its needed, using:

var client = new SomeRandomNamespace.Server.ServerNameClient();

Q: How to correctly dispose this client?

2) Or by creating this client as globally accessible singleton class, so it will be created only once? What are the benefits and drawbacks? How will be requests processed if there will be multiple requests in same time?

Many thanks!

user969153
  • 568
  • 8
  • 25
  • If only Windows Phone could add a service reference, huh? – Jerry Nixon Jun 04 '14 at 16:38
  • Not WP, but Windows Store app (see tags), or any other WPF app. – user969153 Jun 05 '14 at 05:13
  • Sorry, a Windows Store App and a Phone app are the same to me a they are combined now in universal app projects. Sorry. We should have tags that are more explicit, perhaps. The most effective approach is a service reference in the project of course. There are no drawbacks to doing that. Having a static reference to the client is useless, it will gain you nothing. But a central method to return the client is fine. Refer to this: http://stackoverflow.com/questions/5251213/any-change-in-net-4-0-in-how-to-properly-call-a-wcf-service – Jerry Nixon Jun 06 '14 at 15:08

1 Answers1

0

Here's how you call the client:

  var _Client = new MyServiceReference.MyServiceClient();
  try
  {
      var _Item = _Client.GetItem(123);
      _Client.Close();
      return _Item;
  }
  catch
  {
      _Client.Abort();
      throw;
  }

As you can see, Close() and Abort() are important. Not Dispose().

Best of luck!

Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233