在 .Net 2 中使用 WCF 时,int 属性为 0

编程入门 行业动态 更新时间:2024-10-22 21:18:09
本文介绍了在 .Net 2 中使用 WCF 时,int 属性为 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我在 .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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 20:02:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141093.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   Net   WCF   int

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!