将 WCF 服务连接到外部 API [英] Connecting a WCF service to an external API
问题描述
我需要通过 API 将 WCF 服务连接到外部 WCF 服务,该服务在 JSON<中返回数据/strong> 格式.
I need to connect a WCF service to an external WCF service via an API which is returning data in the JSON format.
我一直在寻找 wsdl 和 wadl 来实现这一点,但我不确定它们是否在外部服务上实现或如何访问它们.
I have been looking at wsdl and wadl to achieve this but I am not sure whether they were implemented on the external service or how to go about accessing them.
已在外部服务上启用.
<serviceMetadata>
has been enabled on the external service.
从我目前看到的情况来看,wsdl 似乎已经过时并且只与 SOAP 兼容,这听起来对吗?所以这是正确的,我自然更喜欢使用 wadl.
From what I have seen so far wsdl seems to be outdated and only compatible with SOAP, does that sound right? So this being correct, I would naturally prefer to use wadl.
这些是我唯一的选择吗?如果是,是否有任何好的指南来指导如何实施这些?
Are these my only options and if so are there any good guides that walk through how to implement these?
谢谢.
推荐答案
这是基于我在工作中实施并修改(但未测试)以使用 JSON 的精简版本(基于此处的其他一些答案和在网络上):
This is based on a stripped down version of something I implemented at work and modified (but not tested) to work with JSON (based on some other answers here and on the web):
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("http://somedomain.com/serviceAddress").Result;
string responseContent = response.Content.ReadAsStringAsync().Result;
有很多方法可以做到这一点,但上面的代码展示了做到这一点是多么容易(或者至少应该是).
There are numerous ways to do this, but the above code demonstrates how easy it is (or should be, at least) to do this.
请注意,我为异步调用使用了 Result
属性;如果您在标记为 async
的方法中进行此调用,您还可以执行以下操作:
Note that I used the Result
property for the async calls; if you're making this call from within a method marked as async
, you could also do:
HttpResponseMessage response = await client.GetAsync("http://somedomain.com/serviceAddress");
string responseContent = await response.Content.ReadAsStringAsync();
HttpClient
位于 System.Net.Http
命名空间中.
HttpClient
is in the System.Net.Http
namespace.
这篇关于将 WCF 服务连接到外部 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!