WCF 服务获取响应方法中的内部 500 错误
以下是我的请求,我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也许可以给你一个答案。
将设置标头的行移至设置内容类型之前的位置,然后重试代码,如下所示:
我根据 WebRequest 对象的 ContentType 属性的文档提出此建议MS:
WebRequest 上的 MS 文档
现在,我意识到我们没有明确设置 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:
I make this suggestion based on the documentation for the ContentType property for the WebRequest object from MS:
MS docs on WebRequest
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.
您应该在服务器上启用 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.