Reputation: 1403
I have a WCF Service in which I implements some methods(Operartional Contract). I need to load some values from Excel to dictionary. I can load the values. But It gets loaded every time when service member gets accessed/consumed.
I don't want want to load every time. Its enough to load only once. How to achieve this.
public class Service1 : IService1
{
Dictionary<string, List<MappingFields>> MappingCollections;
public Service1()
{
MappingCollections = loadCodes(Location, fileName);
}
}
#region IService1 Members
public void TicketTrack(Tickets[] tickets)
{
............;
............;
}
Upvotes: 1
Views: 143
Reputation: 29668
You could do something like this:
static Dictionary<string, List<MappingFields>> MappingCollections;
public void TicketTrack(Tickets[] tickets)
{
if (MappingCollections == null) MappingCollections = loadCodes(Location, fileName);
...
}
This would check for the existence of an instance of mapping collections and if it doesn't exist, create it (could probably shunt that out to a utility method).
Upvotes: 1
Reputation: 2577
Make it a static variable:
private static Dictionary<string, List<MappingFields>> MappingCollections = loadCodes(Location, fileName);
Make method 'loadCodes' also static. This way the dictionary will be loaded only once for AppDomain.
Upvotes: 0
Reputation: 604
You could use lazy to achieve that:
private static Lazy<Dictionary<string, List<MappingFields>>> MappingCollections =
new Lazy<Dictionary<string, List<MappingFields>>>(x => loadCodes(Location, fileName));
public void TicketTrack(Tickets[] tickets)
{
var myData = MappingCollections.Value;
}
Upvotes: 0