C#:Webservice 更改预期参数类型(从普通 POCO 更改为自动生成的类)
我在类库中有以下类:Artist
,它是一个 POCO
现在我在 Web 服务中有一个方法(它引用了上面提到的库),其签名如下:
[WebMethod]
public int Artist_AddArtist(Artist a) {
//
}
当我尝试从应用程序(也引用上述类库)使用此服务时,Artist_AddArtist
方法的预期参数不是Artist
,但是在 Reference.cs
中生成了一种新的 Artist
类型,它是自动生成的部分类。
因此,由于在我的应用程序中,我使用了库中的相同 Artist
类,并且现在 Web 服务方法需要这个新的自动生成类型,因此我无法将其实例传递给 Web 服务。
我该如何解决这个问题?
I have the following class in Class Library: Artist
, which is a POCO
Now I have a method in a web-service (which has a reference to the mentioned-above library) with a signature like this:
[WebMethod]
public int Artist_AddArtist(Artist a) {
//
}
When I try to consume this service from an application (that also has a reference to the mentioned-above Class library), the expected parameter of the Artist_AddArtist
method is not Artist
, but a new type of Artist
that is being generated in Reference.cs
which is a partial class that is auto-generated.
Thus since in my application I use, supposedly the same Artist
class from the library and now the Web Service method expects this new auto generated type, I cannot pass an instance of it to the web-service.
How can I fix this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许切换到 WCF 服务是您的一个选择。 据我记得,使用 WCF 服务,您可以在服务器和客户端上重用相同的类型。
本文介绍如何将 ASMX Web 服务迁移到 WCF 服务。
Maybe switching to WCF services is an option for you. As far as I remember, with a WCF service, you can reuse the same types on ther server and client side.
This article explains how to migrate an ASMX web service to a WCF service.
你不能也不应该解决这个问题。
其他一些人会告诉您执行诸如编辑生成的文件之类的操作,但这不是一个好的做法(因为一旦 Web 参考更新,更改就会消失)。
你所看到的是设计使然。 请参阅基础知识:Web 服务的工作原理。
简而言之,当您使用“添加 Web 引用”时,Visual Studio 会从服务下载 WSDL 文件,并使用 WSDL 中的 XML 架构来创建一些代理类来表示该架构所描述的 XML。 它还为服务本身创建一个代理类,其中包含服务中每个操作的方法。
代理数据类可以序列化为服务期望接收的 XML,并且可以从服务器回复发送的 XML 反序列化回来。
一种思考方式是,您只会遇到这个问题,因为客户端和服务都是 .NET。 如果您的客户端是用 Java 编写的,那么您就不会考虑共享类。
请注意,如有必要,WCF 可以执行此操作。 它引入了客户端和服务之间的依赖关系(它们都必须使用包含类的程序集的兼容版本),但是当您需要这样做时,选项就在那里。 当这些类中存在必须由客户端和服务使用的行为时,它非常有用。
You cannot, and should not, fix the problem.
Some others will tell you to do things like edit the generated file, but that's not a good practice (as the changes will go away as soon as the Web Reference is updated).
What you're seeing is by design. See Basics: How Web Services Work.
Briefly, when you use "Add Web Reference", Visual Studio downloads the WSDL file from the service, and uses the XML Schemas from the WSDL to create some proxy classes to represent the XML described by the schema. It also creates a proxy class for the service itself, having methods for each operation in the service.
The proxy data classes can serialize to the XML that the service is expecting to receive, and can be deserialized back from the XML that the server sends in reply.
One way to think of it is that you only have this problem because both client and service are .NET. If your client were written in Java, then you wouldn't be thinking of sharing classes.
Note that WCF can do this, if necessary. It introduces a dependency between the client and service (they both have to use compatible versions of the assembly containing the classes), but when you need to do it, the option is there. It's useful when there is behavior in these classes that must be used both by the client and by the service.