将WCF与JavaScript一起使用,但对所有客户端而言是否具有足够的通用性? [英] Consume WCF with JavaScript but keep it generic enough for all clients?

查看:63
本文介绍了将WCF与JavaScript一起使用,但对所有客户端而言是否具有足够的通用性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的客户创建一个Web服务,以便他们可以在自己的网站上显示其数据.由于我不知道每个客户端都在运行什么平台,因此,为所有浏览器都可以使用的WCF服务创建代理的最佳解决方案是什么?另外,我不确定如何显示数据.假设我的用户没有任何开发技能.我将通过其他界面,使用户能够下载创建请求所需的代码,然后处理响应.最好在客户端解析xml响应,然后创建数据列表,或者已对列表进行格式化(以字符串形式),并让客户端执行document.write?我看过一些解决方案,但是它们似乎需要使用带有脚本管理器的ASP页面.就像我说的那样,我想要足够通用的东西来使用不同的浏览器.主要是IE和FireFox.

I want to create a web service for my clients so they can display their data on their own web sites. Since I will not know what platform each client is running, what would be the best solution to create a proxy to the WCF service that can be used by all browsers? Also, I am not sure how I should present the data. Let's assume my users do not have any development skills. I will, by some other interface, give the user the ability to download the code needed to create the request and then process the response. Would it be better to parse an xml response on the client side then create the list of data or have the list already formatted (in a string) and have the client do a document.write? I have looked at a few solutions, but they seem to require the use of an ASP page with a script manager. Like I said, I would like something generic enough to use different browsers. Mainly IE and FireFox.

丹尼尔

推荐答案

首先,由于您不想依赖Microsoft Ajax ScriptManager,因此请不要使用< enableWebScript/>在EndpointBehaviors/behavior中.这是Microsoft特有的JSON.

First, since you don't want to depend on Microsoft Ajax ScriptManager, don't use <enableWebScript /> in the endpointBehaviors/behavior. It is Microsoft-specific JSON.

但是,幸运的是,WCF使得允许您的客户端决定他们想要XML还是通用JSON变得非常容易.

Fortunately, however, WCF makes it very easy to allow your client to decide whether they want XML or generic JSON.

  1. 使用< webHttp/> 行为.

< endpointBehaviors>
< behavior name ="My.WcfServices.webHttpBehavior">
< webHttp/>
</行为>
</endpointBehaviors>

<endpointBehaviors>
<behavior name="My.WcfServices.webHttpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>

创建自定义WebServiceHost和自定义属性属性,如下所述 Damian Mehers的博客,WCF REST服务.在Mehers的代码中,类型由请求内容类型确定.您可能需要扩展它以检查URL,例如.xml或.json或?format = xml | json.

Create a custom WebServiceHost and custom property attribute as described in Damian Mehers' blog, WCF REST Services. In Mehers' code, the type is determined by the request content type. You may want to extend it to examine the URL, for example, .xml or .json or ?format=xml|json.

SerializeReply 方法中,检查URL.

In the SerializeReply method, examine the URL.

消息请求= OperationContext.Current.RequestContext.RequestMessage;
Uri url = request.Properties ["OriginalHttpRequestUri"] as Uri;
//检查?format查询字符串
System.Collections.Specialized.NameValueCollection colQuery = System.Web.HttpUtility.ParseQueryString(url.Query);
字符串strResponseFormat = colQuery ["format"];
//或检查扩展名
字符串strResponseFormat = url.LocalPath.Contains(.json")吗? "json":"xml";

Message request = OperationContext.Current.RequestContext.RequestMessage;
Uri url = request.Properties["OriginalHttpRequestUri"] as Uri;
// Examine ?format query string
System.Collections.Specialized.NameValueCollection colQuery = System.Web.HttpUtility.ParseQueryString(url.Query);
string strResponseFormat = colQuery["format"];
// or examine extension
string strResponseFormat = url.LocalPath.Contains(".json") ? "json" : "xml";

定义您的方法

[OperationContract]
[WebGet(UriTemplate ="Hello.{responseFormat}")]]//或"Hello?format = {responseFormat}"
[DynamicResponseType]
公共字符串Hello(string responseFormat)
{
返回"Hello World";
}

[OperationContract]
[WebGet(UriTemplate="Hello.{responseFormat}")] // or "Hello?format={responseFormat}"
[DynamicResponseType]
public string Hello(string responseFormat)
{
return "Hello World";
}

URL示例:
http://localhost/myrest.svc/Hello.xml
http://localhost/myrest.svc/Hello.json

http://localhost/myrest.svc/Hello?format = xml
http://localhost/myrest.svc/Hello?format = json

Example URLs:
http://localhost/myrest.svc/Hello.xml
http://localhost/myrest.svc/Hello.json
or
http://localhost/myrest.svc/Hello?format=xml
http://localhost/myrest.svc/Hello?format=json

  1. JSON和XML都易于在浏览器中使用.库,例如JSON的jQuery和XML的Sarissa,使它变得更加容易.

注意:如果看到错误找不到与绑定WebHttpBinding的端点的方案http匹配的基地址.",请添加 baseAddressPrefixFilters 元素,然后将localhost(或任何域)添加到IIS主机头名称.

NOTE: If you see error "Could not find a base address that matches scheme http for the endpoint with binding WebHttpBinding.", add the baseAddressPrefixFilters element and add localhost (or whatever your domain) to IIS Host Header Names.

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
    <baseAddressPrefixFilters>
        <add prefix="http://localhost"/>
    </baseAddressPrefixFilters>
</serviceHostingEnvironment>

这篇关于将WCF与JavaScript一起使用,但对所有客户端而言是否具有足够的通用性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆