智能设备WebService程序开发和部署

 
1.PDA调用WebService的WebMethod操作数据库。
2.WebService的地址可以配置,PDA通过在WinCE上产生WebService代理类来动态调用WebService。
3.WebService通过SoapHeader来传递自定义的身份认证信息。
4.Pocket PC模拟器部署程序方法。
 
1.PDA调用WebService的WebMethod操作数据库。
 
2.动态调用WebService的方法。
方法一:通过 WebClient类和反射调用WebService。该方法在.Net Compact Framework下不适用。

            #region 取WebService
            // 1. 使用 WebClient 下载 WSDL 信息。
            WebClient web = new WebClient();
            Stream stream = web.OpenRead("
http://192.168.183.162/emes/WebServiceProxy/QualityEntry.asmx?WSDL");
 
            // 2. 创建和格式化 WSDL 文档。
            ServiceDescription description = ServiceDescription.Read(stream);
 
            // 3. 创建客户端代理代理类。
            ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
 
            importer.ProtocolName = "Soap"; // 指定访问协议。
            importer.Style. = ServiceDescriptionImportStyle.Client; // 生成客户端代理。
            importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
 
            importer.AddServiceDescription(description, null, null); // 添加 WSDL 文档。
 
            // 4. 使用 CodeDom 编译客户端代理类。
            CodeNamespace nmspace = new CodeNamespace(); // 为代理类添加命名空间,缺省为全局空间。
            CodeCompileUnit unit = new CodeCompileUnit();
            unit.Namespaces.Add(nmspace);
 
            ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
 
            CompilerParameters parameter = new CompilerParameters();
            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");
 
            CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);
            Assert.AreEqual(false, result.Errors.HasErrors, "Getwebservices failed!");
 
            Assembly asm = result.CompiledAssembly;
 
            #endregion
           
            Type typeQualityEntry = asm.GetType("QualityEntry"); // 如果在前面为代理类添加了命名空间,此处需要将命名空间添加到类型前面。
                       
 
            // 测试HelloWorld
            object bjQualityEntry = Activator.CreateInstance(typeQualityEntry);
            MethodInfo method = typeQualityEntry.GetMethod("HelloWorld");
            Console.WriteLine(method.Invoke(objQualityEntry, null));
           
            // 测试UserLogin
            string userid = "PDA";
            string password = "PDA";
 
            Type typeAuthInfo = asm.GetType("AuthInfo");
            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);
 
            PropertyInfo propertyAuthInfoValue = typeQualityEntry.GetProperty("AuthInfoValue");
            propertyAuthInfoValue.SetValue(objQualityEntry, objAuthInfo, null);
 
            MethodInfo methodUserLogin = typeQualityEntry.GetMethod("UserLogin");
           
            object result2 = false;
            try
            {
                result2 = methodUserLogin.Invoke(objQualityEntry, null);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
 
            Type typeMessage = asm.GetType("Messages");
            PropertyInfo propertyIsSuccess = typeMessage.GetProperty("PropertyIsSuccess");
            object bjValue = propertyIsSuccess.GetValue(result2, null);
            Assert.AreEqual(true, objValue);
 
            propertyAuthInfoValue = typeQualityEntry.GetProperty("AuthInfoValue");
            object bjAuthInfoValue= propertyAuthInfoValue.GetValue(objQualityEntry, null);
 
            PropertyInfo propertyOrgID = typeAuthInfo.GetProperty("OrgID");
            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);
 
                Type typeInvObject = asm.GetType("InvObject");
                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);
            }

方法二:通过WSDL产生WebService代理类。
wsdl.exe 使用方法参见MSDN帮助。
wsdl.exe默认产生的类在.Net Compact Framework下也不适用,但可以对产生的类进行修改,去掉.Net Compact Framework不存在的类即可通过编译。

.NET Compact Framework 不支持所有由 Web 服务描述语言工具 (Wsdl.exe) 生成的代码。但是,如果您在 Microsoft Visual Studio 2005 中将 Web 引用添加到智能设备项目中,则使用 Web 服务的应用程序可使用生成的代理。

某些情况下您可能需要使用 Wsdl.exe。有一种情况是您需要提供代理中粒子成员的顺序以符合 Web 服务所需要的顺序。Wsdl.exe 工具带有/order选项,该选项在粒子成员上生成显式顺序标识符。

此示例演示从生成的代理中移除哪些代码,以使其可由 .NET Compact Framework 使用。要移除的代码以出现在生成的代理中的顺序进行说明。

从生成的代理中移除不受支持的代码

  1. 使用 Web 服务描述语言工具 (Wsdl.exe) 生成代理。

  2. 移除定义名为RetBaseTypesOperationCompleted、类型为 SendOrPostCallback 的委托的代码。

  3. 移除定义和引用RetBaseTypesCompleted事件、RetBaseTypesCompletedEventHandler委托和RetBaseTypesCompletedEventArgs类的代码。

  4. 移除定义和调用RetBaseTypesAsync方法的代码。

  5. 移除定义和调用OnRetBaseTypesOperationCompleted方法的代码。

  6. 移除定义和调用CancelAsync方法的代码。

  7. 移除Serializable属性。

请参见

概念
.NET Compact Framework 中的网络编程
.NET Compact Framework 帮助主题


3.通过SOAP来传递自定义的身份认证信息。

下面的代码示例是一个 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退出。然后测试上网。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值