【问题标题】:Singleton class Background Services单例类后台服务
【发布时间】:2016-06-15 20:51:32
【问题描述】:

我有一个包含 3 个项目的解决方案,

  1. MainProject(UWP 应用程序)
  2. 通用(类库)
  3. 后台服务(Windows 运行时应用程序)

MainProject 引用 Common 和 Background Services,Background Services 引用 Common。

我还有一个存储在 Common 中的单例类。当从 MainProject 引用时,单例类可以正常工作,但是当我尝试从后台服务项目中引用单例类时,单例类中的所有属性都是空的。

我已尝试检查单例类的 GetInstance 属性。从后台服务引用单例类时不会创建新实例,但属性仍然为空。

单例类:

public class UserTokenInfo
{
    private static UserTokenInfo instance = null;
    private static object lockThis = new object();

    private UserTokenInfo() { }

    public static UserTokenInfo GetInstance
    {
        get
        {
            lock (lockThis)
            {
                if (instance == null)
                    instance = new UserTokenInfo();

                return instance;
            }
        }
    }

    public string access_token { get; set; }
    public string expiry { get; set; }
    public string Email { get; set; }

    public bool isTokenValid()
    {
        if (Convert.ToDateTime(expiry) > DateTime.Now)
            return true;
        return false;
    }

    public string FirstName { get; set; }
    public string SecondName { get; set; }
    public List<string> Topic { get; set; } = new List<string>();
    public List<string> Website { get; set; } = new List<string>();
}

【问题讨论】:

    标签: c# windows-runtime singleton background-task


    【解决方案1】:

    看起来您已经为应用程序中的一个公共模块创建了两个实例。

    尝试从其中一个项目中排除库的静态绑定,然后尝试。

    使用 DLL/引用而不是静态库。希望对你有帮助

    【讨论】:

    • 您好,感谢您的回复,能否请您解释一下我将如何在 VS2015 中执行此操作
    猜你喜欢
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    相关资源
    最近更新 更多