简体   繁体   English

将WCF服务连接到外部API

[英]Connecting a WCF service to an external API

I need to connect a WCF service to an external WCF service via an API which is returning data in the JSON format. 我需要通过返回JSON格式数据的APIWCF服务连接到外部WCF服务。

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. 我一直在研究wsdlwadl以实现此目的,但是我不确定它们是在外部服务上实现还是如何访问它们。

<serviceMetadata> has been enabled on the external service. <serviceMetadata>已在外部服务上启用。

From what I have seen so far wsdl seems to be outdated and only compatible with SOAP , does that sound right? 从到目前为止我所看到的, wsdl似乎已经过时并且仅与SOAP兼容,听起来正确吗? So this being correct, I would naturally prefer to use wadl . 因此,这是正确的,我自然会更喜欢使用wadl

Are these my only options and if so are there any good guides that walk through how to implement these? 这些是我唯一的选择吗?如果是,是否有任何好的指南可以指导您实现这些方法?

Thanks. 谢谢。

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): 这是基于我在工作中实现并简化(但未经测试)以与JSON结合使用的简化版本(基于此处和网络上的一些其他答案):

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. 有很多方法可以执行此操作,但是上面的代码演示了执行此操作的容易程度(至少应该如此)。

Note that I used the Result property for the async calls; 注意,我对异步调用使用了Result属性。 if you're making this call from within a method marked as async , you could also do: 如果要从标记为async的方法中进行此调用,则还可以执行以下操作:

HttpResponseMessage response = await client.GetAsync("http://somedomain.com/serviceAddress");

string responseContent = await response.Content.ReadAsStringAsync();

HttpClient is in the System.Net.Http namespace. HttpClientSystem.Net.Http命名空间中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM