C# System.Web.Services.Protocols.SoapException:服务器无法处理请求。---> System.ArgumentNullException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/563162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:18:16  来源:igfitidea点击:

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.ArgumentNullException

c#web-services

提问by Daver

I've been getting the error:

我一直收到错误:

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.ArgumentNullException

System.Web.Services.Protocols.SoapException:服务器无法处理请求。---> System.ArgumentNullException

When I try to call my webservice using the following code:

当我尝试使用以下代码调用我的网络服务时:

{
//Create an echoSync request - set the services.
ServiceNameType sourceService = new ServiceNameType {Service = "echoSync", OnBehalfOf = "Source"};
ServiceNameType destService = new ServiceNameType {Service = "echoSync", OnBehalfOf = "Destination"};

//Create the request.
SDD2RequestType request = new SDD2RequestType
                  {
                      AppId = "echoSync",
                      SourceService = sourceService,
                      DestService = destService,
                      RequestId = "1",
                      RequestBody = "Ping"
                  };

//Create the originator.
originator originator = new originator {id = "123456789"};

//Create the transport.
SDD2Transport transport = new SDD2Transport {originatorValue = originator};

//Send the request.
SDD2ResponseType response = null;
response = transport.SDD2TransportOp(request);

//Write out the response.
System.Console.WriteLine(response.ResponseBody.ToString());
}

My webservice method is quite simple and it looks like this:

我的 webservice 方法非常简单,它看起来像这样:

    [System.Web.Services.Protocols.SoapHeaderAttribute("originatorValue", Direction = System.Web.Services.Protocols.SoapHeaderDirection.InOut)]
    [System.Web.Services.WebMethodAttribute()]
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://memberdirect.net/SDD2/Transport", RequestElementName = "SDD2Transport", RequestNamespace = "http://cucbc.com/sdd2/transport/", ResponseElementName = "SDD2TransportResponse", ResponseNamespace = "http://cucbc.com/sdd2/transport/", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Default)]
    [return: System.Xml.Serialization.XmlElementAttribute("SDD2Response", Namespace = "http://cucbc.com/sdd2/")]
    public override SDD2ResponseType SDD2TransportOp(SDD2RequestType SDD2Request)
    {
        if (SDD2Request == null) throw new ArgumentNullException("SDD2Request");
        var response = new SDD2ResponseType();

        switch (SDD2Request.AppId)
        {
            case "echoSync":
                response.AppId = SDD2Request.AppId;
                response.ProcessingStatus = ProcessingStatusType.Complete;
                response.RequestId = SDD2Request.RequestId;
                response.ResponseBody = "Pong";
                break;
            default:
                throw new System.NotImplementedException();
                break;
        }

        return response;
    }

When I make the call I know the request is not NULL but when it arrives at the webservice it is always received as null. I generated the webservice from WSDL using the wsdl.exe utility and clearly I don't understand all the details of SOAP that I should. Has anyone else run into this issue?

当我打电话时,我知道请求不是 NULL,但是当它到达 web 服务时,它总是被接收为 null。我使用 wsdl.exe 实用程序从 WSDL 生成了 web 服务,显然我不了解我应该了解的 SOAP 的所有细节。有没有其他人遇到过这个问题?

回答by configurator

I have no idea what causes this so this is not an answer to your question, but only a suggestion for a way to look further into the problem

我不知道是什么原因造成的,所以这不是您问题的答案,而只是对进一步研究问题的方法的建议

Try using a sniffer to look at the actual data being sent between the machines and find out on which side the problem is. I've used Wiresharksuccessfully once.

尝试使用嗅探器查看机器之间发送的实际数据,并找出问题出在哪一边。我曾经成功使用过Wireshark

回答by Daver

I didn't have a chance to use Wireshark as Configurator suggested but I was able to resolve this issue by Updating Web Reference in the Solution Explorer.

我没有机会按照 Configurator 的建议使用 Wireshark,但我能够通过在解决方案资源管理器中更新 Web 参考来解决此问题。

I'm a bit chagrined at having found such a simple solution to this after having wrestled with it for over an hour but I hope that this post helps someone out there.

在与它搏斗了一个多小时后找到了如此简单的解决方案,我有点懊恼,但我希望这篇文章可以帮助那里的人。

回答by John Saunders

"parameter is received as null" almost always means that there is a difference between the qualified names expected by the server, and those being sent by the client. An XML qualified name consists of the namespace plus the local name. This usally means that the namespace is incorrect on the client side, though I've seen one report of a difference with the name.

“参数接收为空”几乎总是意味着服务器期望的限定名称与客户端发送的限定名称之间存在差异。XML 限定名称由命名空间加上本地名称组成。这通常意味着命名空间在客户端是不正确的,尽管我看到过一份关于名称差异的报告。

In any case, you've found the #1 solution to the problem - "Update Web Reference".

无论如何,您已经找到了该问题的 #1 解决方案 - “更新 Web 参考”。

回答by Rob Kent

I had the same problem, although the cause might be different. When I stepped through my code in the debugger, the method argument went from being an instance to a null.

我遇到了同样的问题,尽管原因可能不同。当我在调试器中逐步执行我的代码时,方法参数从实例变为空值。

For the record it was caused by the RequestElementName being different to the method name. There may be other influencing factors but when I either removed the RequestElementName attribute from the method definition or I made it the same as the method name, the problem with the null argument disappeared.

根据记录,这是由 RequestElementName 与方法名称不同引起的。可能还有其他影响因素,但是当我从方法定义中删除 RequestElementName 属性或将其设置为与方法名称相同时,空参数的问题就消失了。

For example, your code has RequestElementName = "SDD2Transport" and method name SDD2TransportOp.

例如,您的代码具有 RequestElementName = "SDD2Transport" 和方法名称 SDD2TransportOp。

Now I don't know why this should be a problem, especially as you also have: ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Default, not Wrapped, which implies that the ElementName will not be used anyway.

现在我不知道为什么这应该是一个问题,尤其是当您还有:ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Default, not Wrapped,这意味着无论如何都不会使用 ElementName。

I wonder if this is a bug in the .NET SOAP client protocol class because what happens is that although the correct method is called in the web service (because it is identified in the SOAP Action header), the arguments do not get deserialized.

我想知道这是否是 .NET SOAP 客户端协议类中的错误,因为尽管在 Web 服务中调用了正确的方法(因为它在 SOAP Action 标头中标识),但参数并未反序列化。

In my case I had to edit the generated C# class every time I recreated it in order to remove the RequestElementName attributes. Note that I had no control over the source WSDL as it was supplied by a 3rd party.

就我而言,每次重新创建生成的 C# 类时,我都必须编辑它,以删除 RequestElementName 属性。请注意,我无法控制源 WSDL,因为它是由第 3 方提供的。