简体   繁体   English

自定义业务对象:启用AJAX的WCF

[英]Custom Business Object : AJAX Enabled WCF

Can I pass a custom object between AJAX enabled WCF and my asp.net page? 我可以在启用AJAX的WCF和asp.net页面之间传递自定义对象吗?

I searched the web but could not find any examples. 我在网上搜索,但找不到任何示例。 Most shows simple types like string and integers. 大多数显示简单类型,例如字符串和整数。

I also do not know how to populate custom object's property through JavaScript on the client side. 我也不知道如何在客户端通过JavaScript填充自定义对象的属性。

We have a browser add on and we have to pass data to that addon from a web service, I researched and looks like AJAX enabled WCF is way to go 我研究了一个附加浏览器,并且必须将数据从Web服务传递到该附加组件,并且看来启用AJAX的WCF是可行的方法

Using .net framework 3.5 and VS 2008 使用.net Framework 3.5和VS 2008

With .NET 3.5 your best bet is WebHttpBinding which accepts plain old XML (POX) and you need to send XML to the WCF service. 使用.NET 3.5,最好的选择是WebHttpBinding ,它接受纯旧的XML(POX),并且您需要将XML发送到WCF服务。

You can also use WCF REST using REST starter kit. 您还可以使用REST入门套件使用WCF REST。 For samples have a look here . 样品请看这里 This supports JSON as well. 这也支持JSON。

If you were using .NET 4.0, JSON-enabled WCF HTTP was the way to go. 如果使用的是.NET 4.0,则应采用启用JSON的WCF HTTP WCF REST with 4.0 was an alternative although I really do not like it. 带有4.0的WCF REST是替代方法,尽管我真的不喜欢它。

You can't pass the actual custom objects, but you can of course pass the serialized version of them through your service and to your page, javascript, etc. Basically, you have to map the fields of your complex custom .NET types to classes decorated with the DataContract attribute. 您不能传递实际的自定义对象,但是您当然可以通过服务将它们的序列化版本传递给页面,javascript等。基本上,您必须将复杂的自定义.NET类型的字段映射为类。用DataContract属性修饰。 These classes are the types that your service will return. 这些类是服务将返回的类型。 The DataContract-decorated classes will contain fields with primitive types, like strings, integers, etc. The WCF service will serialize these into XML or JSON. DataContract装饰的类将包含具有原始类型的字段,例如字符串,整数等。WCF服务会将它们序列化为XML或JSON。

On the client side, jQuery will be your best friend. 在客户端,jQuery将是您最好的朋友。 I personally prefer JSON because the properties of your objects are much easier to get at that way instead having to deal with parsing a bunch of XML. 我个人更喜欢JSON,因为通过这种方式可以更轻松地获取对象的属性,而不必处理大量XML。 So, setup your service to output JSON. 因此,将您的服务设置为输出JSON。

Also, to make your service URLs easier to read, make sure to use a RESTful approach. 另外,为了使您的服务URL易于阅读,请确保使用RESTful方法。 It's as easy as decorating your service methods with the WebGet attribute and supplying a UriTemplate. 就像使用WebGet属性装饰服务方法并提供UriTemplate一样简单。 Once you see some examples, it'll blow your mind. 一旦您看到了一些示例,就会大为震惊。 Note: if you ever encounter a WebInvoke with Method="GET", just use WebGet instead...it's more compact...no Method specification needed. 注意:如果遇到使用Method =“ GET”的WebInvoke,请改用WebGet ...它更紧凑...无需方法规范。

This particular article was EXTREMELY useful to me when I was developing my WCF service and the ASP.NET app that consumed it: http://www.c-sharpcorner.com/UploadFile/sridhar_subra/116/ 当我开发WCF服务和使用它的ASP.NET应用程序时,这篇特别的文章对我非常有用: http : //www.c-sharpcorner.com/UploadFile/sridhar_subra/116/

Here's another person asking the same question as you: http://social.msdn.microsoft.com/forums/en-US/wcf/thread/879d46af-9c78-4b5d-b746-82843d742a6f 这是另一个人问与您相同的问题: http : //social.msdn.microsoft.com/forums/zh-CN/wcf/thread/879d46af-9c78-4b5d-b746-82843d742a6f

Hope this helps! 希望这可以帮助! Long live WCF! WCF万岁!

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

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