locked
Bind gridview with collection received via webservice

    Question

  • I want to send an observablecollection object through a WCF service and receive it in windows store app.  the service Is as below.

      
      public ObservableCollection<State> GetName()
        {
    
            StateEntities objEntities = new StateEntities();
            ObservableCollection<State> stateCollection = new ObservableCollection<State>();
            foreach (State s in objEntities.States)
            {
                stateCollection.Add(s);
            }
            return stateCollection;
        }
    

    State is a Class having data from the table.  The contract is given below

      
     public interface IHost
        {
            OperationContract]
            ObservableCollection<State> GetName();
        }
    

    Now I want to consume the service in a windows phone app.  A button click should trigger the service consumption , get data from the service as observable collection and feed it into a gridview.

    public sealed partial class MainPage : Page
        {
    
         ServiceReference1.HostClient proxy;
    
     public MainPage()
            {
                proxy  = new HostClient();
                this.InitializeComponent();            
            }
    
     private async void Button_Click(object sender, RoutedEventArgs e)
            {
    
                ObservableCollection<State> stateCollection = new ObservableCollection<State>();
                stateCollection = await proxy.GetNameAsync();
                dataGrid.ItemsSource = stateCollection;
            }
    }
    

    The Xaml code :

    <StackPanel Grid.Column="1" Grid.Row="1">
                <GridView x:Name="dataGrid" Grid.Column="1" DataContext="{Binding}" HorizontalAlignment="Center"  Grid.Row="1" VerticalAlignment="Center" Width="100"/>
                <Button Content="Load Data from Service" HorizontalAlignment="Center"  Grid.Row="1" Grid.Column="1" VerticalAlignment="Bottom"  Click="Button_Click"/>
     </StackPanel>

    but it is throwing and exception in the await line. This is the exception.

    An exception of type 'System.ServiceModel.CommunicationException'  occurred in mscorlib.dll but was not handled in user code

    Additional information: An error occurred while receiving the HTTP  response to "ttp://localhost:65338/Host.svc." This could be due to the  service endpoint binding not using the HTTP protocol. This could also  be due to an HTTP request context being aborted by the server

    Can anybody suggest an alternative or solution ?

    Monday, November 17, 2014 10:50 AM

All replies

  • I don't think there is something directly wrong with your code but something with the communication. is the specified url in the client app right? in your error I see ttp://localhost:65338/Host.svc and I think that should be http

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, November 17, 2014 11:11 AM
  • Ya it is http . I ve removed the 'h' in the editor as I was not able to post links.
    I am able to receive string and integers. But Collections, lists, arrays etc.. are not getting transmitted or received. I am not sure whether it is the problem with the sending or receiving entity.

    I have also tried configuring the service reference with all the collection types.

    And it is showing the same error.


    • Edited by Kiran Bose Monday, November 17, 2014 11:50 AM
    Monday, November 17, 2014 11:48 AM
  • can you reach the service from your browser?

    did  you checked if localhost loopback is enabled in the project settings?


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#


    Monday, November 17, 2014 11:55 AM
  • The service is reachable from the browser .

    You have created a service.To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:svcutil.exe http://localhost:65338/Host.svc?wsdlYou can also access the service description as a single file:http://localhost:65338/Host.svc?singleWsdlThis will generate a configuration file and a code file that contains the client class. Add the two files to your client application and use the generated client class to call the Service. For example:C#class Test
    {
        static void Main()
        {
            HostClient client = new HostClient();
    
            // Use the 'client' variable to call operations on the service.
    
            // Always close the client.
            client.Close();
        }
    }
    
    But I am not aware of the loopback. Can you please give assistance on that ?
    
    

    Monday, November 17, 2014 12:18 PM
  • go to the project properties of your app. I think on the debug page there is an option to allow localhost loopback. Don't have visual studio right here.. otherwise I maked a screenshot for you.. but check the project properties and you wil find it

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, November 17, 2014 12:26 PM
  • Yea. the loopback Is enabled. Loopback should have been enabled earlier, otherwise how can string be passed via web service.

    I tried turning loopback off, but it will not connect to the service right ? 

    The debugger shows that the below exceptions are thrown.

    Exception:Thrown: "An existing connection was forcibly closed by the remote host" (System.Net.Sockets.SocketException)
    A System.Net.Sockets.SocketException was thrown: "An existing connection was forcibly closed by the remote host"
    Time: 11/17/2014 6:08:09 PM
    Thread:Worker Thread[11848]
    Exception:Thrown: "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host." (System.IO.IOException)
    A System.IO.IOException was thrown: "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."
    Time: 11/17/2014 6:08:09 PM
    Thread:Worker Thread[11848]

    Exception:Thrown: "The underlying connection was closed: An unexpected error occurred on a receive." (System.Net.WebException)
    A System.Net.WebException was thrown: "The underlying connection was closed: An unexpected error occurred on a receive."
    Time: 11/17/2014 6:08:09 PM
    Thread:Worker Thread[11848]

    Exception:Thrown: "An error occurred while receiving the HTTP response to http://localhost:65338/Host.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted
     by the server (possibly due to the service shutting down). See server logs for more details." (System.ServiceModel.CommunicationException)
    A System.ServiceModel.CommunicationException was thrown: "An error occurred while receiving the HTTP response to http://localhost:65338/Host.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an
     HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details."
    Time: 11/17/2014 6:08:09 PM
    Thread:Worker Thread[11848]


    • Edited by Kiran Bose Monday, November 17, 2014 12:51 PM
    Monday, November 17, 2014 12:50 PM
  • Hi Kiran,

    I think you may need to follow the instruction: How to enable loopback and troubleshoot network isolation, double check if all your settings are correct.

    It's not possible that you can access the HTTP localhost site in Browser but cannot access in Windows Store App.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Wednesday, November 26, 2014 9:45 AM
    Moderator
  • Can you check whether you have added the capability for network?
    Wednesday, November 26, 2014 10:47 AM