从未调用默认构造函数

本文关键字:构造函数 默认 调用 | 更新日期: 2023-09-27 18:27:10

我正在使用Andy创建的oAuthTwitterTimeline示例,但我很难确保调用默认构造函数(Parameterless constructor)从我的web.config文件中提取数据,而不是让我在中硬编码应用程序设置。

我正在调用方法"GetMyTimeline()",但调用AuthenticateSettings。

当我最终跳转到AuthenticateMe方法时,我得到了一堆空引用异常。

AuthResponse twitAuthResponse = authenticate.AuthenticateMe(AuthenticateSettings);

这是无参数构造函数

public OAuthTwitterWrapper()
    {
        string oAuthConsumerKey = ConfigurationManager.AppSettings["oAuthConsumerKey"];
        string oAuthConsumerSecret = ConfigurationManager.AppSettings["oAuthConsumerSecret"];
        string oAuthUrl = ConfigurationManager.AppSettings["oAuthUrl"];
        AuthenticateSettings = new AuthenticateSettings { OAuthConsumerKey = oAuthConsumerKey, OAuthConsumerSecret = oAuthConsumerSecret, OAuthUrl = oAuthUrl };
        string screenname = ConfigurationManager.AppSettings["screenname"];
        string include_rts = ConfigurationManager.AppSettings["include_rts"];
        string exclude_replies = ConfigurationManager.AppSettings["exclude_replies"];
        int count = Convert.ToInt16(ConfigurationManager.AppSettings["count"]);
        string timelineFormat = ConfigurationManager.AppSettings["timelineFormat"];         
        TimeLineSettings = new TimeLineSettings
        {
            ScreenName = screenname,
            IncludeRts = include_rts,
            ExcludeReplies = exclude_replies,
            Count = count,
            TimelineFormat = timelineFormat
        };
        string searchFormat = ConfigurationManager.AppSettings["searchFormat"];
        string searchQuery = ConfigurationManager.AppSettings["searchQuery"];
        SearchSettings = new SearchSettings
        {
            SearchFormat = searchFormat,
            SearchQuery = searchQuery
        };
    }

控制器没有什么特别之处

public class HomeController : Controller
{
    private readonly OAuthTwitterWrapper.OAuthTwitterWrapper _oAuthTwitterWrapper;
    public HomeController(OAuthTwitterWrapper.OAuthTwitterWrapper oAuthTwitterWrapper)
    {
        _oAuthTwitterWrapper = oAuthTwitterWrapper;
    }
    public ActionResult Index()
    {
        return View();
    }
    public JsonResult GetTwitterFeed()
    {
        return Json(_oAuthTwitterWrapper.GetMyTimeline(), JsonRequestBehavior.AllowGet);
    }
//Some more stuff
}

全班"OAuthTwitterWrapper"

namespace OAuthTwitterWrapper
{
public class OAuthTwitterWrapper : IOAuthTwitterWrapper
{
    public IAuthenticateSettings AuthenticateSettings { get; set; }
    public ITimeLineSettings TimeLineSettings { get; set; }
    public ISearchSettings SearchSettings { get; set; }
    /// <summary>
    /// The default constructor takes all the settings from the appsettings file
    /// </summary>
    public OAuthTwitterWrapper()
    {
        string oAuthConsumerKey = ConfigurationManager.AppSettings["oAuthConsumerKey"];
        string oAuthConsumerSecret = ConfigurationManager.AppSettings["oAuthConsumerSecret"];
        string oAuthUrl = ConfigurationManager.AppSettings["oAuthUrl"];
        AuthenticateSettings = new AuthenticateSettings { OAuthConsumerKey = oAuthConsumerKey, OAuthConsumerSecret = oAuthConsumerSecret, OAuthUrl = oAuthUrl };
        string screenname = ConfigurationManager.AppSettings["screenname"];
        string include_rts = ConfigurationManager.AppSettings["include_rts"];
        string exclude_replies = ConfigurationManager.AppSettings["exclude_replies"];
        int count = Convert.ToInt16(ConfigurationManager.AppSettings["count"]);
        string timelineFormat = ConfigurationManager.AppSettings["timelineFormat"];         
        TimeLineSettings = new TimeLineSettings
        {
            ScreenName = screenname,
            IncludeRts = include_rts,
            ExcludeReplies = exclude_replies,
            Count = count,
            TimelineFormat = timelineFormat
        };
        string searchFormat = ConfigurationManager.AppSettings["searchFormat"];
        string searchQuery = ConfigurationManager.AppSettings["searchQuery"];
        SearchSettings = new SearchSettings
        {
            SearchFormat = searchFormat,
            SearchQuery = searchQuery
        };
    }
    /// <summary>
    /// This allows the authentications settings to be passed in
    /// </summary>
    /// <param name="authenticateSettings"></param>
    public OAuthTwitterWrapper(IAuthenticateSettings authenticateSettings)
    {
        AuthenticateSettings = authenticateSettings;
    }
    /// <summary>
    /// This allows the authentications and timeline settings to be passed in
    /// </summary>
    /// <param name="authenticateSettings"></param>
    /// <param name="timeLineSettings"></param>
    public OAuthTwitterWrapper(IAuthenticateSettings authenticateSettings, ITimeLineSettings timeLineSettings)
    {
        AuthenticateSettings = authenticateSettings;
        TimeLineSettings = timeLineSettings;
    }
    /// <summary>
    /// This allows the authentications, timeline and search settings to be passed in
    /// </summary>
    /// <param name="authenticateSettings"></param>
    /// <param name="timeLineSettings"></param>
    /// <param name="searchSettings"></param>
    public OAuthTwitterWrapper(IAuthenticateSettings authenticateSettings, ITimeLineSettings timeLineSettings, ISearchSettings searchSettings)
    {
        AuthenticateSettings = authenticateSettings;
        TimeLineSettings = timeLineSettings;
        SearchSettings = searchSettings;
    }
    public string GetMyTimeline()
    {
        var timeLineJson = string.Empty;
        IAuthenticate authenticate = new Authenticate();
        AuthResponse twitAuthResponse = authenticate.AuthenticateMe(AuthenticateSettings);
        // Do the timeline
        var utility = new Utility();
        timeLineJson = utility.RequstJson(TimeLineSettings.TimelineUrl, twitAuthResponse.TokenType, twitAuthResponse.AccessToken);
        return timeLineJson;
    }
    public string GetSearch()
    {
        var searchJson = string.Empty;
        IAuthenticate authenticate = new Authenticate();
        AuthResponse twitAuthResponse = authenticate.AuthenticateMe(AuthenticateSettings);
        // Do the timeline
        var utility = new Utility();
        searchJson = utility.RequstJson(SearchSettings.SearchUrl, twitAuthResponse.TokenType, twitAuthResponse.AccessToken);
        return searchJson;
    }
}
}

从未调用默认构造函数

如果有人有同样的问题。。。

我用标记了默认的无参数构造函数

namespace OAuthTwitterWrapper
{
public class OAuthTwitterWrapper : IOAuthTwitterWrapper
{
public IAuthenticateSettings AuthenticateSettings { get; set; }
public ITimeLineSettings TimeLineSettings { get; set; }
public ISearchSettings SearchSettings { get; set; }

[InjectConstructor]
public OAuthTwitterWrapper()
{
    string oAuthConsumerKey = ConfigurationManager.AppSettings["oAuthConsumerKey"];
    string oAuthConsumerSecret = ConfigurationManager.AppSettings["oAuthConsumerSecret"];
    string oAuthUrl = ConfigurationManager.AppSettings["oAuthUrl"];
    AuthenticateSettings = new AuthenticateSettings { OAuthConsumerKey = oAuthConsumerKey, OAuthConsumerSecret = oAuthConsumerSecret, OAuthUrl = oAuthUrl };
    string screenname = ConfigurationManager.AppSettings["screenname"];
    string include_rts = ConfigurationManager.AppSettings["include_rts"];
    string exclude_replies = ConfigurationManager.AppSettings["exclude_replies"];
    int count = Convert.ToInt16(ConfigurationManager.AppSettings["count"]);
    string timelineFormat = ConfigurationManager.AppSettings["timelineFormat"];         
    TimeLineSettings = new TimeLineSettings
    {
        ScreenName = screenname,
        IncludeRts = include_rts,
        ExcludeReplies = exclude_replies,
        Count = count,
        TimelineFormat = timelineFormat
    };
    string searchFormat = ConfigurationManager.AppSettings["searchFormat"];
    string searchQuery = ConfigurationManager.AppSettings["searchQuery"];
    SearchSettings = new SearchSettings
    {
        SearchFormat = searchFormat,
        SearchQuery = searchQuery
    };

在参考Unity(我的IoC容器)之后。