我们系统中的常用类型,都有对应的序列化器来支持。至于客户端和服务器端的远程调用,我们通过Asp.net AJAX扩展的web service来实现,先看看服务器端的代码:
namespace StepByStep.Forms
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class AJAXService : System.Web.Services.WebService
{
public class WebServerInfo
{
public DateTime ServerTime
{
get;
set;
}
//如果想在序列化时忽略此属性,请打开下一行的注释
//[ScriptIgnore]
public string ServerInformation
{
get;
set;
}
}
[WebMethod]
public WebServerInfo GetServerInfo()
{
WebServerInfo result = new WebServerInfo();
result.ServerTime = DateTime.Now;
result.ServerInformation = GetServerInformation();
return result;
}
private static string GetServerInformation()
{
StringBuilder strB = new StringBuilder();
using (TextWriter writer = new StringWriter(strB))
{
writer.WriteLine("MachineName: {0}", Environment.MachineName);
writer.WriteLine("OS Version: {0}", Environment.OSVersion.VersionString);
writer.WriteLine("Is 64 bits: {0}", Environment.Is64BitOperatingSystem.ToString());
writer.WriteLine("Processor Count: {0}", Environment.ProcessorCount);
}
return strB.ToString();
}
}
}
再看看客户端的代码:
<asp:ScriptManager runat="server" ID="scriptManager" EnableScriptGlobalization="true">
<Services>
<asp:ServiceReference Path="~/Forms/AJAXService.asmx" />
</Services>
</asp:ScriptManager>
<SOA:SubmitButton runat="server" Text="Get Server Info" AsyncInvoke="onGetServerInfo" />
<script type="text/javascript">
function onGetServerInfo() {
StepByStep.Forms.AJAXService.GetServerInfo(onGetServerInfoSuccess, onFailed);
//这个名字空间需要和服务器端对应
return false;
}
function onGetServerInfoSuccess(serverInfo) {
$get("serverInfoText").innerText = serverInfo.ServerTime;
$get("serverInfoText").innerText += "\n" + serverInfo.ServerInformation;
SubmitButton.resetAllStates();
}
function onFailed(e) {
SubmitButton.resetAllStates();
$showError(e);
}
</script>
在这个过程中,涉及到的对象序列化,都会遵循Asp.net AJAX的JSON序列化机制。关于JSON序列化,还有很多等待着自己去研究,继续钻研......