none
WCF issues RRS feed

  • Question

  • Hello guys! I have a WCF service library, name ServerWCF, which contains a "AdminService" service. One of the methods of this service is List<Station> LoadStation() which should return a List of train stations from my database.

    I also have a WPF Client, and I'm using a dropdown that should contain the Cities of the stations. When I added the Service Reference in the client, named AdminServiceReference, i noticed that the signature of the method is Station[] Load Station() instead on List<Station>LoadStation(). I don't know if I can do anything about this as the code is generated automatically in the client, but whatever(I used the resharper refactoring suggestion, but didn't make any difference). The problem is that on the below code, the foreach line is never accessed. I placed a breakpoint on the line above, I stepped into and noticed that the list returned by the Server contains all of the station, which is a good thing, but after i step out and step over, the program is not going to the foreach that populates the dropdown, but returns to the second line, which is the reason why, obviously, my dropdown remains empty, lonely and sad :( Any tip on how can i sort this out? PS: that same code worked fine when my list was a ArrayList and was containing only Cities, but I need all the objects.

    Client code:

    private void fromStationCB_DropDownOpened(object sender, EventArgs e)
            {
                var adminProxy = new AdminServiceClient();
                fromStationCB.Items.Clear();
                var list = adminProxy.LoadStations();
                foreach (var items in list)
                    fromStationCB.Items.Add(items.City);
                label5.Content = "";
            }

    Server code:

    public List<Station> LoadStations()
            {
                List<Station> list = new List<Station>();
                using (var context = new RailwaySystemModelContainer())
                {
                    /*foreach (var station in context.Stations)
                        list.Add(station);*/
                    list.AddRange(context.Stations);
                    return list;
                }
            }
    Tuesday, April 4, 2017 9:14 PM

All replies

  • fromStationCB_DropDownOpened(object sender, EventArgs e)

    {

    var adminProxy = new AdminServiceClient();

    fromStationCB.Items.Clear();

    var list = adminProxy.LoadStations();

    foreach (var items in list)

             fromStationCB.Items.Add(items.City);

    label5.Content = ""; }


    Are you sure "var list" (bold part) is contains value not empty? If there is no value in "list" your loop will not execute.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Tuesday, April 4, 2017 9:25 PM
  • Hi leinad777,

    Thank you for posting here.

    According to your question is more related to WCF, I will move it to Windows Communication Foundation, Serialization, and Networking forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 5, 2017 6:21 AM
  • I checked the discussion on WCF issues - line not reached, what is your current code?

    Your server code is “public List<Station> LoadStations()”, but, your serviceContract is “Station[] LoadStations()”. I do not think your service will work as your service contract and service implement class are different.

    >> I placed a breakpoint on the line above, I stepped into and noticed that the list returned by the Server contains all of the station

    What is this list? Is it in service LoadStations or “var list”? If it is “var list”, I am confused that the foreach return directly.

    I suggest you try below steps, and share us the result.

    1. Modify your LoadStations like below:
    //Service Contract
            [OperationContract]
            List<Station> LoadStations();
    //Service Implementation
            public List<Station> LoadStations()
            {
                List<Station> list = new List<Station>();
                Station s1 = new Station() { StationId=1, City="C1", Type="T1" };
                Station s2 = new Station() { StationId = 2, City = "C2", Type = "T2" };
                Station s3 = new Station() { StationId = 3, City = "C3", Type = "T3" };
                List<Station> Stations = new List<Station>();
                Stations.Add(s1);
                Stations.Add(s2);
                Stations.Add(s3);
                list.AddRange(Stations);
                return list;
                //using (var context = new RailwaySystemModelContainer())
                //{
                //    /*foreach (var station in context.Stations)
                //        list.Add(station);*/
                //    list.AddRange(context.Stations);
                //    return list;
                //}
            }
    2. Add ServiceReference, in the popup dialog, Data Type->check Always generate message contracts->Collection type->System.Collections.Generic.List, this will make it return list
    3. Add below code in DropDownOpened
    private void comboBox_DropDownOpened(object sender, EventArgs e)
            {
                WCFLibService.Service1Client client = new WCFLibService.Service1Client();
                WCFLibService.LoadStationsResponse result= client.LoadStations(new WCFLibService.LoadStationsRequest());
                foreach (var item in result.LoadStationsResult)
                    comboBox.Items.Add(item.City);
            }



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 5, 2017 8:02 AM
  • When you create (or update) service reference you can use Advanced dialog to change collection type, see image. So you are sure the list in event handler contains items?

    change collection type

    Friday, April 7, 2017 11:25 AM
  • Have your issue been resolved? It seems your issue has been resolved from below:

    #WCF issues - line not reached

    http://stackoverflow.com/questions/43217850/wcf-issues-line-not-reached


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 10, 2017 2:21 AM