0

This problem is in regards to LinqToTwitter. I am trying to make a new instance of MvcAuthorizer but I keep getting a null exception when trying to assign the keys to the credential store. Here is the specific error: An exception of type 'System.NullReferenceException' occurred in LinqToTwitter.AspNet.dll but was not handled in user code

Update

This is getting weirder. So after getting the same initial error after changing over to StateServer, I decided to change SessionStateCredentialStore to InMemoryCredentialStore and it then appears to work, except that the data isn't returned. I can see the successful request to twitter in fiddler, but the data is not returned to the code as it appears to stop executing.

and here is the code wrapped in a constructor, which by the way is not in a controller:

public class Twitter
{

// FIELDS
    private readonly MvcAuthorizer _auth;

    // PROPERTIES
    public TwitterContext Twit
    {
        get
        {
            return new TwitterContext(_auth);
        }
    }
public Twitter()
    {
        // all keys
        var cKey = ConfigurationManager.AppSettings[
            "twitConsumerKey"];
        var cSecKey = ConfigurationManager.AppSettings[
            "twitConsumerSecret"];
        var oToken = AuthTool.getUserTwitterTokens().token;
        var oTokenSecc = AuthTool.getUserTwitterTokens()
            .tokenSecret;

        ICredentialStore cred = new InMemoryCredentialStore();
        cred.ConsumerKey = cKey; // <--- Error here when using SessionStateCredentialStore but not when using InMemoryCredentialStore
        cred.ConsumerSecret = cSecKey;
        cred.OAuthToken = oToken;
        cred.OAuthTokenSecret = oTokenSecc;

        // get auth
        _auth = new MvcAuthorizer()
                {
                    CredentialStore = cred
                };
    }

public string RetrieveTwitterId(string twitName)
{
    // this block executes the query as verified with fiddler using InMemoryCredentialsStore 
    // but doesn't return anything to the user variable. The rest of the code does not execute.
    var user = Twit.User.SingleOrDefault
        (x => x.Type == UserType.Show &&
               x.ScreenName == twitName);

    if(user != null)
    {
       return user.UserID.ToString("F0");
    }
    return null;
 }
 }

I call the method from within a web api method:

[HttpPost]
public HttpResponseMessage CreateNewInterview(Model.NewInterview interview)
    {
        // find twitter Id
        var twitterId = new Twitter().RetrieveTwitterId(interview.SubjectTwitterName);// error 


        // assign model
        var model = new NewInterview
            (interview.Title, twitterId, DateTime.Now, interview.PublicQuestions,   interview.Category);

        // create the new interview
        Admin.createInterview(model);

        // create response
        return Request.CreateResponse(HttpStatusCode.OK);
    }

What is the cause of this?

user1206480
  • 1,798
  • 3
  • 28
  • 45

1 Answers1

0

Where are you using it? I will take a wild guess that it is the session that is null. Make sure the session state is available where you are using this.

JuhaKangas
  • 875
  • 5
  • 17
  • I am using it in the main start up project, if that is what you mean. What do I need to do make sure that the session state is available? – user1206480 May 10 '14 at 18:55
  • AcquireRequestState is the earliest point at which the sessionstate is available. Could you show us the code where you using this class? – JuhaKangas May 10 '14 at 23:20
  • I've made an update to the post. I am making the call from within a Web Api Method. – user1206480 May 11 '14 at 00:05
  • Ah, Web Api does not use session state by default...and you shouldn't use it ideally. So yeah, definately use another type, like InMemory that you were trying out. Not sure I can help you with you other problem that it isn't returning anything when you are using InMemory...can you provide anymore info on that? – JuhaKangas May 11 '14 at 22:23
  • Yeah , my guess is that since I am calling linqToTwitter from within web api it hijacks the http process and doesn't return control to web api. So I think I'll have to rethink my logic; implement my linqToTwitter logic before or after web api. – user1206480 May 11 '14 at 22:45