sharepoint SPFarm.local.GetObject返回null 我安装了SharePoint基金会2010(在“独立”类型)上的Windows 2012 R2../P>

sharepoint SPFarm.local.GetObject返回null 我安装了SharePoint基金会2010(在“独立”类型)上的Windows 2012 R2../P>,sharepoint,Sharepoint,我创建了一个windows应用程序项目。 加载时,新计数器(场)成功 但是,SPFarm.local.GetObject返回null 有人知道原因吗,请帮帮我 资料来源如下: //-------source start------- //Counter.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint;

我创建了一个windows应用程序项目。 加载时,新计数器(场)成功

但是,SPFarm.local.GetObject返回null

有人知道原因吗,请帮帮我

资料来源如下:

//-------source start-------

//Counter.cs
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

namespace WinAppTest
{  

    class Counter : SPPersistedObject
    {        
         public static String SettingName = "Counter";

         public static String SettingGuid = "62648e50-8aee-42b2-b074-2f49ced85587";

         [Persisted]

         public String name;

         [Persisted]   

         public String count;   

         public Counter() 

         {

         }    

         public Counter(SPPersistedObject parent)

         : base(SettingName, parent, new Guid(SettingGuid))

         {

         }

    }

}
//表格1.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using Microsoft.SharePoint.Administration;

namespace WinAppTest
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Write_Click(object sender, EventArgs e)
        {     
            SPFarm farm = SPFarm.Local;
            //------- *conf is null*-----
            Counter conf = (Counter)farm.GetObject(new Guid(Counter.SettingGuid));
            //------- *conf is null*-----
            conf.Name = "pf1";
            conf.count = "1";
            conf.Update();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SPFarm farm = SPFarm.Local;
            Counter conf = (Counter)farm.GetObject(new Guid(Counter.SettingGuid));
            if (conf == null)
            {
                conf = new Counter(farm);
            }
            //------- *conf is null*-----
            conf = (Counter)farm.GetObject(new Guid(Counter.SettingGuid));
            //------- *conf is null*-----

            String name = conf.name;
            String count = conf.count;
        }
    }
}

最后,我认为源代码有错误

这样修改,没关系

private void Write_Click(object sender, EventArgs e)
        {     
            SPFarm farm = SPFarm.Local;

            Counter conf = (Counter)farm.GetObject(new Guid(Counter.SettingGuid));


            if (conf == null)
            {
                conf = new Counter(farm);
            }
            conf.Name = "pf1";
            conf.count = "1";
            conf.Update();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SPFarm farm = SPFarm.Local;
            Counter conf = (Counter)farm.GetObject(new Guid(Counter.SettingGuid));

            String name = conf.name;
            String count = conf.count;
        }