WCF 服务获取响应方法中的内部 500 错误

发布于 2024-11-24 13:58:13 字数 1514 浏览 5 评论 0原文

以下是我的请求,我在 getresponse 收到 500 内部服务器错误

string requestData = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Header><h:HeaderItem xmlns:h=\"http://tempuri.org/\">a header item</h:HeaderItem><ActivityId CorrelationId=\"090c553b-bfcc-4e4f-94cd-1b4333fe82a9\" xmlns=\"http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics\">377a454b-b543-4c6f-b4ac-3981029b60e6</ActivityId></s:Header><s:Body><string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">a body item</string></s:Body></s:Envelope>";
byte[] requestDataBytes = Encoding.UTF8.GetBytes(requestData);
 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/WebService/");
                request.Method = "POST";
                request.ContentType = "text/xml; charset=utf-8";
                request.Headers.Add("SOAPAction", "http://tempuri.org/IWebService/GetMessage");
                request.ContentLength = requestDataBytes.Length;

                StreamWriter streamWriter = new StreamWriter(request.GetRequestStream());
                streamWriter.Write(requestData);
                streamWriter.Flush();
                streamWriter.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader streamReader = new StreamReader(response.GetResponseStream());
                string responseBody = streamReader.ReadToEnd();

Below is my request i get a 500 internal server error at getresponse

string requestData = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Header><h:HeaderItem xmlns:h=\"http://tempuri.org/\">a header item</h:HeaderItem><ActivityId CorrelationId=\"090c553b-bfcc-4e4f-94cd-1b4333fe82a9\" xmlns=\"http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics\">377a454b-b543-4c6f-b4ac-3981029b60e6</ActivityId></s:Header><s:Body><string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">a body item</string></s:Body></s:Envelope>";
byte[] requestDataBytes = Encoding.UTF8.GetBytes(requestData);
 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/WebService/");
                request.Method = "POST";
                request.ContentType = "text/xml; charset=utf-8";
                request.Headers.Add("SOAPAction", "http://tempuri.org/IWebService/GetMessage");
                request.ContentLength = requestDataBytes.Length;

                StreamWriter streamWriter = new StreamWriter(request.GetRequestStream());
                streamWriter.Write(requestData);
                streamWriter.Flush();
                streamWriter.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader streamReader = new StreamReader(response.GetResponseStream());
                string responseBody = streamReader.ReadToEnd();

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

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

发布评论

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

评论(2

哽咽笑 2024-12-01 13:58:13

我也许可以给你一个答案。

将设置标头的行移至设置内容类型之前的位置,然后重试代码,如下所示:

       request.Headers.Add("SOAPAction", "http://tempuri.org/IWebService/GetMessage");
       request.ContentType = "text/xml; charset=utf-8";

我根据 WebRequest 对象的 ContentType 属性的文档提出此建议MS:

WebRequest 上的 MS 文档

此属性的值存储在 WebHeaderCollection 中。如果
WebHeaderCollection 设置后,属性值丢失。

现在,我意识到我们没有明确设置 WebHeaderCollection,但是您在该集合中设置了一个标头,这让我怀疑至少这是一个问题的可能性 - 将现有的 ContentType 渲染为空白,并被解释为 Web 服务入站端的某些默认值。

也许机会渺茫,但值得一试。

I may have an answer for you.

Move the line where you set your headers to the point before you set the content type, and retry your code, as follows:

       request.Headers.Add("SOAPAction", "http://tempuri.org/IWebService/GetMessage");
       request.ContentType = "text/xml; charset=utf-8";

I make this suggestion based on the documentation for the ContentType property for the WebRequest object from MS:

MS docs on WebRequest

The value for this property is stored in WebHeaderCollection . If
WebHeaderCollection is set, the property value is lost.

Now, I realize we're not expressly setting the WebHeaderCollection, but you are setting a header in that collection, and it made me suspect at least the possibility of this being a problem - rendering your existing ContentType blank, and being interpreted as some default on the inbound side of the web service.

Maybe a long shot, but it might be worth a try.

岁月染过的梦 2024-12-01 13:58:13

您应该在服务器上启用 WCF 跟踪日志记录并查看它是否指示错误。通常,反序列化期间或在命中应用程序代码之前在 WCF 层中发生的错误将写入此日志。

有关设置 WCF 跟踪日志记录的详细信息,请参阅此处这里

You should enable WCF trace logging on your server and see if it indicates an error. Typically errors that occur during deserialization or in the WCF layers before hitting your app code will write to this log.

For more info on setting up WCF trace logging, see here or here.

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