consume a web service from a wpf standalone application

hello all,

i have a wpf application with a combobox and a button.

i need to populate this combobox with values from the NorhtWind db - Customers table - filed :city.

so i need to click the button, call the web service and populate the combobox with the cities from the customer table.

I use c#. i need the method that returns the cities in the web service.

and then how to call this service to populate the combobox with data

urgent help needed.

nikolaoskAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nikolaoskAuthor Commented:
thanks.

i have seen those. i need a full code solution for the problem described above. i do not mind if it is in VB
.
0
nikolaoskAuthor Commented:
anyone? i just need a simple sample on how can someone consume a web service from a wpf client.

c# or vb is accepted

please help
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

AshokCommented:
Step 1:

Add Connection in Server Explorer, after connecting to NorthWind Database,
add New Item in Solution "Linq-To-Sql Class"
Then DRAG Customers table to DBML designer

Step 2:
In XAML of the Form, add

<ComboBox Height="23" Margin="31,86,39,0" Name="comboBox1" Text="{Binding Path=City}"  DisplayMemberPath="City" VerticalAlignment="Top" />

Step 2:  Add following code in private void Window_Loaded(object sender, RoutedEventArgs e)

            DataClassDataContext dbNorthWnd = new DataClassDataContext();


            var query = from s in dbNorthWnd.Customers
                        select s;
            comboBox1.ItemsSource = query.ToList();            

HTH
Ashok
0
AshokCommented:
0
nikolaoskAuthor Commented:
thanks for your asnwer.

what i actually need is this

i need to have a wpf project in my solution.

i need to have a web service project in my solution

i need the web service to have a method where it goes and queries a db (through LINQ) and returns a resultset.

then i need in the wpf window to have a datagrid.
i need to call the service from private void Window_Loaded

and populate the datagrid with data that is fetched from the web servide method
0
AshokCommented:
I understand it. Will post it later.

Thanks,
Ashok
0
nikolaoskAuthor Commented:
thanks a lot
0
AshokCommented:
I have Vista and it's giving very hard time making WCF work.  Unfortunately I do not have Windows XP computer with VS 2008.

Anyway,

Create WCF Service Library Project

Add Linq-ToSql Class named "DataClass.dbml"  Drag table Customers to DBML designer.

==== Code for IWcfSvc.cs interface ====

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfSvcLibrary
{
    // NOTE: If you change the interface name "IWcfSvc" here, you must also update the reference to "IWcfSvc" in App.config.
    [ServiceContract]
    public interface IWcfSvc
    {
        [OperationContract]
        List<Customer> GetCustomerList();
    }
}

======= Code for WcfSve.cs implementation =========

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfSvcLibrary
{
    // NOTE: If you change the class name "WcfSvc" here, you must also update the reference to "WcfSvc" in App.config.
    public class WcfSvc : IWcfSvc
    {
        public List<Customer> GetCustomerList()
        {
            // Add your operation implementation here
            var sStoreList = from City in dbNorthWnd.Customers
                             select City;
            return sStoreList.ToList();
        }

        DataClassDataContext dbNorthWnd = new DataClassDataContext();

    }
}

===================================================

Build above Project to make sure it compiles.

Then Use File menu to Add New Project of WPF type.

Add Reference "System.ServiceModel"
and
Add Reference (from Project Tab) to add ServiceProjectLibrary project (it should be in the list)

Add Service Reference (Discover)

============ Window1.xaml.cs ===============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;  // Added
using System.ComponentModel;           // Added
using WpfBoxCityWcf.WcfSvcRef;         // Added
using WcfSvcLibrary;                   // Added

namespace WpfBoxCityWcf
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        WcfSvcClient _Proxy = new WcfSvcClient();

        public Window1()
        {
            InitializeComponent();
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _Proxy.GetCustomerListCompleted += new EventHandler<GetCustomerListCompletedEventArgs>(_Proxy_GetCustomerListCompleted);
            _Proxy.GetCustomerListAsync();

        }

        void _Proxy_GetCustomerListCompleted(object sender, GetCustomerListCompletedEventArgs e)
        {
            cBoxCity.ItemsSource = e.Result;
        }

    }
}
=======================================================================
<Window x:Class="WpfBoxCityWcf.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
    <Grid>
    <ComboBox Height="23" Margin="20,103,23,0" Name="cBoxCity" DisplayMemberPath="City" VerticalAlignment="Top" />
  </Grid>
</Window>

HTH
Ashok
0
AshokCommented:
Forgot to mention

when adding Service Reference,  click Advanced button
to change
check Generate asynchrous operation
and
select System.Collection.ObjectModel.ObeservableCollection (from dropdown list)

then
click OK to generate reference.
0
AshokCommented:
WcfSve.cs should be WcfSvs.cs
0
AshokCommented:
I got the whole project on Vista working with WPF and WCF.

My WCF Service Library Project Name = WcfSvcLibrary (WcfSvc.cs and IWcfSvc.cs same code as in my previous post)
My WPF project name = WpfBoxCityWcf

Complete App.Config in WcfSvcLibrary
========================
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="WcfSvcLibrary.Properties.Settings.NORTHWNDConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=&quot;C:\SQL Server 2000 Sample Databases\NORTHWND.MDF&quot;;Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="serviceBehavior" name="WcfSvcLibrary.WcfSvc">
        <endpoint address="" binding="wsHttpBinding" contract="WcfSvcLibrary.IWcfSvc">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/Design_Time_Addresses/WcfSvcLibrary/WcfSvc/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

=====================
Updated Window1.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;  // Added
using System.ComponentModel;           // Added
using WpfBoxCityWcf.WcfSvcRef;         // Added
using WcfSvcLibrary;                   // Added

namespace WpfBoxCityWcf
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        WcfSvcClient _Proxy = new WcfSvcClient();

        public Window1()
        {
            InitializeComponent();
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            cBoxCity.IsEnabled = false;
            _Proxy.GetCustomerListCompleted += new EventHandler<GetCustomerListCompletedEventArgs>(_Proxy_GetCustomerListCompleted);
            _Proxy.GetCustomerListAsync();

        }

        void _Proxy_GetCustomerListCompleted(object sender, GetCustomerListCompletedEventArgs e)
        {
            cBoxCity.ItemsSource = e.Result;
            cBoxCity.IsEnabled = true;
        }

    }
}

======
Window1.xaml (Same as before)

<Window x:Class="WpfBoxCityWcf.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
    <Grid>
    <ComboBox Height="23" Margin="20,103,23,0" Name="cBoxCity" DisplayMemberPath="City" VerticalAlignment="Top" />
  </Grid>
</Window>

HTH
Ashok
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AshokCommented:
BTW,

WcfSvc.cs should be as follows
====================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfSvcLibrary
{
    // NOTE: If you change the class name "WcfSvc" here, you must also update the reference to "WcfSvc" in App.config.
    public class WcfSvc : IWcfSvc
    {
        public List<Customer> GetCustomerList()
        {
            // Add your operation implementation here
            var sCityList = from City in dbNorthWnd.Customers
                             select City;
            return sCityList.ToList();
        }

        DataClassDataContext dbNorthWnd = new DataClassDataContext();

    }
}

HTH
Ashok
0
nikolaoskAuthor Commented:
can you send me the code? i mean the actual solution. for some reason it cannot afind me any service when i do add service reference
0
AshokCommented:
Which OS are you using?

Vista or Windows 7?

0
AshokCommented:
OK, I am going to post the whole solution here,

I am unable to attach it here.  I get following error when I try to add file even after renaming to .txt extension.

The extension of one or more files in the archive is not in the list of allowed extensions:
WpfBoxCityWcf/WcfSvcLibrary/WcfSvcLibrary.suo

HTH
Ashok
0
AshokCommented:
Check your e-mail box. I just sent you a ZIP file.

You should be getting it within 30 minutes.

Ashok
0
nikolaoskAuthor Commented:
THANKS
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.