没有默认构造函数的类通过SOAP传递 [英] Class with no default constructor being passed through SOAP

查看:58
本文介绍了没有默认构造函数的类通过SOAP传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个没有默认构造函数的类。该类的本质本质上排除了它与一个人并且将其中一个人公然歪曲它正在建模的对象。


所以说到这一点,我该如何去做当SOAP要求类具有默认构造函数时通过SOAP?

在ASP.NET 2中,错误消息是< class>无法序列化,因为它没有无参数构造函数 (请注意拼写错误/拼写错误)ASP.NET 1.x中出现类似的错误


(实现我自己的IXmlSerializable接口无效。我试过了。)


问候

Dave A

解决方案

Dave,


您收到此错误的原因是因为Web服务使用XmlSerialization

来发送您的对象 - 这意味着您的类受所有绑定

限制XmlSerializer加上它 - 默认公共构造函数是一个




你如何解决它?那么你不应该绕过它。但是如果你必须使用pub-def构造函数将这个类包装在另一个类中,并且

实现了IXmlSerializable来自定义xml序列化行为。在

一个End to End .NET商店中,您甚至可以使用

BinaryFormatter发送一个字节流。


或者你可以只是发送一个字节流作为函数返回和

序列化/反序列化使用binaryformatter - 虽然这假设您的

客户端有.NET,这打破了Web服务的理念。 />

- Sahil Malik [MVP]

即将发布的ADO.NET 2.0书籍 - http://tinyurl.com/9bync

---------------------- -------------------------------------------------- ----

---------------


" Dave A" <哒** @ sigmasolutionsdonotspamme.com.au>在消息中写道

新闻:ux ************** @ TK2MSFTNGP15.phx.gbl ...

我有一个课程没有默认构造函数。

类的性质固有地将其排除在一个并将其放入其中

公然歪曲它正在建模的对象。


所以说过,当SOAP需要这个类有默认构造函数的
时,如何让它通过SOAP?


In ASP.NET 2的错误消息是< class>无法序列化因为它没有参数构造函数b $ b没有参数化构造函数 (请注意拼写错误/拼写错误

错误)ASP.NET 1.x中出现类似的错误


(实现我自己的IXmlSerializable接口无效我试过了

吧。)


问候

戴夫A


< blockquote> Dave,


您收到此错误的原因是因为Web服务使用XmlSerialization

来发送您的对象 - 这意味着您的类受所有绑定

限制XmlSerializer加上它 - 默认的公共构造函数是一个




你如何解决它?那么你不应该绕过它。但是如果你必须使用pub-def构造函数将这个类包装在另一个类中,并且

实现了IXmlSerializable来自定义xml序列化行为。在

一个End to End .NET商店中,您甚至可以使用

BinaryFormatter发送一个字节流。


或者你可以只是发送一个字节流作为函数返回和

序列化/反序列化使用binaryformatter - 虽然这假设您的

客户端有.NET,这打破了Web服务的理念。 />

- Sahil Malik [MVP]

即将发布的ADO.NET 2.0书籍 - http://tinyurl.com/9bync

---------------------- -------------------------------------------------- ----

---------------


" Dave A" <哒** @ sigmasolutionsdonotspamme.com.au>在消息中写道

新闻:ux ************** @ TK2MSFTNGP15.phx.gbl ...

我有一个课程没有默认构造函数。

类的性质固有地将其排除在一个并将其放入其中

公然歪曲它正在建模的对象。


所以说过,当SOAP需要这个类有默认构造函数的
时,如何让它通过SOAP?


In ASP.NET 2的错误消息是< class>无法序列化因为它没有参数构造函数b $ b没有参数化构造函数 (请注意拼写错误/拼写错误

错误)ASP.NET 1.x中出现类似的错误


(实现我自己的IXmlSerializable接口无效我试过了

吧。)


问候

戴夫A


< blockquote> Sahil,


我已经进一步研究了它,实际上有一个非常优雅的工作

,这让我的设计和SOAP都很开心。 />

您创建一个默认构造函数,但用...装饰它...

[已废弃("此构造函数在内部用于SOAP通信和

无法通过代码调用。",true)]

public myClass()

{

}


无法调用此构造函数,因为它会导致

编译错误,但SOAP很乐意调用它。我测试了它

并且它有效。 (它需要我反对的ASP.NET 2)


问候

戴夫A


" Sahil Malik [MVP]" <共***************** @ nospam.com>在消息中写道

新闻:O1 ************* @ TK2MSFTNGP09.phx.gbl ...

戴夫,

你得到这个错误的原因是因为Web服务使用XmlSerialization来发送你的对象 - 这意味着你的类受到XmlSerializer对它的所有限制的约束 - 默认的公共构造函数是
你如何解决这个问题?那么你不应该绕过它。但是如果你必须将这个类用pub-def构造函数包装在另一个类中,并实现IXmlSerializable来自定义xml序列化行为。在一个端到端.NET商店中,您甚至可以使用二进制格式发送一个字节流。

或者您可以发送一个字节流作为函数返回和
使用binaryformatter序列化/反序列化 - 虽然这假设你的
客户端有.NET,这破坏了Web服务理念。

- Sahil Malik [MVP]
即将发布的ADO.NET 2.0书籍 - http://tinyurl.com/9bync
-------------------------------------------------- --------------------------
---------------

Dave A <哒** @ sigmasolutionsdonotspamme.com.au>在消息中写道
新闻:ux ************** @ TK2MSFTNGP15.phx.gbl ...
我有一个没有默认构造函数的类。
类的本质本质上将它排除在一个并且将一个人放入其中将公然歪曲它正在建模的对象。

所以说过,我该怎么做当SOAP要求类具有默认构造函数时,让它通过SOAP?

在ASP.NET 2中,错误消息是< class>无法序列化,因为它没有参数化构造函数 (请注意拼写错误/拼写错误)ASP.NET 1.x中出现类似的错误

(实现我自己的IXmlSerializable接口没有帮助。我试过< 。

问候
戴夫A



I have a class that does not have a default constructor. The nature of the class inherently excludes it from having one and to put one in will blatantly misrepresent the object that it is modelling.

So having said that, how do I get it to go through SOAP when SOAP requires that the class has a default constructor?

In ASP.NET 2 the error message is "<class> cannot be serialized because it does not have a parametless constructor" (please note the typo/spelling error) A similar error occurs in ASP.NET 1.x

(Implementing my own IXmlSerializable interface that does not help. I tried it.)

Regards
Dave A

解决方案

Dave,

The reason you get this error is because Web services use XmlSerialization
to send your objects across - which means your class is bound by all
restrictions XmlSerializer puts on it - default public constructor being one
of them.

How do you get around it? Well you should not get around it. But if you
must, wrap this class in another class with a pub-def-constructor, and
implement IXmlSerializable to customize the xml serialization behavior. In
an End to End .NET shop you may even send a stream of bytes using
BinaryFormatter.

Or you can just send a bytestream as a function return and
serialize/deserialize using binaryformatter - though that assumes that your
client has .NET, which breaks the web service philosophy.

- Sahil Malik [MVP]
Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync
----------------------------------------------------------------------------
---------------

"Dave A" <da**@sigmasolutionsdonotspamme.com.au> wrote in message
news:ux**************@TK2MSFTNGP15.phx.gbl...
I have a class that does not have a default constructor. The nature of the
class inherently excludes it from having one and to put one in will
blatantly misrepresent the object that it is modelling.

So having said that, how do I get it to go through SOAP when SOAP requires
that the class has a default constructor?

In ASP.NET 2 the error message is "<class> cannot be serialized because it
does not have a parametless constructor" (please note the typo/spelling
error) A similar error occurs in ASP.NET 1.x

(Implementing my own IXmlSerializable interface that does not help. I tried
it.)

Regards
Dave A


Dave,

The reason you get this error is because Web services use XmlSerialization
to send your objects across - which means your class is bound by all
restrictions XmlSerializer puts on it - default public constructor being one
of them.

How do you get around it? Well you should not get around it. But if you
must, wrap this class in another class with a pub-def-constructor, and
implement IXmlSerializable to customize the xml serialization behavior. In
an End to End .NET shop you may even send a stream of bytes using
BinaryFormatter.

Or you can just send a bytestream as a function return and
serialize/deserialize using binaryformatter - though that assumes that your
client has .NET, which breaks the web service philosophy.

- Sahil Malik [MVP]
Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync
----------------------------------------------------------------------------
---------------

"Dave A" <da**@sigmasolutionsdonotspamme.com.au> wrote in message
news:ux**************@TK2MSFTNGP15.phx.gbl...
I have a class that does not have a default constructor. The nature of the
class inherently excludes it from having one and to put one in will
blatantly misrepresent the object that it is modelling.

So having said that, how do I get it to go through SOAP when SOAP requires
that the class has a default constructor?

In ASP.NET 2 the error message is "<class> cannot be serialized because it
does not have a parametless constructor" (please note the typo/spelling
error) A similar error occurs in ASP.NET 1.x

(Implementing my own IXmlSerializable interface that does not help. I tried
it.)

Regards
Dave A


Sahil,

I have looked into it further and there is actually a very elegant work
around that keeps both my design and SOAP happy.

You create a default constructor but adorn it with...

[Obsolete("This constructor is used internally for SOAP communications and
cannot be called through code.", true)]
public myClass()
{
}

It is not possible to call this constructor because it will result in a
compilation error but SOAP is only too happy to call it. I have tested it
and it works. (It requires ASP.NET 2 which I am targetting anyway)

Regards
Dave A

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:O1*************@TK2MSFTNGP09.phx.gbl...

Dave,

The reason you get this error is because Web services use XmlSerialization
to send your objects across - which means your class is bound by all
restrictions XmlSerializer puts on it - default public constructor being
one
of them.

How do you get around it? Well you should not get around it. But if you
must, wrap this class in another class with a pub-def-constructor, and
implement IXmlSerializable to customize the xml serialization behavior. In
an End to End .NET shop you may even send a stream of bytes using
BinaryFormatter.

Or you can just send a bytestream as a function return and
serialize/deserialize using binaryformatter - though that assumes that
your
client has .NET, which breaks the web service philosophy.

- Sahil Malik [MVP]
Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync
----------------------------------------------------------------------------
---------------

"Dave A" <da**@sigmasolutionsdonotspamme.com.au> wrote in message
news:ux**************@TK2MSFTNGP15.phx.gbl...
I have a class that does not have a default constructor. The nature of the
class inherently excludes it from having one and to put one in will
blatantly misrepresent the object that it is modelling.

So having said that, how do I get it to go through SOAP when SOAP requires
that the class has a default constructor?

In ASP.NET 2 the error message is "<class> cannot be serialized because it
does not have a parametless constructor" (please note the typo/spelling
error) A similar error occurs in ASP.NET 1.x

(Implementing my own IXmlSerializable interface that does not help. I
tried
it.)

Regards
Dave A



这篇关于没有默认构造函数的类通过SOAP传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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