在 .Net 2 中使用 WCF 时,int 属性为 0 [英] int properties are 0 when consuming WCF in .Net 2

查看:21
本文介绍了在 .Net 2 中使用 WCF 时,int 属性为 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .Net 4 中有一个带有 WCF 服务的 MVC 项目和 BasicHttpBinding.

I have an MVC project in .Net 4 with WCF service with BasicHttpBinding.

在 .Net 2 中使用此服务时,如果属性为 int,则到达的值为 0.

When consuming this service in .Net 2 the values that arriving if the property is int are 0.

如果它是一个字符串,那就没问题了.

if it is a string, than it goes fine.

在 .Net 4 中构建一个新项目,使用相同的服务并使用确切的实现(如 .Net 2)==> int 值是正确的.

bulding a new project in .Net 4 consuming the same service and using the exact implementation (as the .Net 2) ==> the int values are correct.

为什么?

谢谢!

推荐答案

我敢打赌你有一个具有实际 int 属性的数据契约:

I bet you have a data contract that has the actual int property:

public int YourProperty ......

以及旁边的 YourPropertySpecified 属性:

public bool YourPropertySpecified ......

由于 int 不能为 null,WCF 无法区分您是否定义了一个值 - 您需要告诉它.

Since an int cannot be null, WCF cannot distinguish whether or not you have defined a value - you need to tell it.

因此,如果您使用 int 属性并为其设置值 - 您需要将其随附的 YourPropertySpecified 属性设置为 true:

So if you use an int property and set a value to it - you also need to set its accompanying YourPropertySpecified property to true:

yourData.YourProperty = 42;
yourData.YourPropertySpecified = true;

通过这个额外的步骤,int 值应该可以很好地到达服务器

With this extra step, the int values should arrive at the server just fine

这篇关于在 .Net 2 中使用 WCF 时,int 属性为 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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