Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WCF C#4.0 NET
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 25-Sep-12 7:24am
Edited 26-Sep-12 1:40am
v4
Comments
Ashraff Ali Wahab at 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 at 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 _cEmployees = null;
 
public Dimensions()
{
_cEmployees = new List();
}
 
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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Firstly:
 
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?
 
[DataMember]
  private List _cEmployees = null;
 
Ideally all DataMember's should be properties and not variables. Try this:
 
[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.
  Permalink  
v2
Comments
SuperMiQi at 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 at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 6,656
1 Sergey Alexandrovich Kryukov 6,400
2 CPallini 5,240
3 George Jonsson 3,584
4 Gihan Liyanage 2,625


Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 26 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100