Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅客户端的简单属性中的DataContract对象不会在调用之间丢失_C#_Web Services_Wcf_Datacontract - Fatal编程技术网

C# 仅客户端的简单属性中的DataContract对象不会在调用之间丢失

C# 仅客户端的简单属性中的DataContract对象不会在调用之间丢失,c#,web-services,wcf,datacontract,C#,Web Services,Wcf,Datacontract,我有这个代码,一个使用DataContract的服务。 主机构建在网站上。 请注意,服务处于持续模式: public interface IService { [OperationContract] int GetNewAge(Person person); } [DataContract] public class Person { private int age; [DataMember] public int Age {

我有这个代码,一个使用DataContract的服务。 主机构建在网站上。 请注意,服务处于持续模式:

public interface IService
{
    [OperationContract]
    int GetNewAge(Person person);
}


[DataContract]
public class Person
{
    private int age;
    [DataMember]
    public int Age
    {
        get { return age; }
        set { age = value; }
    }
    [DataMember]
    public int AgeNextYear
    {
        get { return age + 1; }
    }
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class Service : IService
{
    public int GetNewAge(Person person)
    {
        return person.AgeNextYear;
    }
}

The Client: Uses the type person:

ServiceClient c = new ServiceClient();
Person person = new Person { Age = 100 };
int curAge = person.Age;
int nextYearAge1 = person.AgeNextYear;
int nextYearAge2 = c.GetNewAge(person);
牧师-好的简单的属性很好用

nextYearAge1-0,而不是101

nextYearAge2-程序崩溃


有人能帮忙吗?非常感谢,Liron。

您的数据合同应该是数据合同。像
AgeNextYear
这样的逻辑不会被传输,并且没有代理类可以使用该逻辑

如果WCF对话的双方都是C#并且您使用的是数据协定程序集,那么您可以这样做。然后,只需删除
AgeNextYear
上的
[DataMember]
属性就可以了,因为逻辑是通过公共契约程序集共享的

例如:

[DataContract]
public class Person
{
    // this is plain data. It can be transfered back and forth,
    // other languages and frameworks will have no problem 
    // building proxy classes for it
    [DataMember]
    public int Age { get; set; }

    // this is not data. There is no data, there only is a calculation. 
    // That's logic. Logic cannot be transfered. Lets say your age is 18, 
    // then this is 19. But the point that this is not a fixed value of 19, 
    // but actually Age + 1, cannot be transfered. It's not data. It should 
    // not be part of the contract if you want this to be usable as a 
    // generic web service.
    [DataMember]
    public int AgeNextYear
    {
        get { return Age + 1; }
    }
}

你所说的“数据契约应该是数据契约”是什么意思?在客户机上使用“Person”是否可以?对数据的强调意味着你的数据契约类应该只是一个数据容器-没有行为\逻辑应该是你的数据契约类的一部分。您当然可以在客户端上使用
Person
类,但以一种改进的方式-删除逻辑或在线路两侧共享数据协定类型的程序集。当从WSDL\XSD元数据自动生成时,数据协定类不能具有行为\逻辑的技术原因特别是因为XSD只描述DTO的结构,而不描述逻辑。