WCF - 传递给服务操作时丢失对象属性的值 [英] WCF - Losing value of a property of an object while passing to Service Operation

查看:123
本文介绍了WCF - 传递给服务操作时丢失对象属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VS2012 / .NET 4.0,WCF服务,将对象传递给服务



我试图从客户端调用添加服务操作来创建区域。不知何故,客户端代码不接受Region对象,在通过代码调试时,我看到RegionID显示为零,尽管我在调用Add操作之前在客户端中分配了一个值。



客户代码:

VS2012/.NET 4.0, WCF Service, Passing an Object to Service

I am trying to call an Add service operation from client to create a Region. Somehow, the client code is not accepting the Region object and when debugging through code I see the RegionID is displayed as zero although I assigned a value in client before the call to Add operation.

Client Code:

RegionServiceClient objClient = new RegionServiceClient("BasicHttpBinding_IRegionService");

    RegionServiceProxy.RegionData objRegion = new RegionServiceProxy.RegionData();
    objRegion.RegionID = 5;
    objRegion.RegionDescription = "New Region"
    int newID = objClient.Add(objRegion);







服务运营代码:




Service Operation Code:

public int Add(RegionData objRegion)
{
    // objRegion.RegionID shows up as 0
    // objRegion.RegionDescription shows up as "New Region"
}





问题:

- 为什么我会丢失RegionID的值?

- 我错过了什么吗?



这是我的数据合约:





Question:
- Why am I losing the value for RegionID?
- Am I missing something?

Here is my Data Contract:

[DataContract]
    public class RegionData
    {
        [DataMember]
        public int RegionID = 0;
        [DataMember]
        public string RegionDescription = string.Empty;
    }







请帮忙!




Please help!

推荐答案

我根据代码创建了一个示例应用程序,一切正常,请查看以下详细信息:



[ServiceContract]

公共接口IService1

{

[OperationContract]

int Add(RegionData objRegion);

}



[DataContract]

公共类RegionData

{

[DataMember]

public int RegionID = 0;

[DataMember]

public string RegionDescription = string.Empty;

}



公共类服务1:IService1

{

public int Add(RegionData objRegion)

{

// objRegion.RegionID显示为0

// objRegion.RegionDescription显示为新区域



return objRegion.RegionID;

}

}





private void button1_Click (对象发送者,EventArgs e)

{

ServiceReference1.Service1Client cl = new ServiceReference1.Service1Client();

ServiceReference1.RegionData data = new ServiceReference1.RegionData();



data.RegionID = 20; //设置你想要的任何int值

data.RegionDescription =测试说明;





int abc = cl.Add(data); //输出高于int值即20.

}









检查您的代码并找出问题所在。
I created a sample app as per code and all works fine, check below details:

[ServiceContract]
public interface IService1
{
[OperationContract]
int Add(RegionData objRegion);
}

[DataContract]
public class RegionData
{
[DataMember]
public int RegionID = 0;
[DataMember]
public string RegionDescription = string.Empty;
}

public class Service1 : IService1
{
public int Add(RegionData objRegion)
{
// objRegion.RegionID shows up as 0
// objRegion.RegionDescription shows up as "New Region"

return objRegion.RegionID;
}
}


private void button1_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client cl = new ServiceReference1.Service1Client();
ServiceReference1.RegionData data = new ServiceReference1.RegionData();

data.RegionID = 20; // Set whatever int value you want
data.RegionDescription = "Testing descriptions";


int abc = cl.Add(data); //the output comes out to be above int value i.e. 20.
}




Review your code and find out what is going wrong.


这是一个更新,基于以下文章:

http://stackoverflow.com/questions / 2046206 /如何防止指定属性生成在wcf客户端 [ ^ ]



当我添加以下属性而不仅仅是[DataMember],我能够看到我的服务操作中的值 - 添加(objRegion)并将数据添加到数据库中。



[DataMember(IsRequired = true)]



仍在研究为什么我们需要IsRequired在所有领域Data Contract为true,但是如果我们不将IsRequired添加或设置为false,那么除了字符串字段之外,您还为Data Contract的所有字段生成了Specified属性,如果未设置Spefified属性,则值在service方法中显示为零对于那些领域都是如此。
Here is an update, based on following article:
http://stackoverflow.com/questions/2046206/how-to-prevent-specified-properties-being-generated-in-wcf-clients[^]

When I add following attribute instead of just [DataMember], I was able to see the value in my service operation - Add(objRegion) and data is being added into the database.

[DataMember(IsRequired=true)]

Still on research why we need IsRequired on all the fields of Data Contract to true, but if we don't add or set IsRequired to false, then you have Specified properties generated for all fields of Data Contract except string fields and the value shows up as zero in service method if you do not set Spefified properties to be true for those fields.


这篇关于WCF - 传递给服务操作时丢失对象属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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