本文目录一览:
怎么用javascript连接webservice
直接上示例代码 : MyService.asmx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
/// summary
/// MyService 的摘要说明
/// /summary
[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService {
public MyService () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public XmlNode xml() {
XmlDocument doc = new XmlDocument();
doc.LoadXml("<hi>Hello World</hi>");
return doc.FirstChild;
}
[WebMethod]
public string add(int a, int b)
{
return (a + b)+"";
}
}
aspx页面
<html>
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
// <![CDATA[
function Button1_onclick() {
var data;
data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\""
+ ">"
+ "<soap12:Body>"
+ "<HelloWorldResponse xmlns=\"\">"
+ "<HelloWorldResult>string</HelloWorldResult>"
+ "</HelloWorldResponse>"
+ "</soap12:Body>"
+ "</soap12:Envelope>";
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var URL = "MyService.asmx";
xmlhttp.Open("POST", URL, false);
xmlhttp.SetRequestHeader("Content-Type", "text/xml; charset=gb2312");
xmlhttp.SetRequestHeader("SOAPAction", "");
xmlhttp.setRequestHeader("Content-Length", data.length);
xmlhttp.Send(data);
var xml = xmlhttp.responseText;
var doc = new ActiveXObject("Microsoft.XMLDOM");
doc.loadXML(xml);
//alert(doc.selectSingleNode("//HelloWorldResult").text);
//alert(doc.xml);
alert(doc.selectSingleNode("//HelloWorldResponse").childNodes[0].nodeName);
}
function add() {
var a = 10;
var b = 15;
var data;
data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\""
+ ">"
+ "<soap12:Body>"
+ "<add xmlns=\"\">"
+ "<a>" + a + "</a>"
+ "<b>" + b + "</b>"
+ "</add>"
+ "</soap12:Body>"
+ "</soap12:Envelope>";
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var URL = "MyService.asmx";
xmlhttp.Open("POST", URL, false);
xmlhttp.SetRequestHeader("Content-Type", "text/xml; charset=gb2312");
xmlhttp.SetRequestHeader("SOAPAction", "");
xmlhttp.setRequestHeader("Content-Length", data.length);
xmlhttp.Send(data);
var xml = xmlhttp.responseText;
alert(xml);
var doc = new ActiveXObject("Microsoft.XMLDOM");
doc.loadXML(xml);
}
function getxml() {
var a = 10;
var b = 15;
var data;
data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\""
+ ">"
+ "<soap12:Body>"
+ "<add xmlns=\"\"/>"
+ "</soap12:Body>"
+ "</soap12:Envelope>";
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var URL = "MyService.asmx";
xmlhttp.Open("POST", URL, false);
xmlhttp.SetRequestHeader("Content-Type", "text/xml; charset=gb2312");
xmlhttp.SetRequestHeader("SOAPAction", "");
xmlhttp.setRequestHeader("Content-Length", data.length);
xmlhttp.Send(data);
var xml = xmlhttp.responseText;
alert(xml);
var doc = new ActiveXObject("Microsoft.XMLDOM");
doc.loadXML(xml);
}
// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="button" onclick="getxml()" />
</div>
</form>
</body>
</html>
LINQ的原理浅析
LINQ(Language Integrated Query)是Visual Studio 2008中的领军人物。借助于LINQ技术,我们可以使用一种类似SQL的语法来查询任何形式的数据。目前为止LINQ所支持的数据源有SQL Server、Oracle、XML(标准通用标记语言下的一个应用)以及内存中的数据集合。开发人员也可以使用其提供的扩展框架添加更多的数据源,例如MySQL、Amazon甚至是Google Desktop。
一般来讲,这类查询语句的一个重要特点就是可以并行化执行。虽然有些情况下并行可能会带来一些问题,但这种情况非常少见。这样也就水到渠成地引出了PLINQ这个并行处理的LINQ类库。
PLINQ原名为Parallel LINQ,支持XML和内存中的数据集合。执行于远程服务器上的查询语句(例如LINQ to SQL)显然无法实现这个功能。
将LINQ语句转换为PLINQ语句极为简单——只需要在查询语句中From子句所指定的数据源的最后添加.AsParallel()
即可。随后Where、OrderBy和Select子句将自动改为调用这个并行的LINQ版本。
据MSDN Magazine介绍,PLINQ可以以三种方式执行。第一种是管道处理:一个线程用来读取数据源,而其他的线程则用来处理查询语句,二者同步进行——虽然这个单一的消费线程可能并不那么容易与多个生产线程同步。不过若是能够仔细配置好负载平衡的话,仍然会极大地减少内存占用。
第二种模式叫做“stop and go”,用于处理结果集需要被一次返回时(例如调用ToList、ToArray或对结果排序)的情况。在这种模式下,将依次完成各个处理过程,并将结果统一返回给消费线程。这个模式在性能上将优于第一种模式,因为它省去了用来保持线程同步所花费的开销。
最后一种方法叫做“inverted enumeration”。该方法并不需要实现收集到所有的输出,然后在单一的线程中处理,而是将最终调用的函数通过ForAll扩展传递到每个线程中。这是目前为止最快的一种处理模式,不过这需要传递到ForAll中的函数是线程安全的,且最好不包含任何lock之类的互斥语句。
若是PLINQ中任意的一个线程抛出异常,那么所有的其他线程将会被终止。若是抛出了多个异常,那么这些异常将被组合成一个MultipleFailuresException类型的异常,但每个异常的调用堆栈仍会被保留。
linq查询语句
where lbbn.length == 2
就行了
var table = cache_cplb.Where(o => o.lbbh.length == 2).ToList();
foreach (var row in table) {
}