WCF WebAPI 客户端不知道服务器类型

发布于 2024-12-23 10:05:42 字数 270 浏览 2 评论 0原文

我正在关注 WCF Web API chm 文件的 .6 版本。我已经构建了我的服务,当我通过 IE 访问它时一切正常。但是当我创建控制台应用程序时,我不明白客户端如何知道“联系人”类型。当然,我可以添加参考,但是世界上的其他客户如何知道这些类型?

List<Contact> contacts = resp.Content.ReadAs<List<Contact>>();

客户如何知道 Contact 类的更改?谢谢。

I am following along in the .6 release of the WCF Web API chm file. I have built my service and everything works fine when I access it via IE. But when I create my console app, I don't understand how the client can know about the "contact" type. Sure I can add a reference, but how would some other client out there in the world know about the types?

List<Contact> contacts = resp.Content.ReadAs<List<Contact>>();

How would a client know about changes to the Contact class?Thanks.

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

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

发布评论

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

评论(2

温馨耳语 2024-12-30 10:05:42

使用基于 SOAP 的 WCF 绑定,客户端通常会生成一个基于 WSDL 的客户端,该客户端将指定这些自定义类型。

但是据我所知,在基于 REST 的 Web API 世界中,没有任何工具可以做到这一点。期望制作客户端的第 3 方客户/程序员以某种其他形式给出数据契约,并制作兼容的类。

换句话说,并没有真正自动的方法来做到这一点。

Using the SOAP based WCF bindings, the client would normally generate a client off the WSDL, which would specify these custom types.

However as far as I know, in the REST based world of Web API, there is no facility for doing that. It is expected that the 3rd party customer / programmer making the client is given the data contract in some other form, and makes a compatible class.

In other words, there is not really an automatic way of doing that.

雨后咖啡店 2024-12-30 10:05:42

客户端类型上与响应类型中的属性(名称/类型)匹配的每个属性均由 ReadAs映射。

如果您的响应类型和客户端类型有一个字符串属性“Name”,则正在解析其值。

你不需要参考。

更新:如果您不想在客户端使用联系人类型,您可以尝试如下操作:

var json = JsonValue.Parse(response.Content.ReadAsStringAsync().Result);

如果服务器端的联系人类型有一个属性“名称”,您应该能够执行以下操作:(

var name = json["Name"];

假设您的回复是单个联系人 - 如果 List的类型为 JsonArray - 您应该得到线索... 此处 是显示 JsonValue 和 JsonArray 用法的示例)。

关于“联系类型的更改”,请阅读此内容

Every property on your client type that matches a property (Name/Type) in the response type is mapped by ReadAs<T>.

If you have a string property "Name" on your response type and your client type, its value is being parsed.

You don't need a reference.

Update: If you don't want to work with a contacts type on the client side you could try something like this:

var json = JsonValue.Parse(response.Content.ReadAsStringAsync().Result);

If your contact type on the server side had a property "Name" you should be able to do the following:

var name = json["Name"];

(Assuming your response was a single contact - in case of List<Contact> "json" would be of type JsonArray - you should get a clue... here is a sample showing usage of JsonValue and JsonArray).

Concerning "changes on contact type" please read this.

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