Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

WCF service and return Type RSS

6 replies

Last post Jul 29, 2008 09:27 AM by kunal2383

(0)
  • WCF service and return Type

    Jul 28, 2008 06:37 AM | LINK

    Hello All, 

    I am getting following error:

    Cannot convert type 'WPFClient.ServiceReference1.PrecinctBLEntity[]' to 'System.Collections.ObjectModel.ObservableCollection<WPFClient.ServiceReference1.PrecinctBLEntity>'

    Please help me. I am stuck in this error since some hours.

    my WCF service is has below:

    IPrecinctService.cs:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Runtime.Serialization;

    using System.ServiceModel;

    using System.Text;

    namespace Test

    {

    #region PrecinctService Interface

    [ServiceContract]

    public interface IPrecinctService

    {

     

    [
    OperationContract]

    PrecinctBLEntity Create(PrecinctBLEntity data);

    [OperationContract]

    PrecinctBLEntity Update(PrecinctBLEntity data);

    [OperationContract]

    void Delete(PrecinctBLEntity data);

    [OperationContract]

    List<PrecinctBLEntity> Read(PrecinctBLEntity data);

    [OperationContract]

    List<PrecinctBLEntity> PrecinctsData();

    }

    #endregion

    #region
    Precinct Entity

    [DataContract]

    public class PrecinctBLEntity

    {

    string precinctName;

    string portion;

    string precinctNumber;

    [DataMember]

    public string PrecinctName

    {

    get { return precinctName; }set { precinctName = value; }

    }

    [DataMember]

    public string Portion

    {

    get { return portion; }set { portion = value; }

    }

    [DataMember]

    public string PrecinctNumber

    {

    get { return precinctNumber; }set { precinctNumber = value; }

    }

    }

    #endregion

    }

    PrecinctService.cs (WCF service)

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Runtime.Serialization;

    using System.ServiceModel;

    using System.Text;

    namespace Test

    {

    public class PrecinctService : IPrecinctService

    {

     

    public PrecinctBLEntity Create(PrecinctBLEntity data)

    {

    PrecinctFactory pf = PrecinctFactory.Instance;IPrecinct ip = pf.GetObject(); ;

     

    return ip.Create(data);

    }

    public PrecinctBLEntity Update(PrecinctBLEntity data)

    {

    PrecinctFactory pf = PrecinctFactory.Instance;

    IPrecinct ip = pf.GetObject();return ip.Update(data); ;

    }

    public void Delete(PrecinctBLEntity data)

    {

    PrecinctFactory pf = PrecinctFactory.Instance;

    IPrecinct ip = pf.GetObject();

     

    ip.Delete(data);

    }

    public List<PrecinctBLEntity> Read(PrecinctBLEntity data)

    {

    PrecinctFactory pf = PrecinctFactory.Instance;

    IPrecinct ip = pf.GetObject();

    return ip.Read(data);

    }

    public List<PrecinctBLEntity> PrecinctsData()

    {

    PrecinctFactory pf = PrecinctFactory.Instance;

    IPrecinct ip = pf.GetObject();

    return ip.PrecinctsData();

    }

     

    }

    }

    Below is the clientside code XAML.cs

    ObservableCollection<ServiceReference1.PrecinctBLEntity> _GameCollection =

    new ObservableCollection<ServiceReference1.PrecinctBLEntity>();

    private void Read_Click(object sender, RoutedEventArgs e)

    {

    ServiceReference1.
    PrecinctServiceClient ps = new WPFClient.ServiceReference1.PrecinctServiceClient();

    ServiceReference1.PrecinctBLEntity[] result = ps.PrecinctsData();

    _GameCollection = (ObservableCollection<ServiceReference1.PrecinctBLEntity>)result;// Error is here;

    //collect1 = ps.PrecinctsData().ToList();

    //_GameCollection = (System.Collections.ObjectModel.ObservableCollection<ServiceReference1.PrecinctBLEntity>)pc1;

     

    }

    Any help would be greatly appreciated.

    Application Developers

  • rajesh shirpuram

    rajesh shirp...

    Contributor

    2315 Points

    507 Posts

    Re: WCF service and return Type

    Jul 28, 2008 06:54 AM | LINK

    Right Click on Service ref in your solution and Click on Configure Service.

    Their you will be able to set return types of your service. Presently it might be returning an Generic List Collection Array .... change it to required.

    Cheers

     

    Thanks
    Rajesh Shirpuram

    (If this has answered your question, please click on "mark as answer" on this post. Thank you!)
  • the_cubanate

    the_cubanate

    Member

    95 Points

    54 Posts

    Re: Re: WCF service and return Type

    Jul 28, 2008 07:01 AM | LINK

    Rajesh is correct. You cannot return a List<t> like that. Have your procedures return either an array or IEnumerable<T>.

  • Re: WCF service and return Type

    Jul 28, 2008 11:36 PM | LINK

    I am still facing problem in casting.

    pls help

  • the_cubanate

    the_cubanate

    Member

    95 Points

    54 Posts

    Re: Re: WCF service and return Type

    Jul 29, 2008 01:48 AM | LINK

    This might not be the prettiest of solutions but it's worked for me before. Instead of:

    ServiceReference1.PrecinctBLEntity[] result = ps.PrecinctsData();

    do

    List<PrecinctBLEntity> result = new list<PrecintBLEntitu>(ps.PrecinctsData()); 

     

  • rajesh shirpuram

    rajesh shirp...

    Contributor

    2315 Points

    507 Posts

    Re: Re: Re: WCF service and return Type

    Jul 29, 2008 05:32 AM | LINK

    hi the_cubanate, thanks for the workaround .... i will make a note of it. :)

    Thanks
    Rajesh Shirpuram

    (If this has answered your question, please click on "mark as answer" on this post. Thank you!)
  • kunal2383

    kunal2383

    Contributor

    5604 Points

    1050 Posts

    Re: Re: WCF service and return Type

    Jul 29, 2008 09:27 AM | LINK

    You can do the following:

    foreach( PrecinctBLEntity pEntity in e.Result )
    {
                // do what u want with the record u fetched;
    }

    Because, in web service call though you are returning as a list, but it is internally casting it as an array.

    You can also change the default return type by right clicking on Service Reference -> Properties.

     

    (If this has answered your question, please click on "mark as answer" on this post. Thank you!)

    Remember: Please click "Mark As Answer", if this helped you.

    Regards - Kunal Chowdhury | My Blog | Silverlight-Zone