locked
NullReferenceException when calling a WCF Data Service Method RRS feed

  • Question

  • Hey everone, 

    Ok, here's the problem i've been dealing with the whole day: 

    I'm building a web application forms using WCF Data Service and XPO as ORM Data Model in the server side which contains a method, and in the client side, i'm trying to add a line to a database from a web form after authenfitication. 

    The user can log in without problems with this code: 

    try
               
    {
                    ctx
    = null;
                    ctx
    = new XpoContext(serviceRootUri);
               
                    ctx
    .Credentials = new NetworkCredential(UserName.Text, UserPass.Text);
                   
    var res = ctx.Users.FirstOrDefault();
                   
                   
    FormsAuthentication.RedirectFromLoginPage(UserName.Text, Persist.Checked);
               
    }

               
    catch (SecurityException ex)
                   
    {
                   
    Msg.Text = "Erreur: " + ex.Message;
                   
    }
             
    }

    now i want to add a line to a database with this code: 

    Uri u = new Uri(string.Format(LogIn.ctx.BaseUri + "/CreateUser?name='{0}'&pass='{1}'",
               
    New_UserName.Text, New_UserPass.Text), UriKind.RelativeOrAbsolute);

               
    LogIn.ctx.Execute(u, "GET");

               
    ServiceReference1.Users user = new ServiceReference1.Users();
                user
    .Nom = New_UserName.Text;
                user
    .Pass = New_UserPass.Text;


               
    LogIn.ctx.AddToUsers(user);
               
    LogIn.ctx.SaveChanges();

    but after executing in the 1st two lines an exception occurs. 

    i'm declaring my context as static in the logIn page: 

    private static Uri serviceRootUri = new Uri("http://localhost:28748/WcfDataService1.svc/");
    public static XpoContext ctx;

    As u can see i call my context in the adduser web form with LogIn.ctx ( the same context in the log in page )

    Thanks 

    Monday, April 15, 2013 11:44 AM

Answers

  • Your login code initializes ctx but what about the code you posted later?  Since this is a web app each postback results in a different instantiation of your form.  Note that you do not want to use a static field in your forms.  This is going to cause you way too many problems.  For example suppose 2 users try to log in at the same time.  They'll both try to set the context.  At best you should create the context as an instance field.  Based upon your code you should probably create it as a local var in the code that needs it.  This is especially true if it needs to be disposed.  In the worse case you can create it as a per-request object but start out simple.  I suspect your problem is that the context isn't initialized.  Moving away from static will help identify where the actual problem is.

    Please post ASP.NET questions in the ASP.NET forums (http://forums.asp.net). 

    Michael Taylor
    http://msmvps.com/blogs/p3net

    • Marked as answer by Bob Shen Friday, May 3, 2013 5:33 AM
    Monday, April 15, 2013 1:32 PM
  • Consdiering that all pre-written Exceptions are Serialiseable and are send back as "return value", this one is as likely to come from your code as it can come from the Webservice Function!

    So without the code of the websersive itself, we cannot really help you.

    Also, jsut the message of the exception is highly insifficient. Please post the results of Exception.Tostring() on the exception (or a screenshot of the dialog box taht appears on uncaught exceptions).


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2


    • Edited by Christopher84 Monday, April 15, 2013 4:53 PM
    • Marked as answer by Bob Shen Friday, May 3, 2013 5:33 AM
    Monday, April 15, 2013 4:50 PM

All replies

  • Your login code initializes ctx but what about the code you posted later?  Since this is a web app each postback results in a different instantiation of your form.  Note that you do not want to use a static field in your forms.  This is going to cause you way too many problems.  For example suppose 2 users try to log in at the same time.  They'll both try to set the context.  At best you should create the context as an instance field.  Based upon your code you should probably create it as a local var in the code that needs it.  This is especially true if it needs to be disposed.  In the worse case you can create it as a per-request object but start out simple.  I suspect your problem is that the context isn't initialized.  Moving away from static will help identify where the actual problem is.

    Please post ASP.NET questions in the ASP.NET forums (http://forums.asp.net). 

    Michael Taylor
    http://msmvps.com/blogs/p3net

    • Marked as answer by Bob Shen Friday, May 3, 2013 5:33 AM
    Monday, April 15, 2013 1:32 PM
  • Consdiering that all pre-written Exceptions are Serialiseable and are send back as "return value", this one is as likely to come from your code as it can come from the Webservice Function!

    So without the code of the websersive itself, we cannot really help you.

    Also, jsut the message of the exception is highly insifficient. Please post the results of Exception.Tostring() on the exception (or a screenshot of the dialog box taht appears on uncaught exceptions).


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2


    • Edited by Christopher84 Monday, April 15, 2013 4:53 PM
    • Marked as answer by Bob Shen Friday, May 3, 2013 5:33 AM
    Monday, April 15, 2013 4:50 PM