简体   繁体   English

WCF REST API返回.net应用程序类型

[英]WCF REST API returned type .net application

I have an API that can returns multiple things that I don't know in advance so I have to put as return type System.Object . 我有一个API,它可以返回很多我事先不知道的东西,因此我必须将其作为返回类型System.Object放置。

I have some problems for some types. 对于某些类型,我有一些问题。 Like when it returns different objects its fine but when it returns an System.Int32 , it doesn't work and tells me: 就像当它返回不同的对象时很好,但是当它返回System.Int32 ,它不起作用并告诉我:

Cannot serialize parameter of type 'System.Int32' because it is not the exact type 'System.Object' in the method signature and is not in the known types collection.

Any idea ? 任何想法 ?

I know we can use ServiceKnownType but I'd like to know if there is other way to do it? 我知道我们可以使用ServiceKnownType,但是我想知道是否还有其他方法可以做到? I would need all the existing types (int, bool, string, etc..) in this ... 我将需要所有现有的类型(int,bool,字符串等)。

Do not return System.Object type on service contracts, even when it WCF 4 REST. 即使在WCF 4 REST上,也不要在服务合同上返回System.Object类型。 How default serializers/formatters could produce response for your contract. 默认序列化器/格式器如何对您的合同产生响应。

You can return byte array, convert your System.Object into string and return it. 您可以返回字节数组,将System.Object转换为字符串并返回它。 Anyway you can always serialize your objects to XML or JSON and return as string. 无论如何,您始终可以将对象序列化为XML或JSON并以字符串形式返回。

Also link to forum with same question, maybe it helps you: http://social.msdn.microsoft.com/Forums/nl-NL/wcf/thread/1f43e9b4-39a4-481b-88f0-18c7d4416745 也可以通过相同的问题链接到论坛,也许可以帮助您: http : //social.msdn.microsoft.com/Forums/nl-NL/wcf/thread/1f43e9b4-39a4-481b-88f0-18c7d4416745

You can make a simple if statement that checks to see if it is a int and then you can convert it to a object. 您可以创建一个简单的if语句,以检查它是否为int,然后可以将其转换为对象。 By the way integers are not able to become objects til they are a string so if I did this I would do a simple: 顺便说一下,整数直到成为字符串都无法成为对象,所以如果这样做,我将做一个简单的事情:

blah.toString();

and bam you can now use it as a object. 和bam,您现在可以将其用作对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM