上一篇 / 下一篇 2008-05-23 16:13:38 / 个人分类:个人总结
智能设备程序开发和部署
// 1. 使用 WebClient 下载 WSDL 信息。
WebClient web = new WebClient();
Stream stream = web.OpenRead("http://192.168.183.162/emes/WebServiceProxy/QualityEntry.asmx?WSDL");
ServiceDescription description = ServiceDescription.Read(stream);
ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
importer.Style. = ServiceDescriptionImportStyle.Client; // 生成客户端代理。
importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
CodeNamespace nmspace = new CodeNamespace(); // 为代理类添加命名空间,缺省为全局空间。
CodeCompileUnit unit = new CodeCompileUnit();
unit.Namespaces.Add(nmspace);
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
parameter.GenerateExecutable = false;
parameter.GenerateInMemory = true;
parameter.ReferencedAssemblies.Add("System.dll");
parameter.ReferencedAssemblies.Add("System.XML.dll");
parameter.ReferencedAssemblies.Add("System.Web.Services.dll");
parameter.ReferencedAssemblies.Add("System.Data.dll");
Assert.AreEqual(false, result.Errors.HasErrors, "Getwebservices failed!");
Assembly asm = result.CompiledAssembly;
Type typeQualityEntry = asm.GetType("QualityEntry"); // 如果在前面为代理类添加了命名空间,此处需要将命名空间添加到类型前面。
object bjQualityEntry = Activator.CreateInstance(typeQualityEntry);
MethodInfo method = typeQualityEntry.GetMethod("HelloWorld");
Console.WriteLine(method.Invoke(objQualityEntry, null));
// 测试UserLogin
string userid = "PDA";
string password = "PDA";
object bjAuthInfo = Activator.CreateInstance(typeAuthInfo);
PropertyInfo propertyUserName = typeAuthInfo.GetProperty("UserName");
propertyUserName.SetValue(objAuthInfo, userid, null);
PropertyInfo propertyPassword = typeAuthInfo.GetProperty("Password");
propertyPassword.SetValue(objAuthInfo, password, null);
propertyAuthInfoValue.SetValue(objQualityEntry, objAuthInfo, null);
object result2 = false;
try
{
result2 = methodUserLogin.Invoke(objQualityEntry, null);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
PropertyInfo propertyIsSuccess = typeMessage.GetProperty("PropertyIsSuccess");
object bjValue = propertyIsSuccess.GetValue(result2, null);
Assert.AreEqual(true, objValue);
object bjAuthInfoValue= propertyAuthInfoValue.GetValue(objQualityEntry, null);
object bjOrgID = propertyOrgID.GetValue(objAuthInfoValue, null);
Assert.AreEqual(Convert.ToInt64(170), Convert.ToInt64(objOrgID), "OrgID is not right!");
try
{
PropertyInfo propLogInfo = typeMessage.GetProperty("PropertyLoginInfo");
object bjLogInfo = propLogInfo.GetValue(result2, null);
Type typeLogInfo = asm.GetType("LoginInfo");
PropertyInfo propInvobjects = typeLogInfo.GetProperty("InvObjects");
object[] bjInvObjects = (object[])propInvobjects.GetValue(objLogInfo, null);
PropertyInfo propOrgID = typeInvObject.GetProperty("OrganizationID");
object rgid = propOrgID.GetValue(objInvObjects[0], null);
Assert.AreEqual(Convert.ToInt64(170), orgid, "Orgid is not right!");
Console.WriteLine("End");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
.NET Compact Framework 不支持所有由生成的代码。但是,如果您在 Microsoft Visual Studio 2005 中将 Web 引用添加到智能设备项目中,则使用 Web 服务的应用程序可使用生成的代理。
某些情况下您可能需要使用 Wsdl.exe。有一种情况是您需要提供代理中粒子成员的顺序以符合 Web 服务所需要的顺序。Wsdl.exe 工具带有/order选项,该选项在粒子成员上生成显式顺序标识符。
此示例演示从生成的代理中移除哪些代码,以使其可由 .NET Compact Framework 使用。要移除的代码以出现在生成的代理中的顺序进行说明。
从生成的代理中移除不受支持的代码
-
使用生成代理。
-
移除定义名为
RetBaseTypesOperationCompleted、类型为的委托的代码。 -
移除定义和引用
RetBaseTypesCompleted事件、RetBaseTypesCompletedEventHandler委托和RetBaseTypesCompletedEventArgs类的代码。 -
移除定义和调用
RetBaseTypesAsync方法的代码。 -
移除定义和调用
OnRetBaseTypesOperationCompleted方法的代码。 -
移除定义和调用
CancelAsync方法的代码。 -
移除
Serializable属性。
请参见
下面的代码示例是一个 Web 服务,用于定义客户端必须传递的AuthenticationSOAP 头。该 Web 服务不需要进行身份验证。相反,它可以检查 User.Identity.IsAuthenticated 属性,确定 HTTP Module是否已对用户进行了身份验证。
<%@ WebService Language="C#" Class="SecureWebService" %>
using System;
using System.Web.Services;
using System.Web.Services.Protocols;
public class Authentication : SoapHeader {
public string User;
public string Password;
}
public class SecureWebService : WebService{
public Authentication authentication;
[WebMethod]
[SoapHeader("authentication")]
public string ValidUser(){
if (User.IsInRole("Customer"))
return "User is in role customer";
if (User.Identity.IsAuthenticated)
return "User is a valid user";
return "not authenticated";
}
}
下面的代码示例是一个 Web 服务客户端,用于为AuthenticationSOAP 头中的自定义 SOAP 头身份验证机制传递必要的凭据。
// Create a new instance of a Web service proxy class.
SecureWebService s = new SecureWebService();
// Create the Authentication SOAP header and set values.
Authentication a = new Authentication();
a.User = user.Value;
a.Password = password.Value;
// Assign the Header.
s.AuthenticationValue = a;
4. Pocket PC仿真器部署。
首先安装 Microsoft ActiveSync程序,版本在4.0以上。启动程序后,连接设置如下:
在VS2005 IDE的工具菜单下选择设备仿真器管理器,选中“Pocket PC 2003SE仿真器”,右键选择“连接”,然后再右键点击“插入底座”
在Pocket PC的仿真器,进入“设置”,选择“连接”选项卡,然后先选择“网卡”,网卡连接到“默认单位设置”,适配器类型选择“AsyncMac NDISWAN”,点击OK。
在选择“连接”,选择“高级”选项卡,点击“选择网络”,里面,程序自动连接到选择“单位设置”。点击两次OK退出。然后测试上网。