Saturday, September 8, 2012

Creating an Occasionally Connected Smart Device Application a working example with emulator

  In this blog I plan on going thru how to create an occasionally connected smart device application and get it working correctly. I found this example Smart Device Example link to explain how to create and connect to the emulator. The problem is this is not a complete and working example, so after spending several days just trying to get this to work I will try to go back thru what I did. I will start with this example with my added changes and present a working version of this example.
  The first issue I ran into when trying to create this project is which Visual Studio version should I use. After searching thru the web from the best that I can see I the newest version that still supports winCE is VS2008, so I started with this version. The first step is to make sure you install all the prerequisites as listed below:

Prerequisites


To complete this walk through you must:
   One mistake I made was to not install all of the preequisites, since I noticed I already had MSQL Server Comact 3.5 installed. You must make sure you install the Service pack 1 for this or you will get connection errors when you try to do your remote connection with the compact framework. Mine would blow up on the client side of code listed below:

Microsoft.Synchronization.Data.

  This would give the following cryptic error unhandled Message="TargetInvocationException" and I had to seach lots of articles to finally figure this out. Once I added service pack 1 that error went away. 
  After fixing that I then got a client error in Microsoft.Synchronization.Data.SyncStatistics syncStats = syncAgent.Synchronize(); for the following ArgumentException error on the client side.
   The first thing to do is see if IE can connect to the WCF service to ensure it is still working. To do this I set theMiddleTierServiceLibrary as the startup project and then select the start without debugging option in the Debug menu. This will bring up the deployment window where I will just select cancel and then in the deployment errors click continue. Figure 1 shows the WCF screen and from here I just copy the http://10.83.4.96:8080/NorthwindCacheSyncService/ from the WC F test client and paste it in IE where you should see the page as listed in figure 2. From this I now know that the WCF service is working and a client can connect to it.
 





Figure 1


Figure 2

    Once I know a client can connect I then set SmartDeviceSyncClient as the start up project. Then I just select start debugging from the Debug menu option. Once the code runs I will get the ArgumentException error  so I ignore the error and let the code run. My next step is to try to connect thru IE on the PocketPC emulator with the saem address as listed above. As Shown below you can see that connection files and I cannot connect to the WCF service from the PocketPC.  




Figure 3

    No back to reading more articles on this problem and I come up with this. How is the PocketPC able to establish a connection to the service. Some how it has to have a enet connection and know how to resolve it to an IP address. So the way to do this is in VS IDE Tools->Device Emulator Manager. Once this is selected it bring up a panel with all the emulation devices. On this screen I selected window Mobile 5.0 and right click on it. The right click option and select connect.



Figure 4

Once the device is connected you should see the phone and active sync screens pop up and establish a connect. Now the PocketPC has a ENET connect thru active sync and it should be able to connect to WCF service. But in my case using  http://10.83.4.96:8080/NorthwindCacheSyncService/ still did not connect with the same connection error. So the next thing I tried was to use ppp_peer:8080 as the connection name as this is the default name for the emulated device and behold it was able to connect. Great now I know I can connect on ppp_peer with IE, but what about the client application.




Figure 5




   After running the Smart Device Client code I was still getting a argument error and from this I see that it cannot connect to the WCF service thru IP address directly. So for a test I inserted strHostName = Convert.ToString(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0]); into the Sync() function just to see what the address was that it returned. The address returned was 192.168.55.101 and this was what I was expecting. Then I just let the code run and magic it now started to work.
string


Figure 6



  So from this I am not sure what caused it to start working, but I tested it several times and it seams to run and connect correctly now. I would assume that forcing a DNS look up made the look up table instantiate the connection. I will test this further in another article, but for not it works.


ServerSyncProviderProxy syncProxy =new Microsoft.Synchronization.Data.ServerSyncProviderProxy(svcProxy);