【问题标题】:Microsoft Sync Framework Newbie - Argument Exception was unhandledMicrosoft 同步框架新手 - 参数异常未处理
【发布时间】:2012-03-23 20:04:22
【问题描述】:

我对 Windows 移动开发和 MSFT 同步框架非常陌生。我有一个简单的应用程序,它可以读取条形码,从数据库中检索条目并让用户输入新的读数。这一切都很好。但是,当我尝试同步时,出现以下异常:

System.ArgumentException was unhandled
  Message="ArgumentException"
  StackTrace:
       at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean verifyAccess, StackCrawlMark& stackMark)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
       at Microsoft.Synchronization.Data.ServerSyncProviderProxy.ApplyChanges(SyncGroupMetadata groupMetadata, DataSet dataSet, SyncSession syncSession)
       at Microsoft.Synchronization.SyncAgent.UploadChanges(SyncGroupMetadata groupMetadata)
       at Microsoft.Synchronization.SyncAgent.Synchronize()
       at ElectricBarcodeApp.Form1.buttonSync_Click(Object sender, EventArgs e)
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
       at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
       at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
       at System.Windows.Forms.Application.Run(Form fm)
       at ElectricBarcodeApp.Program.Main()

这是我的代码:

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.IO;
using System.Reflection;

namespace ElectricBarcodeApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            using (SqlCeConnection conn = new SqlCeConnection(
            ("Data Source=" + (Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), "ElectricReading.sdf") + ";Max Database Size=2047"))))
            {

                // Connect to the local database 
                conn.Open();
                using (SqlCeCommand cmd = conn.CreateCommand())
                {
                    SqlCeParameter param = new SqlCeParameter();
                    param.ParameterName = "@Barcode";
                    param.DbType = DbType.String;
                    param.Value = textBarcode.Text.Trim();

                    // SELECT rows
                    cmd.CommandText = "SELECT Location, Reading FROM Main2 WHERE Barcode LIKE @Barcode";
                    cmd.Parameters.Add(param);

                    DataTable data = new DataTable();

                    using (SqlCeDataReader reader = cmd.ExecuteReader())
                    {

                            data.Load(reader);
                            this.dataGrid1.DataSource = data;

                    }

                }
            }


        }

        private void buttonEnter_Click(object sender, EventArgs e)
        {
            using (SqlCeConnection conn = new SqlCeConnection(
            ("Data Source=" + (Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), "ElectricReading.sdf") + ";Max Database Size=2047"))))
            {

                // Connect to the local database 
                conn.Open();
                using (SqlCeCommand cmd = conn.CreateCommand())
                {

                    // INSERT NEW READING
                    cmd.CommandText = "UPDATE Main2 SET Reading = '" + textNewReading.Text.Trim() + "' WHERE Barcode LIKE '" + textBarcode.Text.Trim() + "'"; 

                    int m = cmd.ExecuteNonQuery();
                    cmd.ExecuteNonQuery();
                    if (m > 0)
                    {
                        MessageBox.Show("New Reading Successfully Entered:" + textNewReading.Text.Trim(), "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
                    }

                }
            }

        }

        private void buttonSync_Click(object sender, EventArgs e)
        {
            // The WCF Service
            ElectricReadingCacheWebRef.ElectricReadingCacheSyncService webSvcProxy = new
                ElectricBarcodeApp.ElectricReadingCacheWebRef.ElectricReadingCacheSyncService();

            // The Remote Server Provider Proxy
            Microsoft.Synchronization.Data.ServerSyncProviderProxy serverProvider = new
                Microsoft.Synchronization.Data.ServerSyncProviderProxy(webSvcProxy);

            // The Sync Agent
            ElectricReadingCacheSyncAgent syncAgent = new ElectricReadingCacheSyncAgent();
            syncAgent.RemoteProvider = serverProvider;
            //Main2 is the table to be synchronized
            syncAgent.Main2.SyncDirection = Microsoft.Synchronization.Data.SyncDirection.Bidirectional;

            // Synchronize the databases
            Microsoft.Synchronization.Data.SyncStatistics stats = syncAgent.Synchronize();

            // Show synchronization statistics
            MessageBox.Show("Changes Downloaded: " + stats.TotalChangesDownloaded.ToString() +
                "\r\n" + "Changes Uploaded: " + stats.TotalChangesUploaded.ToString());

        }



    }
}

在这一行抛出异常:

// Synchronize the databases
                Microsoft.Synchronization.Data.SyncStatistics stats = syncAgent.Synchronize();

我在寻找解决方案方面一无所获,非常感谢任何帮助!

编辑:我想可能是因为我没有:

using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;

但添加并没有改变异常。

【问题讨论】:

  • 如果您没有 using 语句,代码将无法编译。
  • 您还应该发布 ElectricReadingCacheSyncAgent 代码,因为它是引发异常的地方。
  • 这似乎是与WCF有关的错误,您是如何生成客户端代理的?

标签: c# .net sql visual-studio microsoft-sync-framework


【解决方案1】:

双击您的网络引用以在对象浏览器中打开它。

展开节点YourAppName.YourAppNameCacheWebRef。

右键单击 YourNameCacheSyncService,然后单击转到定义。

Reference.cs 在代码编辑器中打开。

在最后一个 using 或 imports 语句之后添加以下代码:

Imports Microsoft.Synchronization
Imports Microsoft.Synchronization.Data

或者,在 C# 中:

using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;

删除文件中除 YourNameCacheSyncService 之外的所有类和枚举。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    相关资源
    最近更新 更多