如何解决System.NullReferenceException

时间:2012-12-13 10:30:32

标签: c# php web-services nullreferenceexception

我在php页面中调用了一个Web服务。 Web服务使用C#。当我尝试使用soap客户端对象调用方法时,它会显示如下错误:

System.NullReferenceException: Object reference not set to an instance of an object.

我用来调用Web服务方法的代码是:

 $Username = "username";
 $Password = "password";
 $LifetimeRequest = 60*60*24;


$soap_data = array(
   'Username' => $Username,
   'Password' => $Password,
   'LifetimeRequest' => $LifetimeRequest
);
$client = new SoapClient('http://50.56.173.161:8502/AdomniService.svc?wsdl');
$response = $client->ClientLogin($soap_data);
var_dump($response);

当我使用var_dump时,它会显示如下输出:

object(stdClass)#2 (1) {
  ["ClientLoginResult"]=>
  object(stdClass)#3 (3) {
    ["Error"]=>
    object(stdClass)#4 (5) {
      ["Private"]=>
      float(2)
      ["Public"]=>
      int(1)
      ["Details"]=>
      string(284) "System.NullReferenceException: Object reference not set to an instance of an object.
   at Adomni.AdomniService.ClientLogin(ClientLoginRequest request) in C:\Users\megiddo\Documents\Visual Studio 2010\Projects\Adomni\AdOmniAPIService\AdomniService\AdomniClientService.svc.cs:line 107"
      ["ErrorCode"]=>
      int(0)
      ["ErrorMessage"]=>
      NULL
    }
    ["Status"]=>
    int(-1)
    ["Token"]=>
    object(stdClass)#5 (8) {
      ["Private"]=>
      float(2)
      ["Public"]=>
      int(1)
      ["EventNotificationUri"]=>
      NULL
      ["IsManager"]=>
      bool(false)
      ["LifetimeRequest"]=>
      int(0)
      ["Password"]=>
      NULL
      ["TokenId"]=>
      int(0)
      ["UserName"]=>
      NULL
    }
  }
}

谁能告诉我这里做错了什么?提前谢谢。

C#中使用的代码如下:

using System; 
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Security;
using System.IO;
using System.Data;
using AdOmniWebPortal.AdOmniService;

namespace AdOmniWebPortal
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void AdOmniLogin_Authenticate(object sender, AuthenticateEventArgs e)
        {
            AdomniServiceClient Client = new AdomniServiceClient();
            LoginRequest LoginRequest = new LoginRequest();
            LoginResponse LoginResponse = new LoginResponse();

            LoginRequest.Username = AdOmniLogin.UserName;
            LoginRequest.Password = AdOmniLogin.Password;
            LoginRequest.LifetimeRequest = 60*60*24;
            //This guy will be changed
            LoginRequest.EventNotificationURL = new Uri("http://herp-a-derp.com/awesome.html");

            LoginResponse = Client.Login(LoginRequest);

            if (LoginResponse.Status == 0)
            {
                System.Web.Security.FormsAuthentication.RedirectFromLoginPage(LoginResponse.Token.UserName, true);
                LifetimeToken token = LoginResponse.Token;
                Session["Token"] = token;

                GetUserRequest request = new GetUserRequest() { Token = token };
                GetUserResponse response = Client.GetUser(request);

                if (response.GetUser.Type == AdOmniService.UserType.Seller)
                {
                    Response.Redirect("~/Pages/Seller/SellerHomeDashboard.aspx");
                }

                if (response.GetUser.Type == AdOmniService.UserType.Client)
                {
                    Response.Redirect("~/Pages/Buyer/BuyerHomeDashboard.aspx");
                }

                if (response.GetUser.Type == AdOmniService.UserType.None)
                {
                    Response.Redirect("~/Pages/Buyer/BuyerHomeDashboard.aspx");
                }
            }
            else
            {
                Response.Redirect("~/Login.aspx");
                Response.Write(LoginResponse.Error.ErrorMessage);
            }
        }
    }
}

我已将整个.cs页面内容放在编辑中。

2 个答案:

答案 0 :(得分:1)

使用Fiddler(http调试代理)

这将允许您在对Web服务发出的请求(以xml格式)内达到峰值

所以你可以看到你是否遗漏了任何东西。

通过fiddler引导您的c#客户端,并查看

http://www.fiddler2.com/fiddler2/

答案 1 :(得分:0)

也许在PHP脚本中,您还应该设置EventNotificationURL变量。

在错误响应中查看此部分:

  ["EventNotificationUri"]=>
  NULL

也许该服务希望您传递EventNotificationUri值,就像传递PasswordUsernameLifetimeRequest一样。

<强> [编辑]

尝试将变量名称从Username更改为UserName。据我所知,PHP应该区分大小写,所以"Username" != "UserName"