C#:Webservice 更改预期参数类型(从普通 POCO 更改为自动生成的类)

发布于 2024-07-17 10:57:30 字数 466 浏览 9 评论 0原文

我在类库中有以下类: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法唧唧 2024-07-24 10:57:30

也许切换到 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.

佼人 2024-07-24 10:57:30

你不能也不应该解决这个问题。

其他一些人会告诉您执行诸如编辑生成的文件之类的操作,但这不是一个好的做法(因为一旦 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文