none
WCF 4.0 & Sync Framework 2.0 & Entity Framework 4.0 & ASP.NET MVC 2.0 & auto generated code RRS feed

  • Question

  • Hello,

    I have to use Sync Framework 2.0 in our WPF app that will contain SQL Compact 3.5 and will synchronize with SQL Server 2008.

    I followed this video : http://download.microsoft.com/download/6/7/3/6730f0e7-a649-4656-96ab-150c7501a583/IntroToSyncServicesADODetNet_HighQuality.wmv. 

    But instead DataSet I've used EntityFramework 4.0 and C#.

    I'm very interested in code auto generation by adding Local Database Cache file with extension sync. It is great, and I can modify code in my partial class to change base functionality.

    Everything works grate when I have code for client and server place in WPF application.

    Everything works grate when I use WCF Class Library that contains server synchronization logic.


    But... In the following example they show us how to run solution and host WCF in local "WCF Host" only on my computer.

    The first question is:

    "How I can create instance of class from WCF Class Library that contains all server synchronization logic and then host it and expose in ASP.NET MVC 2.0 application. "

    The most important thing is to keep this *.sync files and don't write all the code manually it gives me the option to automatically update this code when database schema would change.

    The second question is:

    "How I can configure endpoints and behaviors for this instance of WCF Class Library in my web.config when it has its on app.config in class library?..."

    Unfortunately wizard for *.sync files only sees local WPF application, or WCF Class Library, I can't choose directly ASP.NET MVC 2.0 (it would be great) to generate class for synchronization in web app.

    I would be very pleased to see working example.

    Regards,
    Daniel Skowroński

    Tuesday, May 18, 2010 5:14 PM

Answers

  •  

     

    Hello everyone,

     

    Solution to create WCF Class Library instance with synchronization logic hosted in ASP.NET MVC 2.0:

     

    - follow http://download.microsoft.com/download/6/7/3/6730f0e7-a649-4656-96ab-150c7501a583/IntroToSyncServicesADODetNet_HighQuality.wmv to create WCF Class Library

    - create ASP.NET MVC 2.0 App and add WCF Service

    - delete C# file *.cs  behind *.svc

    - add Project Referece from ASP.NET projet to WCF Class Library

    - edit your *.svc file in ASP.NET

     

    here you will see something like:

     

    <%@ ServiceHost Language="C#" Debug="true" Service="namespace-assembly.class" CodeBehind="filename.svc.cs" %>

     

    where Service is Factory method that will create instance of "namespace-assembly.class" so, you have to change this to "wcf_librrary_namespace-assembly.****DataCacheSyncService" and CodeBehind to "wcf_librrary_namespace-assembly.filename.cs"

    - next modify wcf instance in WCF Class Library that will enable hosting it with the same credentials as asp.net app, simply add :  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] attribute

    - next configure web.config:

     

     

    <service name="asp.net-namespace.wcf_service_name" behaviorConfiguration="service_nameBehavior">
      <host>
        <baseAddresses>
         <add baseAddress="http://ipaddres/asp.net-app-name/service-name.svc" />
       </baseAddresses>
      </host>
    
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <endpoint address=""  binding="basicHttpBinding" contract="wcf_librrary_namespace-assembly.I****DataCacheSyncContract" />
     </service>
    
    

     

     

    <behavior name="service_nameBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>

     

    add also <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

    below <system.serviceModel>

     

    - now simply publish it to your server and create ASP.NET app

    - now add Service Reference to your Client Application

    - here we have problem that when you will execute:

     

    *****DataCacheSyncAgent syncAgent = new *****DataCacheSyncAgent(new ******DataCacheSyncContractClient());

    Microsoft.Synchronization.Data.SyncStatistics syncStats = syncAgent.Synchronize();

     

    You will probably get Exception: “Cannot convert type ** to Microsoft.Synchronization.Data.SyncGroupMetadata, One solution to resolve this issue for now that I’ve found is to expand your service reference and b CTR+H rename all “asp-net-assembly-SyncGroupMetadata” and other similar files to “Microsoft.Synchronization.Data.SyncGroupMetadata” etc.

    - Now synchronization should start

     

    As you have said Dong :

    "If you are willing to read a little more about WCF, you will find that it is pretty straightforward to deploy a WCF service on another machine with the existing same machine WCF service code"...

     

    HTH

     

    Regards,

    Daniel Skowroński

     

     

    Monday, May 24, 2010 11:14 AM

All replies

  • Hi,

    This is a question for VS.net designer. Currently, I don't think there is a support for directly creating Sync web app with ASP.NET MVC 2.0. Considering that Sync Service is just a set of managed APIs and their parameters can be serialzed, you can pick your own data transfer protocol. If you know how to expose a management APIs from ASP.NET MVC 2.0, you should be able to build an web app with Sync Service. The questions is that both client and service sides need Sync service be installed since sync providers need to be run on both sides. As a web app, will you be able to force client side to install sync service?

    Thanks,
    Dong


    This posting is provided AS IS with no warranties, and confers no rights.
    Tuesday, May 18, 2010 6:56 PM
    Moderator
  •  

    Thanks for reply Dong,

    I'm a little bit confused and I can't find any tutorials "how to" expose wcf service with "Local Data Cache" wizard on the server, something more advanced than you can see on the provided BASIC video.

    I thought that the question concerning part of the sync framework (I mean Local Data Cache) should be posted in sync framework forum.

    "The questions is that both client and service sides need Sync service be installed since sync providers need to be run on both sides. As a web app, will you be able to force client side to install sync service?"

    As far as I know I can do it as a reason that my WPF client app will be deployed by ClickOnce and you can read here http://www.microsoft.com/downloads/details.aspx?FamilyID=109db36e-cdd0-4514-9fb5-b77d9cea37f6&displaylang=en this statement:

     

    "Bootstrapper Package

    This download also provides Sync Framework 2.0 bootstrapper package samples that can be used with Visual Studio Setup & Deployment projects and ClickOnce applications to redistribute Sync Framework 2.0 components. To use the bootstrapper package, download it to the bootstrapper folder specified by Visual Studio. For Visual Studio 2008, the default location is C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages. After the bootstrapper package has been downloaded to this folder, the Sync Framework 2.0 redistributables appear in the Visual Studio Prerequisites dialog box, where they can be selected to be redistributed along with your application."

     

    My big problem is still "How I can create instance of class from WCF Class Library that contains all server synchronization logic and then host it and expose in ASP.NET MVC 2.0 application. "

    If you can't provide me the answer I would be pleased if you could show me the person/place to seek.

    Regards,

    Daniel Skowroński

     

    Tuesday, May 18, 2010 7:49 PM
  • Hi,

    I assume that your problem is to create a n-tier sync solution with WCF. If it is correct, please reference the MSDN sample: http://msdn.microsoft.com/en-us/library/cc488004.aspx for how to configure and start a WCF sync service.

    Even for the video that you followed, if it has told you how to configure a WCF service on the local host, you can configure the same WCF service on a different machine. Just remember to allow the WCF listening port in your firewall setting.

    Thanks,
    Dong


    This posting is provided AS IS with no warranties, and confers no rights.
    Tuesday, May 18, 2010 8:12 PM
    Moderator
  • Hello Daniel,

     I was interested to hear that you are using Entity Framework objects instead of DataSets and ADO.NET commands in the sync framework. I tried to do this, but gave up since the Sync framework providers such as SqlProviders are very focused on using DataSets. 

    Can you provide some pointers on how you are integrating with EF? Are you storing the various commands in Adapters to provide to the Sync framework?

    Thanks,

     

     

     

     

     

    Wednesday, May 19, 2010 4:53 AM
  • Hi Dong,

     

    I know that I can build n-tier app because I did it by following mentioned video.

    It provides even more advanced scenario than your last sample: http://msdn.microsoft.com/en-us/library/cc488004.aspx

     

    And I also think that

    "Even for the video that you followed, if it has told you how to configure a WCF service on the local host, you can configure the same WCF service on a different machine. Just remember to allow the WCF listening port in your firewall setting." should be easy but it is not...

    "you can configure the same WCF service on a different machine" - if it is so simple then why I can't see any example? 

     

    In my first post I've asked exacly for this:

    "How I can create instance of class from WCF Class Library that contains all server synchronization logic and then host it and expose in ASP.NET MVC 2.0 application. "

    "How I can configure endpoints and behaviors for this instance of WCF Class Library in my web.config when it has its on app.config in class library?..."

     

    If it is so simple then why i see comments without answers in your example: http://msdn.microsoft.com/en-us/library/cc488004.aspx

    host this service inside IIS Edit
    I have question on how to host this service inside IIS. http://social.microsoft.com/Forums/en-US/uklaunch2007ado.net/thread/9bf0b2ce-7dba-46ab-bf31-b383caf4a7dc
    Any guidness will be helpfull.
    Thanks in advance.

     

     

    publish WCF service .. Edit

    sorry for my langage i'm french :) 
    By following this step by step procedure (do not forget proxy!) Everything works as it should: 
    run the application, the WCF service is running well, thanks to the service host (small icon shown us) and sync works ... well 
    But it is when you are debugging via VS mode and an emulator! 

    When we publish OCSDeviceApp project on a PDA device, the executable that launches on-line (via ActiveSync) but not with VS (no debugging eh ..): when is what the WCF service runs there ? How can do i know whether it was launched? ==> Sync does not work: WebService error ... I suppose due to the service is not started! 
    How? 
    Is there published the WCF service (MiddleTierServiceLibrary project)? And how? Under IIS? I tried and it does not work better ... 
    I do not understand and I need help! For several months (yes!) Kills me that I look at all the forums on French and English: nothing ! 

    Thank you for your help 

    Best regards

     

     

     

    Sorry buy my deadline is coming and I need to know if i can achieve this...

     

    Regards,

    Daniel

    Wednesday, May 19, 2010 11:43 AM
  • @aryacjose

    Hi it is straightforward when you have sync framework 2.0, VS 2010, EF 4.0.

    I believe that we can use EF only yo bind UI to data, but synchronization (under the hood) still use DataSets, and when your local store is synchronized you just refresh your EF model so I did.

    Just follow video in my post and thery will be the part to choose data set from wizard, right now we have additionally EF available.

     

    Regards,

    Daniel Skowroński 

    Wednesday, May 19, 2010 12:04 PM
  • Hi Daniel,

    Assuming you followed the steps here: http://msdn.microsoft.com/en-us/library/cc488004.aspx , you would have a WCF Service library project named MiddleTierServiceLibrary.

    To get it hosted in IIS, here's a quick workaround:

    1. Right click on the solution, select Add New Web Site, select WCF Service, the under Location, choose HTTP and specify where you want the project created

    2. Right cllck on the newly created web site project, select Add Reference, then select the DLL from the MiddleTierServiceLibrary (under Bin\Debug)

    3. Delete the files under App_Code folder of the web site project

    4. Open Service.svc and replace the service, similar to this (assuming you followed the sample above) 

    <%

    @ ServiceHost Language="C#" Debug="true" Service="MiddleTierServiceLibrary.AWCacheSyncService" %>

    5. Update the service entries in the config files either by manually changing the existing service name in the web.config and the binding or copying and pasting the entries from the MiddleTierServiceLibrary app.config file to the web site web.config file. Also, make sure to copy the connection string as well

    6. To test, right click Service.svc and choose View in browser

    7. Assuming you didnt get any error, make sure you point the client to the url of your web site project (quickest ways is to delete the web reference on the client and Add Web Reference, this time pointing to your web site project.

    hth

     

    Wednesday, May 19, 2010 12:26 PM
    Moderator
  • Hi Daniel,

    I hope that the detailed steps in JuneT's reply will be helpful for you. Regarding that why we don't provide a really cross machine n-tier sample, the reason is that the Database providers can sync in n-tier deployment with different protocols. WCF is just one of them. Since the sync framework doesn't provide any WCF related APIs, we didn't see the reason to go too deep into WCF part. Also, we feel it is much easier for customers to try our sample if it can be run on one machine without many setup steps. If you are willing to read a little more about WCF, you will find that it is pretty straightforward to deploy a WCF service on another machine with the existing same machine WCF service code.  The test team in Sync Framework wrote a good number of n-tier tests with WCF, and we deployed exact same WCF service on same or different machines with either IIS hosting or stand-alone hosting. 

    Thanks,

    Dong 

     


    This posting is provided AS IS with no warranties, and confers no rights.
    Wednesday, May 19, 2010 5:07 PM
    Moderator
  • I'm close to create WCF Class Library instance with synchronization logic hosted in ASP.NET MVC 2.0, so stay tuned.

    I will post what I've figured out.

     

    Regards,

    Daniel Skowroński

    Thursday, May 20, 2010 9:38 PM
  •  

     

    Hello everyone,

     

    Solution to create WCF Class Library instance with synchronization logic hosted in ASP.NET MVC 2.0:

     

    - follow http://download.microsoft.com/download/6/7/3/6730f0e7-a649-4656-96ab-150c7501a583/IntroToSyncServicesADODetNet_HighQuality.wmv to create WCF Class Library

    - create ASP.NET MVC 2.0 App and add WCF Service

    - delete C# file *.cs  behind *.svc

    - add Project Referece from ASP.NET projet to WCF Class Library

    - edit your *.svc file in ASP.NET

     

    here you will see something like:

     

    <%@ ServiceHost Language="C#" Debug="true" Service="namespace-assembly.class" CodeBehind="filename.svc.cs" %>

     

    where Service is Factory method that will create instance of "namespace-assembly.class" so, you have to change this to "wcf_librrary_namespace-assembly.****DataCacheSyncService" and CodeBehind to "wcf_librrary_namespace-assembly.filename.cs"

    - next modify wcf instance in WCF Class Library that will enable hosting it with the same credentials as asp.net app, simply add :  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] attribute

    - next configure web.config:

     

     

    <service name="asp.net-namespace.wcf_service_name" behaviorConfiguration="service_nameBehavior">
      <host>
        <baseAddresses>
         <add baseAddress="http://ipaddres/asp.net-app-name/service-name.svc" />
       </baseAddresses>
      </host>
    
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <endpoint address=""  binding="basicHttpBinding" contract="wcf_librrary_namespace-assembly.I****DataCacheSyncContract" />
     </service>
    
    

     

     

    <behavior name="service_nameBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>

     

    add also <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

    below <system.serviceModel>

     

    - now simply publish it to your server and create ASP.NET app

    - now add Service Reference to your Client Application

    - here we have problem that when you will execute:

     

    *****DataCacheSyncAgent syncAgent = new *****DataCacheSyncAgent(new ******DataCacheSyncContractClient());

    Microsoft.Synchronization.Data.SyncStatistics syncStats = syncAgent.Synchronize();

     

    You will probably get Exception: “Cannot convert type ** to Microsoft.Synchronization.Data.SyncGroupMetadata, One solution to resolve this issue for now that I’ve found is to expand your service reference and b CTR+H rename all “asp-net-assembly-SyncGroupMetadata” and other similar files to “Microsoft.Synchronization.Data.SyncGroupMetadata” etc.

    - Now synchronization should start

     

    As you have said Dong :

    "If you are willing to read a little more about WCF, you will find that it is pretty straightforward to deploy a WCF service on another machine with the existing same machine WCF service code"...

     

    HTH

     

    Regards,

    Daniel Skowroński

     

     

    Monday, May 24, 2010 11:14 AM