调用wcf的get函数会抛出CommunicationException

本文关键字:CommunicationException 函数 wcf get 调用 | 更新日期: 2023-09-27 17:49:25

我有两个项目。一个具有wcf服务和数据库。其他用于调用这些函数。如果我调用webservice的一个函数,它会向数据库添加一些东西,它会工作得很好。但是当我调用从数据库中检索的函数时。我得到CommunicationException。我在WebService.svc.cs中调用的函数是:

public List<Artist> GetAllArtists()
        {
            Database1Entities db = new Database1Entities();
            return (db.Artists).ToList();
        }

我这样称呼它

    protected void Page_Load(object sender, EventArgs e)
    {
        ServiceReference2.WebServiceClient o = new ServiceReference2.WebServiceClient();
        Artist [] artists = o.GetAllArtists(); //Exception arises here
        string str = "";
        foreach (Artist artist in artists){
            str += artist.ArtistID + " ";
        }
        Response.Write(str);
        gv.DataSource = artists;
        gv.DataBind();
    }
异常:

System.ServiceModel。CommunicationException未被用户代码处理HResult=-2146233087 Message=接收HTTP时发生错误回复http://localhost:23060/WebService.svc。这可能是由于到不使用HTTP协议的服务端点绑定。这也可能是由于HTTP请求上下文被服务器(可能是由于服务关闭)。查看服务器日志了解更多详情。Source=mscorlib StackTrace:服务器堆栈跟踪:atSystem.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException (WebExceptionhttpexception, HttpWebRequest request, HttpAbortReason, abortReason)System.ServiceModel.Channels.HttpChannelFactory1.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间间隔超时)在System.ServiceModel.Channels.RequestChannel。请求(消息消息,时间间隔超时)在System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息
消息,TimeSpan超时)在System.ServiceModel.Channels.ServiceChannel。调用(String action, Boolean单向,ProxyOperationRuntime操作,Object[] in,对象[]out,超时时间System.ServiceModel.Channels.ServiceChannelProxy.InvokeService (IMethodCallMessage方法调用,ProxyOperationRuntime操作)在System.ServiceModel.Channels.ServiceChannelProxy.Invoke (IMessage
消息)[0]异常被重新抛出:System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage (IMessagereqMsg (IMessage)在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 ' ' WebLab6AlbumManagementSystemClient '服务项目
引用' ServiceReference2 ' Reference.cs: 567行在WebLab6AlbumManagementSystemClient.ViewAllArtists.Page_Load(对象
sender, EventArgs e) in c:'Users'Muhammad Rehan'Documents'Visual
工作室
2013 ' ' WebLab6AlbumManagementSystemClient ' ViewAllArtists.aspx.cs项目:行16在System.Web.Util.CalliEventHandlerDelegateProxy。回调(对象发送者,EventArgs e)在System.Web.UI.Control。OnLoad (EventArgs e)在System.Web.UI.Control.LoadRecursive ()在System.Web.UI.Page。ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

InnerException: System.Net.WebExceptionHResult = -2146233079消息=底层连接已关闭:接收时发生意外错误。源=系统加:在System.Net.HttpWebRequest.GetResponse ()System.ServiceModel.Channels.HttpChannelFactory1.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间间隔InnerException: System.IO.IOException HResult=-2146232800 . timeout消息=无法从传输连接读取数据:已存在远程主机强制关闭连接。源=系统StackTrace:在System.Net.Sockets.NetworkStream读(Byte[]缓冲区,在System.Net.PooledStream.Read(Byte)的Int32偏移量,Int32大小[]缓冲区,Int32偏移量,Int32大小)System.Net.Connection。SyncRead(HttpWebRequest请求,Boolean)userRetrievedStream, Boolean, probeRead)System.Net.Sockets.SocketException HResult=-2147467259 Message=An远程主机强制关闭现有连接Source=System ErrorCode=10054 NativeErrorCode=10054 StackTrace: atSystem.Net.Sockets.Socket。接收(Byte[] buffer, Int32 offset, Int32 .大小,SocketFlags, SocketFlags)System.Net.Sockets.NetworkStream。读取(Byte[] buffer, Int32偏移量)Int32 size) InnerException:

调用wcf的get函数会抛出CommunicationException

所有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();
}

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

相关文章:
  • 没有找到相关文章