WCF 客户端(添加服务引用)讨厌 WebGet 和 WebInvoke...真的,确实如此

发布于 2024-08-08 21:55:58 字数 805 浏览 8 评论 0原文

我一直致力于创建独立于 .Net 客户端运行的 WCF 服务。感谢 Google 和 StackOverflow,我已经能够创建简单的 xml 和 json 服务,而无需 Soap 包装器和一堆我不需要的花哨的 WCF 东西。这是一次痛苦的经历,因此才有了这个问题的主题。当使用 WebGet 和 WebInvoke 自动添加服务引用时,WCF 在客户端存在严重错误。

为了检查通信,我在本地创建了一个 WCF 客户端,并通过 Fiddler 传递所有内容。这样,无论是否有效,我至少可以看到客户端试图发送的内容。当它最终起作用时,我可以看到从两端发送的数据,然后在非 .Net 客户端中复制此通信。

我当前的问题是,当我更改服务以期望 POST 数据为 json(启用WebScript 行为)时,客户端不知道,并且它仍然尝试将对象作为 xml 发送。在使用“添加服务引用”时,我遇到了很多客户端配置无法自动正确设置的问题,因此我希望我可以将其添加到客户端上的 app.config 中。当使用 XML 时,我在服务中创建和使用的对象会自动由客户端进行 xml 序列化(这是最方便的)。在当前版本的 WCF 中,是否可以将其作为 json 来执行?

应该指出的是,我能够弄清楚我需要手动做什么,并让它以原始形式与 Fiddler(请求生成器)一起工作,因此我可以在代码中序列化我的对象并通过 http post 手动发送数据。 ..无论如何,这就是我在非.Net 客户端中执行此操作的方式。这更多的是一个更好地理解 WCF 方面的问题,以及为什么我在客户端缺少如此多的属性,而客户端几乎没有任何文档可以解决这些问题。

I have been working to create WCF services that will operate independent of .Net clients. Thanks to Google and StackOverflow, I have been able to create both simple xml and json services without Soap wrappers and a bunch of fancy WCF stuff that I just don't need. It has been a painful experience, hence the subject line of this question. WCF is mad buggy on the client side when using WebGet and WebInvoke when automatically adding the service reference.

To inspect the communication, I've been creating a WCF client locally and passing everything through Fiddler. That way, whether it works or not, I can at least see what the client is trying to send. And when it finally does work, I can see the data being sent from both ends and then duplicate this communication in a non-.Net client.

My current problem is that when I change the service to expect POST data as json (enableWebScript behavior), the client has no idea, and it still tries to send the objects as xml. I've had a ton of issues with the client's config not automatically being set properly when using Add Service Reference, so I'm hoping it's something simple I can add to the app.config on the client. When using XML, the objects I create and use in the service are automatically xml serialized by the client (which is most convenient). Is that even possible to do as json in the current version of WCF?

It should be noted that I was able to figure out what I need to do manually and get it to work in a raw form with Fiddler (request builder), so I can serialize my objects in code and send the data manually via http post...that's how I'm doing it in my non-.Net clients anyway. This is more of a question to understand the WCF aspects better and why I'm missing so many attributes on the client side where there's little to no documentation available to address the issues.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

许你一世情深 2024-08-15 21:55:58

WCF 服务引用适用于自描述的 RPC 有效负载 - 即 SOAP、wsHttp 等。同样,WCF 强类型客户端仅适用于 RPC 有效负载,因为只有它们能够广播其所需的所有类型信息等。正常工作。

当您使用 webget 和 webinvoke 时,您正在创建非 rpc 服务(旨在编写 REST 服务),这些服务也不是自描述的,因此它不太适合服务引用功能。

您当然可以为此编写一个 .Net 客户端 - 但您会发现使用 WebClient/WebRequest 编写它要容易得多,手动格式化/读取 XML/Json 请求/响应(或使用 DataContractSerializer 和 DataContractJsonSerializer 来帮助解决那)。

WCF service references are for RPC payloads that are self-describing - i.e. SOAP, wsHttp etc. Equally WCF strongly-typed clients are only intended to work with the RPC payloads because only they are capable of broadcasting all the type information etc required for it to work correctly.

When you use webget and webinvoke you are creating non-rpc services (intended to write REST services) which are also not self-describing and therefore it's not ideally suited for the service reference functionality.

You can of course write a .Net client for this - but you'll find it a lot easier to write it using WebClient/WebRequest, manually formatting/reading the XML/Json requests/responses (or use DataContractSerializer and DataContractJsonSerializer to help out with that).

寂寞清仓 2024-08-15 21:55:58

SOAP 是自描述的(通过 WSDL)。

WebGet/WebInvoke 不会公开任何告诉客户端使用 JSON 而不是 XML 的元数据。

SOAP is self-describing (through a WSDL).

WebGet/WebInvoke do not expose any metadata that would tell the client to use JSON instead of XML.

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