在 WCF 服务中使用 C# 编写 json?
我正在考虑向我的 WCF 服务添加 SMS 功能。我发现了一个名为 Penny SMS 的廉价短信服务。
他们的界面支持 json。但我不知道如何在我的 WCF 服务中调用它。
以下是接口/示例:
示例 JSON-RPC 请求
{ "method": "send",
"params": [
"YOUR_API_KEY",
"[email protected]",
"5551231234",
"Test Message from PENNY SMS"
]
}
如何使用 C# 从 WCF 服务调用该请求?我正在寻找一种将其包装到方法调用中的方法。类似于:
StaticSMSClass.SendSMS("1234567890", "My Message to send");
请注意,如果使用 C# 更可行,它们还支持 XML-RPC API。
更新:我尝试自己创建一个呼叫,但没有成功。我将在一个单独的问题中发布我的尝试,看看是否有人有办法做到这一点。
I am looking at adding SMS abilities to my WCF service. I found a cheap SMS service called Penny SMS.
Their interface supports json. But I have no idea how to call it in my WCF service.
Here is the interface/example:
Sample JSON-RPC Request
{ "method": "send",
"params": [
"YOUR_API_KEY",
"[email protected]",
"5551231234",
"Test Message from PENNY SMS"
]
}
How would I call that with C# from a WCF service? What I am looking for is a way to wrap this into a method call. Something like:
StaticSMSClass.SendSMS("1234567890", "My Message to send");
Note they also support an XML-RPC API if that is more doable from C#.
UPDATE: I made a stab at creating a call myself, but it did not work. I am going to post my attempt in a separate question and see if anyone has a way to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要将带有 JSON 消息的 HTTP POST 发送到远程服务器。您可以使用 HttpWebRequest 来执行此操作。您可以手动构建 JSON(消息看起来很简单),或者为其定义类型并使用 JSON 序列化器。
MSDN 有一个示例,对于您的情况,它看起来像(未经测试):
You need to send a HTTP POST with the JSON message to the remote server. You can do this with HttpWebRequest. You either build the JSON manually (the messages seem simple), or define types for it and use a JSON serializer.
MSDN has an example, for your case it would look something like (untested):
请参阅我对问题的回答“使用 WCF JSON Web 服务的客户端配置”了解如何使用 WCF 创建 JSON 客户端。
See my answer to the question "Client configuration to consume WCF JSON web service" for how the create a JSON client with WCF.
到目前为止的答案都很好,但是您可以利用的另一件事(因为您位于 WCF 服务中)是使用 DataContractJsonSerializer。
特别是,我提到了如何在 driis 示例的第一行中实际填充 JSON。
现在,最好的方法之一可能是创建一个包含这些成员的新类:
然后,只需每次创建 SomeType 的实例,并在每次要发送一段数据时使用 DataContractJsonSerializer 将其序列化为 JSON 。请参阅 http://msdn.microsoft.com/en-us/library/bb412179。 aspx 了解如何独立使用 DataContractJsonSerializer。
希望这有帮助!
The answers so far are good, but one additional thing you can take advantage of (since you are within a WCF service) is the use of DataContractJsonSerializer.
In particular, I refer to how you actually populate your JSON in the first line of driis's example.
Now, one of the best ways to do this may be to create a new class with these members:
Then, just create an instance of SomeType every time and serialize it to JSON using the DataContractJsonSerializer every time you want to send over a piece of data. See http://msdn.microsoft.com/en-us/library/bb412179.aspx for how to use the DataContractJsonSerializer stand-alone.
Hope this helps!
检查 WCF REST API。他们提供 JSON,也许他们也可以发送 JSON(在内部 WCF 解决方案中它可以工作)。也许您必须在 wsdl 中构建合约才能使服务运行,但也许它可以成功。
Check the WCF REST API. They serve JSON, maybe they also can send JSON (in intra WCF solution it works). Maybe you have to construct the contract in wsdl to get the service running but maybe it works out.