WCF 将对象返回给客户端 [英] WCF returning Objects to Client

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

问题描述

我正在尝试使用 WCF,但我想我遇到了障碍.我的问题是我可以从客户端"调用 Add(double,double)getPerson() .但是,我无法调用 Person 对象的任何方法.我用裸方法精简了类.这是我的代码片段,请让我知道我做错了什么..

I am trying to play around with WCF and I think I've hit a block. My issue is that I am able to call Add(double,double) and getPerson() from the "Client". However, I am not able to call any methods of Person object. I've stripped down the classes with bare methods. Here are my code snippets, please let me know what I am doing wrong..

服务器代码

  namespace Test.WebSvc{
  [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Sample")]
  public interface ICalculator
  {
    [OperationContract]
    double Add(double n1, double n2);
    [OperationContract]
    Person getPerson();
  }


 public class CalculatorService : ICalculator
 {
    public double Add(double n1, double n2) { return n1+n2 ; }
    public Person getPerson(){ 
    Person tempPerson = new Person();
    return tempPerson; 
    }
 }

 [DataContract]
 public class Person{
 [OperationContractAttribute]
 public string toString(){
 return "This is a Person Object";
 }

客户端代码

ServiceRef1.CalculatorClient client = ServiceRef1.CalculatorClient();//works
Console.WriteLine(client.Add(1.0,2.0)); //this too works   
ServiceRef1.Person p = client.getPerson(); // this is OK., but is not doing what I wanted it to do
Console.WriteLine(p.toString()); // I do not get "This is Person Object"

我猜我的 Person 类的声明有问题..但是我不知道我应该做什么或我遗漏了什么..

I am guessing something's wrong with my Person class' declaration.. but dint get a clue what should I do or what I am missing..

谢谢!

推荐答案

您在 Person 类型中混淆了两个概念——您所做的将不起作用.

You are mixing up two concepts with your Person type -- what you're doing will not work.

您已在 Person 类型上放置了 DataContract 属性.这是正确的,因为您有一个返回 Person 的服务.Person 对象将被序列化并返回到您的服务客户端(在本例中为 CalculatorClient).

You've put a DataContract attribute on the Person type. This is correct, because you have a service that is returning a Person. The Person object will be serialized and returned to your service client (CalculatorClient in this case).

你应该像这样定义Person:

[DataContract]
public class Person
{
    [DataMember]
    public string Description { get; set; }
}

在您的计算器服务中:

public Person getPerson()
{ 
    Person tempPerson = new Person();
    tempPerson.Description = "This is a Person Object";
    return tempPerson; 
}

这是因为您的 Person 对象的工作是保存数据,并将其从服务器传送到客户端.定义方法/操作不是它的工作,而应该在您的服务类(例如CalculatorService)中完成.添加OperationContract 属性并不会神奇地将数据传输对象变成服务.

This is because your Person object's job is to hold data, and carry it from server to client. It is not its job to define methods / operations, which should instead be done in your Service classes (eg CalculatorService). Adding the OperationContract attribute does not magically turn the Data Transfer Object into a Service.

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

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