user575219
user575219

Reputation: 2442

Return a class of List objects from a wcf service and assign it on the Client

How do I return a class with List<T> objects from a wcf service to a client?
Can someone show me an example on how to return a class with some list objects and assign it to the client? I am getting this error on the client when I try to assign the class with Listobjects to local variables on the form

Error:  Cannot implicitly convert type 'System.Collections.Generic.List<TesterWCFService.ServiceRef.TypeCode>' to 'System.Collections.Generic.List<Project1.TypeCode>'  

Code:

public interface ICodesService 
{
    [OperationContract]
    CodesList LoadCodeData();
}

[Serializable]
[DataContract]
public class CodesList 
{
    [DataMember]
    public List<TypeCode> TypeCodes{ get; set; }
    [DataMember]
    public List<TypeCode1> TypeCodes1{ get; set; }
}

LoadCodes.svc

public class LoadCodesService : ICodesService 
{
    CodesList _AllCodes = new Codes();
    public CodesList LoadCodeData() {
        using (CodeEntities _codes = new CodeEntities()) {
            _AllCodes.TypeCodes= _codes.TypeCode.ToList();
            _AllCodes.TypeCodes1= _codes.TypeCodes.ToList();
        }
        return _AllCodes
    }
}

On the Client:

public class Codes 
{
     public List<TypeCode> TypeCodes{ get; set; }
     public List<TypeCode1> TypeCodes1{ get; set; }
}

This is the same class as CodesList on the ICodesService. I am declaring it at both placing ICodesService and the client. I want to be loading it on the webservice and assigning it on the client

private void button1_Click(object sender, EventArgs e) 
{
    public Codes _codesInProxy = new Codes();
    LoadCodesServiceReference.CodesServiceClient proxy =  new LoadCodesServiceReference.CodesServiceClient();
    proxy.CodesList _codesList;
    _codesList= proxy.LoadCodeData();//this one returns the codeslist from the service
    _codesInProxy.TypeCodes = codesList.TypeCodes.ToList() 
    // This one gives an error 
    //Now I would like assign it to the class on the client and use it
}

Error:  Cannot implicitly convert type 'System.Collections.Generic.List<TesterWCFService.ServiceRef.TypeCode>' to 'System.Collections.Generic.List<Project1.TypeCode>'  

Upvotes: 0

Views: 7294

Answers (2)

Louis van Tonder
Louis van Tonder

Reputation: 3710

You can/should use the same object reference as that created in the WCF service.

The wcf service will expose the objects defined in the interface, and you should create you client side objects, as references of the WCF objects.

When adding the service reference, Visual Studio already creates all the empty class structures, you can just use them.

Pseudo Code:

New var as wcfserviceReferenceInstance.object

Upvotes: 1

Kaspars Ozols
Kaspars Ozols

Reputation: 7017

It seems that problem is that you have two different definitions for storing call resulton client side - one generated by adding WCF Service Reference (proxy.CodesList) and another defined manually (Codes).

You don't need to re-define server side classes once more in client side. If you add Service Reference then all the data types will be generated automatically. Just change client side so you use proxy.CodesList everywhere.

Upvotes: 1

Related Questions