你调用的对象是空的。???在肥皂中?

发布于 2024-09-28 00:57:13 字数 950 浏览 3 评论 0原文

时出现错误

尝试调用 Web 服务“System.NullReferenceException:未将对象引用设置为对象的实例” 。 此行出现错误

if (Authentication.Username == "x" &&
            Authentication.Password == "y")

这是什么意思?


[WebService(Namespace = "https://domain.com")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Testing : System.Web.Services.WebService
{
    public TestAuthHeader Authentication;
    public class TestAuthHeader : SoapHeader
    {
        public string Username;
        public string Password;
    }

    [WebMethod]
    [SoapHeader("Authentication")]
    public string TestService()
    {
        if (Authentication.Username == "x" &&
            Authentication.Password == "y")
        {
            return "OK";
        }
        else
        {
            return "Access Denided";
        }
    }
}

Got an error while trying to invoke the webservice

"System.NullReferenceException: Object reference not set to an instance of an object."
Error on this line

if (Authentication.Username == "x" &&
            Authentication.Password == "y")

what does this mean?


[WebService(Namespace = "https://domain.com")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Testing : System.Web.Services.WebService
{
    public TestAuthHeader Authentication;
    public class TestAuthHeader : SoapHeader
    {
        public string Username;
        public string Password;
    }

    [WebMethod]
    [SoapHeader("Authentication")]
    public string TestService()
    {
        if (Authentication.Username == "x" &&
            Authentication.Password == "y")
        {
            return "OK";
        }
        else
        {
            return "Access Denided";
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑白记忆 2024-10-05 00:57:13

尝试将类 TestAuthHeader 的定义移到 Web 服务本身之外...

另外,尝试测试未知标头以查看 Authentication 是否最终在那里...

[WebMethod]
[SoapHeader("Authentication")]
[SoapHeader("unknownHeaders",Required=false)]
public string TestService()
{
   foreach (SoapUnknownHeader header in unknownHeaders) {
      // Test header
      Debug.Write(header.Element.Name);
   }
}

除此之外,我们可以看到您调用 Web 服务的代码吗?

*编辑*

如果您没有在客户端执行任何操作来创建 TestAuthHeader 实例并设置其属性,那么您将在服务端获得空引用。来自使用 SoapHeaders 的 MSDN 示例(客户端代码):

MyHeader mySoapHeader = new MyHeader();

// Populate the values of the SOAP header.
mySoapHeader.Username = Username;
mySoapHeader.Password = SecurelyStoredPassword;

// Create a new instance of the proxy class.
MyWebService proxy = new MyWebService();

// Add the MyHeader SOAP header to the SOAP request.
proxy.MyHeaderValue = mySoapHeader;

// Call the method on the proxy class that communicates with
// your Web service method.
string results = proxy.MyWebMethod();

您真正需要的是:

public string TestService()
{
    if (Authentication == null) {
        return "Access is denied";
    }

    // ... the rest of your code

这将处理客户端未设置身份验证对象的情况。

Try moving the definition of class TestAuthHeader outside of the web service itself...

Also, try testing unknown headers to see if Authentication is ending up there...

[WebMethod]
[SoapHeader("Authentication")]
[SoapHeader("unknownHeaders",Required=false)]
public string TestService()
{
   foreach (SoapUnknownHeader header in unknownHeaders) {
      // Test header
      Debug.Write(header.Element.Name);
   }
}

Other than that, can we see the code where you're calling into the web service?

*Edit *

If you're not doing anything on the client side to create an instance of TestAuthHeader and set its properties, you're going to have a null reference on the service side. From the MSDN example of using SoapHeaders (client code):

MyHeader mySoapHeader = new MyHeader();

// Populate the values of the SOAP header.
mySoapHeader.Username = Username;
mySoapHeader.Password = SecurelyStoredPassword;

// Create a new instance of the proxy class.
MyWebService proxy = new MyWebService();

// Add the MyHeader SOAP header to the SOAP request.
proxy.MyHeaderValue = mySoapHeader;

// Call the method on the proxy class that communicates with
// your Web service method.
string results = proxy.MyWebMethod();

All you really need is:

public string TestService()
{
    if (Authentication == null) {
        return "Access is denied";
    }

    // ... the rest of your code

This will handle the scenario of where the client doesn't set an authentication object.

新雨望断虹 2024-10-05 00:57:13

我只是猜测,但在您的 TestService 方法中,Authentication 实例可能为 null,从而给您带来异常。也许您没有为该网站启用任何身份验证?

I'm just guessing, but in your TestService method, the Authentication instance is probably null, giving you that exception. Maybe you don't have any authentication turned on for the site?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文