C# wcf json 网络服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/824331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:05:27  来源:igfitidea点击:

wcf json web service

c#.netwcfjson

提问by Grzenio

What is the best way to create a JSON web service? We have another team that is using Java and they insist to having all communication done using JSON. I would prefer to use WCF rather than any 3rd party framework.

创建 JSON Web 服务的最佳方法是什么?我们有另一个使用 Java 的团队,他们坚持使用 JSON 完成所有通信。我更喜欢使用 WCF 而不是任何 3rd 方框架。

I found this blog: http://www.west-wind.com/weblog/posts/164419.aspx, and it suggests that the Microsoft implementation is flawed with M$ specific crap.

我发现了这个博客:http: //www.west-wind.com/weblog/posts/164419.aspx,它表明微软的实现存在缺陷,有 M$ 特定的废话。

采纳答案by Grzenio

I ended up using JayRock. Its fantastic piece of technology, just works. You don't get any NullReferenceExceptions like from this crap WCF if you don't configure it correctly.

我最终使用了JayRock。其出色的技术,恰到好处。如果你没有正确配置它,你不会从这个垃圾 WCF 中得到任何 NullReferenceExceptions。

回答by W. Kevin Hazzard

If you use WCF and the 3.5 Framework, it couldn't be easier. When you mark your OperationContracts with the WebGet attribute, just set the ResponseFormat parameter to WebMessageFormat.Json. When the service is accessed RESTfully, it will return the data using the DataContractJsonSerializer.

如果您使用 WCF 和 3.5 框架,那就再简单不过了。当您使用 WebGet 属性标记您的 OperationContracts 时,只需将 ResponseFormat 参数设置为 WebMessageFormat.Json。当以 REST 方式访问服务时,它将使用 DataContractJsonSerializer 返回数据。

It's really helpful to mark the POCOs that you want to JSON serialize as [DataContract] and to mark each serializable member as [DataMember]. Otherwise, you end up with funky JSON, as Rick pointed out in his blog post.

将要 JSON 序列化的 POCO 标记为 [DataContract] 并将每个可序列化成员标记为 [DataMember],这真的很有帮助。否则,就像 Rick 在他的博客文章中指出的那样,你最终会得到时髦的 JSON。

回答by mythz

I maintain a mature Open Source alternative to WCF in ServiceStack, a modern, code-first, model-driven, WCF replacement web services framework encouraging code and remote best-practicesfor creating terse, DRY, high-perfomance, scalable REST web services.

我在ServiceStack 中维护了 WCF 的成熟开源替代方案,这是一个现代、代码优先、模型驱动、WCF 替换 Web 服务框架,鼓励代码和远程最佳实践来创建简洁、干燥、高性能、可扩展的 REST Web 服务

It includes .NET's fastest JSON Serializerand has automatic support JSON, JSONP, CORSheaders as well as form-urlencoded/multipart-formdata. The Online Demos are a good start to look at since they all use Ajax.

它包括.NET 最快的 JSON 序列化程序,并自动支持 JSON、JSONP、CORS标头以及 form-urlencoded/multipart-formdata。在线演示是一个很好的开始,因为它们都使用 Ajax。

In addition, there's no XML config, or code-gen and your 'write-once' C# web service provides all JSON, XML, SOAP, JSV, CSV, HTMLendpoints enabled out-of-the-box, automatically with hooks to plug in your own Content Typesif needed.

此外,没有 XML 配置或代码生成,您的“一次编写”C# Web 服务提供所有JSON、XML、SOAP、JSV、CSV、HTML端点,开箱即用,自动带有挂钩以插入如果需要,在您自己的内容类型中

It also includes generic sync/async service clientsproviding a fast, typed, client/server communication gateway end-to-end.

它还包括通用同步/异步服务客户端,提供快速、类型化、端到端的客户端/服务器通信网关。

This is the complete example of all the code needed to create a simple web service, that is automatically without any config, registered and made available on all the web data formats on pre-defined and custom REST-ful routes:

这是创建简单 Web 服务所需的所有代码的完整示例,无需任何配置即可自动注册并在预定义和自定义 REST-ful 路由上的所有Web 数据格式上可用:

public class Hello {
    public string Name { get; set; }
}

public class HelloResponse {
    public string Result { get; set; }
}

public class HelloService : IService<Hello> {
    public object Execute(Hello request)
    {
        return new HelloResponse { Result = "Hello, " + request.Name };
    }
}

Above service can be called (without any build-steps/code-gen) in C# with the line below:

可以在 C# 中使用以下行调用上述服务(无需任何构建步骤/代码生成):

var client = new JsonServiceClient(baseUrl);
var response = client.Send<HelloResponse>(new Hello { Name = "World!" });
Console.WriteLine(response.Result); // => Hello, World

And in jQuery with:

在 jQuery 中:

$.getJSON('hello/World!', function(r){ 
    alert(r.Result); 
});

回答by Mike Gledhill

What is the best way to create a JSON web service? We have another team that is using Java and they insist to having all communication done using JSON. I would prefer to use WCF rather than any 3rd party framework.

创建 JSON Web 服务的最佳方法是什么?我们有另一个使用 Java 的团队,他们坚持使用 JSON 完成所有通信。我更喜欢使用 WCF 而不是任何 3rd 方框架。

Here's an easy-to-follow walkthrough, which takes you through the process of setting up your first WCF Service, then linking it to a SQL Server database.

这是一个易于遵循的演练,它带您完成设置第一个 WCF 服务,然后将其链接到 SQL Server 数据库的过程。

http://mikesknowledgebase.com/pages/Services/WebServices-Page1.htm

http://mikesknowledgebase.com/pages/Services/WebServices-Page1.htm

It uses Microsoft's beloved NorthwindSQL Server database, and shows how to write a simple JSON WCF Web Service to read and write it's data.

它使用微软深受喜爱的NorthwindSQL Server 数据库,并展示了如何编写一个简单的 JSON WCF Web 服务来读取和写入它的数据。

enter image description here

在此处输入图片说明

Oh, and it then shows how to consume the JSON data using JavaScript or an iOS application.

哦,然后它展示了如何使用 JavaScript 或 iOS 应用程序使用 JSON 数据。

Good luck !

祝你好运 !