sticky
Helpful Tips when using WCF as the transport for a mobile sync service application RRS feed

  • General discussion

  • I have seen a few threads in the forum showing issues,questions with the WCF as the transport layer for a mobile sync service application. so I think it is worth to create a new thread and post a simple sample and tips for workaround some limitations on device regarding with supporting WCF in netcf.

     

    Tip1: need to force to use the [XmlSerializerFormat] for the contract interfaces, as shown in the Iservice.cs code below.

    Tip2: need to force to use the basicHttpBinding for your contracts, as shown in the web.config file.

    Tip3: need to modify the netcfSvcUtil.exe generated service.cs code to remove all the duplicated types that are already degined in the Microsoft.Synchronization.Data.dll. this most likely the reason if you encountered InvalidCast exception.

    Tip4: deal with Collection<string> and string[]. as you can see in the sample code below, I have used string [] to ensure it is consistent in the GetSchema(). this most likely the cause if you saw ArgumentException when calling this method.

    Tip5: debugging other issues: set the includeExceptionDetailInFaults="true" flag in the web.config could be helpful, and some logging logic at the server side could be helpful also as shown in the server side code.

    Tip6: netcfSvcUtil.exe, I just use the defualt option, to generate the client side code.

    Tip7: this is a good blog for WCF on devices: http://blogs.msdn.com/andrewarnottms/archive/2007/09/13/calling-wcf-services-from-netcf-3-5-using-compact-wcf-and-netcfsvcutil-exe.aspx

     

    You can also view these tips at: http://blogs.msdn.com/sync/archive/2008/07/14/using-wcf-for-communcation-in-a-mobile-sync-application.aspx

     

    The code sample can be found here: http://blogs.msdn.com/sync/attachment/8731954.ashx

     

    thanks

    Yunwen

     

    Sunday, July 13, 2008 4:17 PM
    Moderator

All replies

  • Hi Yunwen:
    I'm using wsHttpBinding fot the Sync Service and it is working fine, is there a special reason why you say we should use basicHttpBinding?
    Thanks!
    Thursday, May 28, 2009 4:54 PM
  • I tried using XmlSerializerFormat on the contract interface and got the exception "System.InvalidOperationException: There was an error reflecting 'GetChangesResult'. ---> System.InvalidOperationException: To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. Microsoft.Synchronization.ChangeBatch does not implement Add(System.Object)."

    Removing it allowed the WCF code to build.

    I am using the code shown below (which is from the Apress book Pro Sync Framework, page 287):

    Option Strict On
    Option Compare Text
    
    Imports System
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.ServiceModel
    Imports Microsoft.Synchronization
    Imports Microsoft.Synchronization.Data
    
    '<XmlSerializerFormat(), - causes an error as it can't reflect ChangeBatch as it doesn't contain an Add method!?!
    <ServiceContract(SessionMode:=SessionMode.Required), _
    ServiceKnownType(GetType(SyncIdFormatGroup)), _
    ServiceKnownType(GetType(DbSyncContext))> _
    Public Interface IREAIISyncServiceContract
    
    	<OperationContract(IsInitiating:=True, IsTerminating:=False)> _
    	Sub BeginSession()
    
    	<OperationContract(IsInitiating:=False, IsTerminating:=False)> _
    	Sub GetKnowledge(ByRef iBatchSize As UInteger, ByRef oSyncKnowledge As SyncKnowledge)
    
    	<OperationContract(IsInitiating:=False, IsTerminating:=False)> _
    	Function GetChanges(ByVal iBatchSize As UInteger, ByVal oDestinationKnowledge As SyncKnowledge, _
    	 ByRef oChangeData As Object) As ChangeBatch
    
    	<OperationContract(IsInitiating:=False, IsTerminating:=False)> _
    	Sub ApplyChanges(ByVal eResolutionPolicy As ConflictResolutionPolicy, ByVal oSourceChanges As ChangeBatch, _
    	 ByVal oChangeData As Object, ByRef oSessionStatistics As SyncSessionStatistics)
    
    	<OperationContract(IsInitiating:=False, IsTerminating:=True)> _
    	Sub EndSession()
    
    End Interface
    Friday, July 10, 2009 10:10 AM
  • I rebuilt the class from scratch and the problem went away! I've no idea why...
    Wednesday, August 12, 2009 9:40 AM
  • I have added service reference of WCF based sync service (I created that based on your sample) to my desktop application that I am using to create SDF using Sync Services. However I am getting Argument exception similar to what you explained in Tip4. I have changed the IService to following

    [OperationContract]
            SyncSchema GetSchema(Collection<string> tableNames, SyncSession syncSession);

    So that I have collection instead of array of tableNames. I also changed it in reference.cs file. But I am still getting the same exception.

    Any help is highly appreciated.

    Thanks

    Apurva
    Tuesday, September 15, 2009 2:39 PM
  • This is exactly how we did our WCF services for syncing and the example is long, but works. 

    We have other issues regarding syncing, but i believe they are unrelated to writing the WCF service this way.
    Travich
    Friday, March 5, 2010 7:43 PM
  • Thanks for information.

    I was able to make it work as you summarized.

    However, I don't know how WCF service library is going to be up and running in backend server for mobile device use.

    All information I found is about running from VS for testing, but no information for production deployment.

    I spent a lot of time to find the way, but no luck.

    Could you give me any light on this?

    Thanks!

    Monday, April 12, 2010 12:39 PM
  • Hi Henry,

     

    did you try deploying your WCF service on server and let your client communicate thru the service?, i did the same, i have deployed WCF service on server and Client code at Client machine ( i am using Tablet pc as client machine), but service is not able to connect to local tablet pc database.

    My Assumption on Sync is:

    1) client and server should exchange the changed data thru the WCF services

    2) client should write changes to client database and server should write to server ( i think server is communicating with both the databases which may be wrong)

    my deployment model is giving error login filed for 'databaseuser'

     

     

    Monday, July 12, 2010 6:17 PM
  • Here is complete example of N-tier Sync solution over WCF.

    http://www.rajneeshnoonia.com/blog/2012/03/n-tier-sync-framework/

    Regards

    Rajnish Noonia

    Friday, March 30, 2012 10:17 PM