0

I have a class which talks to a wcf

public class Network { private const string PROXY_HOSTNAME_PARAM = "ProxyHostname"; public string GetMyNetwork() { MyProxy myprox = MyProxy.Instance; string network = string.Empty; network = myprox .GetNetworkConfig(); return network; } 

Now i have another class where I create object of the class Network and invoke GetMyNetwork() to get network. When i do this it hits wcf again and gives me the string. I want to avoid that.I want it to cache the network string once and for all and whenever i hit this method i should get the data without hitting wcf again. how can i achieve this ?

3
  • 3
    You could use cache, static variable, save to db, save to file, etc. Commented Jan 17, 2017 at 14:02
  • 2
    One word: Singleton Commented Jan 17, 2017 at 14:10
  • ObjectCache cache = MemoryCache.Default; if(cache.Contains(network)) return (IEnumerable)cache.Get(network); Commented Jan 17, 2017 at 14:14

3 Answers 3

1

You need to set class and function( or property) static

public static MyClass { public static string MyFunc() { return "hello"; } } 

usage

string myString = MyClass.MyFunc(); 
Sign up to request clarification or add additional context in comments.

Comments

1

You can make singleton from your Network class :

public class Network { static Network _instance; public static Network Instance { get { return _instance ?? (_instance = new Network()); } } string _networkString; private Network() { _networkString = string.Empty; } public string GetMyNetwork() { if(string.IsNullOrEmpty(_networkString)) return (_networkString = MyProxy.Instance.GetNetworkConfig()); return _networkString; } } 

Now you are able to use it the same way as you use your MyProxy object :

string network = Network.Instance.GetMyNetwork(); 

Comments

1
public class Network { private const string PROXY_HOSTNAME_PARAM = "ProxyHostname"; private string network = String.Empty; private Network _instance; public string GetMyNetwork() { if (String.IsNullOrWhiteSpace(network)) { MyProxy myprox = MyProxy.Instance; network = myprox .GetNetworkConfig(); } return network; } private Network() { } public Network Instance { get { if (this._instance == null) { this._instance = new Network(); } return this._instance; } } } 

Make sure to maintain only one instance of Network class. Each time you create a new instance of Network class, the internal network variable will be a blank string, so calling GetMyNetwork method will again call the WCF service.

4 Comments

didnt help when i create a object of this class and call my method it hits wcf again
@sameer Whenever you call the method on a new instance of this class, it will hit the WCF. You should maintain a single instance of this class throughout the client application e.g. you could make a static variable or make the Network class singleton.
@sameer I have made this class singleton. Try to invoke the method like Network.Instance.GetMyNetwork(); This should solve your problem.
@Rocker1985 That's not even close to singleton. Thing you've done is preventing Network object from being instantiated with new Network() making it useless.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.