C# 调用wcf的get函数会引发CommunicationException

C# 调用wcf的get函数会引发CommunicationException,c#,asp.net,wcf,C#,Asp.net,Wcf,我有两个项目。有wcf服务和数据库的人。其他函数用于调用这些函数。如果我调用webservice的一个函数,向数据库中添加一些内容,它就可以正常工作。但当我调用一个从数据库中检索某些内容的函数时。我有沟通障碍。我在WebService.svc.cs中调用的函数是: public List<Artist> GetAllArtists() { Database1Entities db = new Database1Entities();

我有两个项目。有wcf服务和数据库的人。其他函数用于调用这些函数。如果我调用webservice的一个函数,向数据库中添加一些内容,它就可以正常工作。但当我调用一个从数据库中检索某些内容的函数时。我有沟通障碍。我在WebService.svc.cs中调用的函数是:

public List<Artist> GetAllArtists()
        {
            Database1Entities db = new Database1Entities();
            return (db.Artists).ToList();
        }
例外情况:

用户代码未处理System.ServiceModel.CommunicationException HResult=-2146233087 Message=接收HTTP时出错 对……的答复。这可能是应得的 到未使用HTTP协议的服务端点绑定。这 也可能是由于HTTP请求上下文被 服务器(可能是由于服务关闭)。请参阅服务器日志 更多细节。Source=mscorlib StackTrace:Server stack trace:at System.ServiceModel.Channel.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException、HttpWebRequest请求、HttpBortreason abortReason)位于 System.ServiceModel.Channel.HttpChannelFactory1.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度 超时) 在System.ServiceModel.Channels.RequestChannel.Request(消息, 时间跨度(超时) 在System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息
消息,TimeSpan(超时) 在System.ServiceModel.Channels.ServiceChannel.Call(字符串操作、布尔单向、ProxyOperationRuntime操作、对象[]ins、, 对象[]输出,时间跨度超时) 位于System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage 方法调用,ProxyOperationRuntime操作) 在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage
(信息) 在[0]处重试异常: 在System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage)中 reqMsg,IMessage retMsg) 在System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&
msgData,Int32类型) 在WebLab6AlbumManagementSystemClient.ServiceReference2.IWebService.GetAllArtists()上 在WebLab6AlbumManagementSystemClient.ServiceReference2.WebServiceClient.GetAllArtists()上 在c:\Users\Muhammad Rehan\Documents\Visual Studio中
2013\Projects\WebLab6AlbumManagementSystemClient\Service
References\servicerence2\Reference.cs:第567行 在WebLab6AlbumManagementSystemClient.ViewAllArtists.Page\u加载(对象
c:\Users\Muhammad Rehan\Documents\Visual中的发件人、事件参数(e)
演播室
2013\Projects\weblab6albummmanagementsystemclient\viewallarartists.aspx.cs:line 16 在System.Web.Util.CallEventHandlerDelegateProxy.Callback(对象发送方, 事件参数(e) 在System.Web.UI.Control.OnLoad(EventArgs e)中 在System.Web.UI.Control.LoadRecursive()中 在System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)

InnerException:System.Net.WebException HResult=-2146233079 Message=基础连接已关闭:接收时发生意外错误。 来源=系统 堆栈跟踪: 在System.Net.HttpWebRequest.GetResponse()中 位于System.ServiceModel.Channels.HttpChannelFactory1.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度 超时)InnerException:System.IO.IOException HResult=-2146232800 Message=无法从传输连接读取数据:现有 远程主机已强制关闭连接。来源=系统 StackTrace:at System.Net.Sockets.NetworkStream.Read(字节[]缓冲区, System.Net.PooledStream.Read(字节[]处的Int32偏移量,Int32大小) 缓冲区,Int32偏移量,Int32大小) System.Net.Connection.SyncRead(HttpWebRequest请求,布尔值 userRetrievedStream,布尔probeRead)内部异常: System.Net.Sockets.SocketException HResult=-2147467259消息=An 远程主机已强制关闭现有连接 Source=系统错误代码=10054 NativeErrorCode=10054堆栈跟踪:at System.Net.Socket.Socket.Receive(字节[]缓冲区,Int32偏移量,Int32 尺寸,SocketFlags(SocketFlags)位于 System.Net.Sockets.NetworkStream.Read(字节[]缓冲区,Int32偏移量, Int32大小)InnerException:


所有的WCF错误消息都是如此神秘,以至于总是不知道是什么导致了错误本身。 错误的原因很可能是某些序列化问题。 尝试添加日志记录或编写测试,以使用DataContractSerializer序列化艺术家数组

 protected void Page_Load(object sender, EventArgs e)
{
    ServiceReference2.WebServiceClient o = new ServiceReference2.WebServiceClient();
    Artist [] artists = o.GetAllArtists().ToArray(); //Exception will not //come now
    string str = "";
    foreach (Artist artist in artists){
        str += artist.ArtistID + " ";
    }
    Response.Write(str);
    gv.DataSource = artists;
    gv.DataBind();
}

在服务设置中检查集合默认类型是否选择为数组或列表。我认为在您的情况下,它被设置为List,因为服务返回List,而您在这里将其分配给数组变量。因此,请尝试添加.ToArray(),或将默认值更改为Array。

Artist
a
DataContract
?它是OperationContract。我的意思是GetAllArtists()是操作契约。然而,在Artist顶部没有任何内容,Artist是使用数据库中的ado.net实体数据模型从数据库自动生成的。
 protected void Page_Load(object sender, EventArgs e)
{
    ServiceReference2.WebServiceClient o = new ServiceReference2.WebServiceClient();
    Artist [] artists = o.GetAllArtists().ToArray(); //Exception will not //come now
    string str = "";
    foreach (Artist artist in artists){
        str += artist.ArtistID + " ";
    }
    Response.Write(str);
    gv.DataSource = artists;
    gv.DataBind();
}