C# 为什么WCF和ASP.NET web应用程序中的连接意外关闭?
这是我在Asp.net web应用程序中使用的代码。它正在使用一个WCF服务,其中有一个函数receiveMsg() receiveMsg()看起来像-C# 为什么WCF和ASP.NET web应用程序中的连接意外关闭?,c#,asp.net,visual-studio,linq,wcf,C#,Asp.net,Visual Studio,Linq,Wcf,这是我在Asp.net web应用程序中使用的代码。它正在使用一个WCF服务,其中有一个函数receiveMsg() receiveMsg()看起来像- public ArrayList receiveMsg(String receiverId) { XDocument xdoc = XDocument.Load(@"C:\Users\abhinav\documents\visual studio 2010\Projects\WcfService1\Wcf
public ArrayList receiveMsg(String receiverId)
{
XDocument xdoc = XDocument.Load(@"C:\Users\abhinav\documents\visual studio 2010\Projects\WcfService1\WcfService1\Messages.xml");
//XDocument xdoc = XDocument.Load(@"~/App_Data/Messages.xml");
ArrayList messages = new ArrayList();
var elements = from item in xdoc.Descendants("Messages").Descendants("Message")
where item.Element("ReceiverId").Value == receiverId
select new{ msgtxt = item.Element("MessageText").Value, sendid = item.Element("SenderId").Value };
foreach (var element in elements)
{
Message msg = new Message();
msg.sender_Id = element.sendid;
msg.message = element.msgtxt;
messages.Add(msg);
}
return messages;
}
我做错了什么?是Linq还是WCF设置等
编辑-
这是我在IService.cs中添加的-
[DataContract]
public class Message
{
[DataMember]
public String sender_Id{get; set;}
[DataMember]
public String message { get; set; }
}
所以我想做的就是-
XML文件包含大量消息。我想检索与接收方Id对应的所有邮件和发件人Id。为此,我创建了一个邮件类,其中发件人Id和邮件为成员。我想创建一个消息类对象的数组列表,并将其返回给客户端
堆栈跟踪-
Communication ExceptionSystem.ServiceModel.CommunicationException: The underlying connection was closed: The connection was closed unexpectedly. ---> System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
--- End of inner exception stack trace ---
Server stack trace:
at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at wcf_web_app.ServiceReference1.IService1.receiveMsg(String receiverId)
at wcf_web_app.ServiceReference1.Service1Client.receiveMsg(String receiverId) in C:\Users\abhinav\documents\visual studio 2010\Projects\wcf_web_app\wcf_web_app\Service References\ServiceReference1\Reference.cs:line 136
at wcf_web_app._Default.Button2_Click(Object sender, EventArgs e) in C:\Users\abhinav\documents\visual studio 2010\Projects\wcf_web_app\wcf_web_app\Default.aspx.cs:line 43
我觉得在生成消息类对象的ArrayList并返回它时存在一些问题。有什么问题吗?首先在代码中添加一些基本的错误处理,看看能找到什么。事实上,我知道问题所在,问题就在我上面提到的那一行。对象是从WCF返回的,但没有被强制转换到ArrayList,我不知道该怎么办。如果receiveMsg的结果已经是ArrayList,为什么要将其强制转换到ArrayList?所以这不是问题所在。问题可能是结果是一个空列表(因为我假设您没有得到异常)。错误不在行中,而是在receiveMsg中。也许receiverId不会导致匹配(区分大小写?)。作为旁注,您不应该将结果保存到磁盘,然后再次加载。在控制台的wcf中,我已经记录了结果,并且receiveMsg工作正常,它会提供所需的输出。如果receiveMsg工作正常,那么问题出在哪里?它已经是一个ArrayList,所以这不成问题。如果你得到了一个例外,那么把它添加到问题中。此外,如果ReceiveMsg不相关,则将其删除,标题与问题有何关系?请阅读:
[DataContract]
public class Message
{
[DataMember]
public String sender_Id{get; set;}
[DataMember]
public String message { get; set; }
}
Communication ExceptionSystem.ServiceModel.CommunicationException: The underlying connection was closed: The connection was closed unexpectedly. ---> System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
--- End of inner exception stack trace ---
Server stack trace:
at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at wcf_web_app.ServiceReference1.IService1.receiveMsg(String receiverId)
at wcf_web_app.ServiceReference1.Service1Client.receiveMsg(String receiverId) in C:\Users\abhinav\documents\visual studio 2010\Projects\wcf_web_app\wcf_web_app\Service References\ServiceReference1\Reference.cs:line 136
at wcf_web_app._Default.Button2_Click(Object sender, EventArgs e) in C:\Users\abhinav\documents\visual studio 2010\Projects\wcf_web_app\wcf_web_app\Default.aspx.cs:line 43