tags:

views:

48

answers:

1

We use dictionary to store the data in memory but the data in memory is increased then exception fire of Fault stage. how to handle this problem.

----------IServices------------

[ServiceContract]
public interface IMyService
{
    [OperationContract()]
    Dictionary<int, Car> getCarList();
}

----------Services------------

public class MyService : IMyService
{
    private const String fileName = @"c:\\Car.txt";
    public MyService()
    {
        if (myCar.dicCar == null)
        {
            myCar.dicCar = new Dictionary<int, Car>();
            myCar.lastID = 0;
            if (File.Exists(fileName))
            {
                StreamReader r = new StreamReader(fileName);
                while (!r.EndOfStream)
                {
                    String line = r.ReadLine();
                    String[] data = line.Split('^');
                    Car c = new Car();
                    if (data.Length == 5)
                    {
                        int id = Convert.ToInt32(data[0].Trim());
                        c.Manufacturer = data[1];
                        c.Name = data[2];
                        c.Model = data[3];
                        c.Price = Convert.ToDouble(data[4]);
                        myCar.dicCar.Add(id, c);
                        myCar.lastID = id > myCar.lastID ?  id: myCar.lastID;
                    }
                }
                r.Close();
            }

        }
    }
}

    public Dictionary<int, Car> getCarList()
    {
        return myCar.dicCar;
    }

in case of dictionary has recordrs about 1350 then exception fire : The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state

A: 

What exactly is the exception? From the example, I can think of a few scenarios:

  • it is faulting when loading the data, perhaps due to duplicates (Add(id, c))
  • it is faulting when serializing

In the latter case, it may be that increasing the WCF quotas helps - or perhaps consider an alternative serialization implementation (reducing the bandwidth).

As a side note, it is probably not ideal to eagerly load that data in the MyService, although it also isn't quite clear to me whether perhaps myCar is a static type (that we can't see). And if so, you might want to watch out for threading issues, especially when it loads the first time (since you don't synchronize, and mutate it while other threads have access). So if you are seeing something about "iterator" in the exception, it could be related to this.

And again: what is the exact exception?

Marc Gravell