默认构造函数从未调用过

时间:2014-10-20 05:49:20

标签: c# model-view-controller twitter-oauth

我正在使用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;
    }
}
}

1 个答案:

答案 0 :(得分:1)

如果有人遇到同样的问题......

我用

标记了默认的无参数构造函数
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容器)之后。