Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I have read several tutorials about WCF but they are always limited to a ServiceContract and a DataContract.

I have tried to use these attributes to experiment the following trial:

I always get a null reference when I request my object. If I do not define the Emloyee class members, this works fine, I can discover the employees collection via the debugger. I am using Visual Studio 2010 on Window 7 64bits. I am trying to proceed as follow:

[ServiceContract]
[KnownType(typeof(Employees))]
public interface IEmployeesService
{
   Employees Members{ [OperationContract()] get; }
}

public class EmployeesService : IEmployeesService
    {
        Employees _cEmployees  = new Employees();

        public Employees Members
        {
            get { return _cEmployees; }
        }
    }

[DataContract]
[KnownType(typeof(Employee))]
public class Employees
{
  [DataMember]
  private List<Employee> _cEmployees = null;

  public Dimensions()
  {
     _cEmployees = new List<Employee>();
  }

  public long Add(Employee Emp)
  {
     _cEmployees .Add(Emp);
     return _cEmployees .Count;
  }
}

[DataContract]
public class Employee
{
    private string m_Name;
    private int m_Age;
    private int m_Salary;
    private string m_Designation;
    private string m_Manager;

    [DataMember]
    public string Name
    {
        get { return m_Name; }
        set { m_Name = value; }
    }

    [DataMember]
    public int Age
    {
        get { return m_Age; }
        set { m_Age = value; }
    }
}

Server:
_Host = new ServiceHost(typeof(EmployeesService));

_tcpEndPoint = _Host.AddServiceEndpoint(
               typeof(IEmployeesService),
               new WSHttpBinding(),
               "http://localhost:9010/wcfEmployeesService");

// Start listening for messages.
_Host.Open();

Output are:
Launch wcf Employees service ...
The service is running and listening on
http://localhost:9010/wcfEmployeesService (WSHttpBinding)

Client:
ICarService channel = null;
UnitTable UnitTable = null;

EndpointAddress endPoint = new EndpointAddress(
                "http://localhost:9010/wcfEmployeesService");

channel = ChannelFactory<IEmployeesService>.CreateChannel(
                new WSHttpBinding(),
                endPoint);
 
Employees Members = null;
try
{
   Members = channel.Employees;
}
catch (Exception e)
{
   Console.WriteLine("Members {0}", e.Message.ToString());
   Console.ReadKey();
}


Do you know if this is possible ? What am I doing wrong ?
Thank you very much in advance for your help / tips.

Best regards.
MiQi
Posted
Updated 26-Sep-12 1:40am
v4
Comments
Ashraff Ali Wahab 25-Sep-12 21:50pm    
Have you created the proxy by adding the service as a service reference or using svcutil.

What is the problem in transmitting hierarchy of objects using WCF, we always do such kind of transfer isn't it.
SuperMiQi 26-Sep-12 7:35am    
Hello Sir,

I always get a null reference when I request my object. If I do not define the Emloyee class members, this works fine, I can discover the employees collection
via the debugger. I am using Visual Studio 2010 on Window 7 64bits.

I am trying to proceed as follow:

[ServiceContract]
[KnownType(typeof(Employees))]
public interface IEmployeesService
{
Employees Members{ [OperationContract()] get; }
}

public class EmployeesService : IEmployeesService
{
Employees _cEmployees = new Employees();

public Employees Members
{
get { return _cEmployees; }
}
}

[DataContract]
[KnownType(typeof(Employee))]
public class Employees
{
[DataMember]
private List<employee> _cEmployees = null;

public Dimensions()
{
_cEmployees = new List<employee>();
}

public long Add(Employee Emp)
{
_cEmployees .Add(Emp);
return _cEmployees .Count;
}
}

[DataContract]
public class Employee
{
private string m_Name;
private int m_Age;
private int m_Salary;
private string m_Designation;
private string m_Manager;

[DataMember]
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}

[DataMember]
public int Age
{
get { return m_Age; }
set { m_Age = value; }
}
}

Do you know if this is possible ? What am I doing wrong ?
Thank you very much in advance for your help / tips.

Best regards.
MiQi

1 solution

Firstly:

C#
public Dimensions()
  {
     _cEmployees = new List<employee>();
  }


This looks like a constructor but the method name doesn't match the class name?

Also, not sure you'd want a private member to be a DataMember?

C#
[DataMember]
  private List _cEmployees = null;


Ideally all DataMember's should be properties and not variables. Try this:

C#
[DataContract]
[KnownType(typeof(Employee))]
public class Employees
{

  private List<employee> _cEmployees = null;

  [DataMember]
  public List<employee> EmployeeList 
  { 
    get { return _cEmployees; } 
    set { _cEmployees = value; } 
  }
 
  public Employees()
  {
     _cEmployees = new List<employee>();
  }
 
  public long Add(Employee Emp)
  {
     _cEmployees .Add(Emp);
     return _cEmployees .Count;
  }
}


Although if you're simply returning a collection why bother with the Employees class at all?

WCF handles collections all on it's own without the need for a wrapper.
 
Share this answer
 
v2
Comments
SuperMiQi 26-Sep-12 8:27am    
Hello Stephen.

Thank for your quick reply. I have tried your solution but still cannot get access to the employees list from the client side. Is WCF forseen to apply .NET remoting like I was using with vs2005. I wanted to use WCF because it is easier to debug but it seems I will not reach the goal. Do you know if this is anyway possible ?
Thank you very much in advance.
Best regards.
MiQi
Stephen Hewison 26-Sep-12 8:31am    
Have you tried removing the KnownType attribute? I'm fairly sure you only need to add KnownTypes when implicit conversions are required. E.g. you have a List<object> data member. You'll have to list the KnownTypes for each of the objects which could be included in the collection.
SuperMiQi 26-Sep-12 9:15am    
Hello Stephen,

I am still struggling with these objects.

Now I am getting:
Employees An error occurred while receiving the HTTP response to http://localhos
t:9010/wcfEmployeesService. This could be due to the service endpoint binding no
t using the HTTP protocol. This could also be due to an HTTP request context bei
ng aborted by the server (possibly due to the service shutting down). See server
logs for more details.

Not very straight forward.

What could you advise me ?

Thank you very much in advance.
Best regards.
MiQi.
SuperMiQi 26-Sep-12 12:28pm    
Hello Stephen,

I have found how to use it.
In the datacontract we need to use the IsReference attribute to link the classes (in child / parent mode).

Thank you for you good tips.
Best regards.
MiQi.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900