在WCF中返回自己的对象 [英] Returning Own Object in WCF

查看:55
本文介绍了在WCF中返回自己的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我已经阅读了一些有关WCF的教程,但它们始终仅限于ServiceContract和DataContract.

我尝试使用这些属性来进行以下试验:

当我请求对象时,我总是得到一个空引用.如果我没有定义Emloyee类成员,那么效果很好,我可以通过调试器发现employee集合.我在64位Windows 7上使用Visual Studio 2010.我正在尝试进行以下操作:

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();


输出为:
启动wcf员工服务...
该服务正在运行并正在监听
http://localhost:9010/wcfEmployeesService(WSHttpBinding)

客户:


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();
}



你知道这有可能吗?我做错了什么?
非常感谢您的帮助/提示.

最好的问候.
MiQi



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

推荐答案

首先:

Firstly:

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



看起来像构造函数,但方法名称与类名称不匹配?

另外,不确定您是否希望私人成员成为DataMember?



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;



理想情况下,所有DataMember都应该是属性,而不是变量.试试这个:



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;
  }
}



尽管如果您只是返回一个集合,为什么还要完全使用Employees类呢?

WCF无需包装即可自行处理所有集合.



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.


这篇关于在WCF中返回自己的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆