wcf ajax,Creating WCF AJAX Services without ASP.NET

Creating WCF AJAX Services without ASP.NET

03/30/2017

2 minutes to read

In this article

Windows Communication Foundation (WCF) AJAX services can be accessed from any JavaScript-enabled Web page, without requiring ASP.NET AJAX. This topic describes how to create such a WCF service.

For instructions on using WCF with ASP.NET AJAX, see Creating WCF Services for ASP.NET AJAX.

There are three parts to a creating a WCF AJAX service:

Creating an AJAX endpoint that can be accessed from the browser.

Creating an AJAX-compatible service contract.

Accessing WCF AJAX services.

Creating an AJAX Endpoint

The most basic way to enable AJAX support in a WCF service is to use the WebServiceHostFactory in the .svc file associated with the service, as in the following example.

language=c#

Debug="true"

Service="Microsoft.Ajax.Samples.CityService"

Factory=System.ServiceModel.Activation.WebServiceHostFactory

%>

Alternatively, you can also use configuration to add an AJAX endpoint. Use the WebHttpBinding on the service endpoint and configure that endpoint with the WebHttpBehavior as shown in the following code snippet.

address="ajaxEndpoint"

behaviorConfiguration="AjaxBehavior"

binding="webHttpBinding"

contract="Microsoft.Ajax.Samples.ICityService" />

For a working example, see the AJAX Service with JSON and XML.

Creating an AJAX-Compatible Service Contract

By default, service contracts exposed over an AJAX endpoint return data in the XML format. Also, by default service operations are accessible through HTTP POST requests to URLs that include the endpoint address followed by the operation name, as shown in the following example.

[OperationContract]

string[] GetCities(string firstLetters);

This operation is accessible using an HTTP POST to http://serviceaddress/endpointaddress/GetCities and return an XML message.

You can use the full Web Programming Model to customize these basic aspects. For example, you can use the WebGetAttribute or WebInvokeAttribute attributes to control the HTTP verb to which the operation responds or use the UriTemplate property of these respective attributes to specify custom URIs. For more information, see the WCF Web HTTP Programming Model topic.

The JSON data format is often used in AJAX services. To create an operation that returns JSON instead of XML, set the ResponseFormat (or the ResponseFormat) property to Json. The Stand-Alone JSON Serialization topic shows how built-in .NET types and data contract types map to JSON.

Normally, JSON requests and responses consist of just one item. For the preceding GetCities operation, the request resembles the following statement.

"na"

The response to that request resembles the following statement.

["Nairobi", "Naples", "Nashville"]

If the operation takes an extra parameter, the request style must be wrapped to wrap both parameters in a single JSON object. An example of this style JSON message is in the following example.

{"firstLetters": "na", "maxNumber": 2}

The following contract accepts this message.

[WebInvoke(BodyStyle=WebMessageBodyStyle.WrappedRequest, ResponseFormat=WebMessageFormat.Json)]

[OperationContract]

string[] GetCities(string firstLetters, int maxNumber);

Accessing AJAX Services

WCF AJAX endpoints always accept both JSON and XML requests.

HTTP POST requests with a content-type of "application/json" are treated as JSON, and those with content-type that indicate XML (for example, "text/xml") are treated as XML.

HTTP GET requests contain all the request parameters in the URL itself.

It is up to the user to decide how to create the HTTP request to the endpoint. Also, the user has full control over constructing the JSON that forms the body of the request. For an example of creating a request from JavaScript, see the AJAX Service with JSON and XML.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: .Net WCF面试题是关于.NET WCF(Windows Communication Foundation)的面试题。WCF是一种用于构建分布式应用程序的微软技术。它提供了一种统一的编程模型,使开发人员能够使用不同的传输协议和编码方式来进行通信。WCF面试题可能涉及到WCF的基本概念、架构、服务契约、终结点等方面的内容。在回答这些问题时,可以引用.NET Framework的相关知识,如.NET Framework是微软的一个应用程序开发框架,它包含了许多不同的组件,如ASP.NET、ADO.NET、Windows Forms等。此外,还可以引用HttpHandler的概念,它是ASP.NET中用于处理传入Http请求的低级API。List<T>和Dictionary<K,V>是.NET Framework中常用的集合类型,它们有不同的特点和用途。List<T>是一个动态数组,可以按索引访问元素,而Dictionary<K,V>是一个键值对集合,可以通过键来访问值。它们的区别和优势可以根据具体的需求来进行比较和说明。 #### 引用[.reference_title] - *1* *3* [互联网面试——.Net 面试题](https://blog.csdn.net/Cool2Feel/article/details/118000408)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [.NET软件开发工程师面试题(上)](https://blog.csdn.net/m0_56366948/article/details/127974031)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值