Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net WCF客户端服务器NullReferenceException_Asp.net_Wcf_Xpo - Fatal编程技术网

Asp.net WCF客户端服务器NullReferenceException

Asp.net WCF客户端服务器NullReferenceException,asp.net,wcf,xpo,Asp.net,Wcf,Xpo,好的,这是我一整天都在处理的问题: 我正在构建一个web应用程序表单,在服务器端使用WCF数据服务和XPO作为ORM数据模型,其中包含一个方法,在客户端,我尝试在验证后从web表单向数据库添加一行 用户可以使用以下代码登录而不会出现问题: try { ctx = null; ctx = new XpoContext(serviceRootUri); ctx.Credentia

好的,这是我一整天都在处理的问题:

我正在构建一个web应用程序表单,在服务器端使用WCF数据服务和XPO作为ORM数据模型,其中包含一个方法,在客户端,我尝试在验证后从web表单向数据库添加一行

用户可以使用以下代码登录而不会出现问题:

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;
                }
         }
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();
现在,我想用以下代码向数据库添加一行:

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;
                }
         }
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();
但在前两行执行后,会发生异常

我在登录页面中将我的上下文声明为静态:

private static Uri serviceRootUri = new Uri("http://localhost:28748/WcfDataService1.svc/");
public static XpoContext ctx;
如您所见,我使用LogIn.ctx调用adduser web表单中的上下文(与登录页面中的上下文相同)


谢谢

有什么例外?我已经解决了我的问题,必须将此添加到我的web.config: