1. PDA call
WebService. 2. the WebService address can be configured. PDA dynamically calls WebService by generating a WebService proxy class on wince. 3. WebService uses soapheader to pass custom identity authentication information. 4. How to deploy a program in the Pocket PC simulator. 1. PDA calls webmethod of WebService to operate the database. For more information, see
Http://www.codeproject.com/KB/webservices/PDAWorkflowclient.aspx
2. dynamically call WebService methods. Method 1: Call WebService through WebClient class and reflection. This method is not applicable in. NET Compact framework.
# Obtain WebService from Region
// 1. Use WebClient to download the WSDL information.
WebClient web = new WebClient ();
Stream stream = web. openread ("
Http: // 192.168.183.162/EMEs/webserviceproxy/qualityentry. asmx? WSDL"); // 2. Create and format the WSDL document.
Servicedescription description = servicedescription. Read (Stream); // 3. Create a client proxy class.
Servicedescriptionimporter importer = new servicedescriptionimporter (); importer. protocolname = "Soap"; // specify the access protocol.
Importer. style. = servicedescriptionimportstyle. Client; // generate a client proxy.
Importer. codegenerationoptions = codegenerationoptions. generateproperties | codegenerationoptions. generatenewasync; importer. addservicedescription (description, null, null); // Add a WSDL document. // 4. Use codedom to compile the client proxy class.
Codenamespace nmspace = new codenamespace (); // Add a namespace for the proxy class. The default value is global space.
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"); // if you have added a namespace for the proxy class before, you need to add the namespace before the type.
// Test helloworld
Object bjqualityentry = activator. createinstance (typequalityentry );
Methodinfo method = typequalityentry. getmethod ("helloworld ");
Console. writeline (method. Invoke (objqualityentry, null ));
// Test 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! "); // Retrieves an inventory Organization
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 );
}
Method 2: generate a WebService proxy class through WSDL. For how to use wsdl.exe, see the msdn help. The default classes generated by wsdl.exe are not applicable to the. NET Compact framework. However, you can modify the generated classes and compile the classes that do not exist in the. NET Compact framework.
. NET Compact framework does not support all Generated code. However, if you add a web reference to a smart device project in Microsoft Visual Studio 2005, Web Service applications can use the generated proxy.
In some cases, you may need to use wsdl.exe. One case is that you need to provide the order of the particle members in the proxy in order to meet the order required by the web service. Wsdl.exe tool/OrderOption, which generates an explicit sequence identifier on the particle member.
This example shows which code is removed from the generated proxy so that it can be used by. NET Compact framework. The code to be removed is described in the order in which it appears in the generated proxy.
Remove unsupported code from the generated proxy
Use Generate a proxy.
Removed definition nameRetBaseTypesOperationCompleted
, Type is .
Remove definitions and referencesRetBaseTypesCompleted
Events,RetBaseTypesCompletedEventHandler
Delegate andRetBaseTypesCompletedEventArgs
Class code.
Remove definition and callRetBaseTypesAsync
Method code.
Remove definition and callOnRetBaseTypesOperationCompleted
Method code.
Remove definition and callCancelAsync
Method code.
RemoveSerializable
Attribute.
See concepts
Network Programming in. NET Compact framework
. NET Compact framework help topic
3. Use soap to transmit custom identity authentication information.
The following code example is a Web service that defines what the client must passAuthentication
SOAP header. This Web service does not require authentication. Instead, it can check the user. Identity. isauthenticated attribute to determine whether the HTTP module has authenticated the user.
<% @ 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;
[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 ";
}
}
The following code example is a Web service client usedAuthentication
The custom SOAP header authentication mechanism in the SOAP header transmits necessary creden.
// 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 =;
4. Deploy the Pocket PC simulator.
First Install the Microsoft ActiveSync program. The version is later than 4.0. After the program is started, the connection settings are as follows:
Under the tool menu of vs2005 IDE, select the device simulator manager, select "Pocket PC 2003se Simulator", right-click and choose "Connect", and then right-click "insert base"
In the Pocket PC simulator, go to "Settings", select the "connection" tab, select "Nic", connect the NIC to "Default unit Settings", and select "asyncmac ndiswan" for the adapter type ", click OK.
Select "connection", select the "advanced" tab, and click "select Network". The program automatically connects to and selects "unit Settings ". Click OK twice to exit. Then test the Internet access.