【问题标题】:Calling a get function of wcf throws CommunicationException调用 wcf 的 get 函数抛出 CommunicationException
【发布时间】:2015-07-29 05:10:51
【问题描述】:

我有两个项目。一个具有 wcf 服务和数据库。其他用于调用这些函数。如果我调用一个向数据库添加内容的 webservice 函数,它工作正常。但是当我调用一个从数据库中检索某些东西的函数时。我得到通信异常。我在 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 消息=接收 HTTP 时发生错误 回复http://localhost:23060/WebService.svc。这可能是由于 到不使用 HTTP 协议的服务端点绑定。这 也可能是由于 HTTP 请求上下文被 服务器(可能是由于服务关闭)。查看服务器日志 更多细节。 Source=mscorlib StackTrace:服务器堆栈跟踪:在 System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest 请求, HttpAbortReason abortReason) 在 System.ServiceModel.Channels.HttpChannelFactory1.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度 暂停) 在 System.ServiceModel.Channels.RequestChannel.Request(消息消息, 时间跨度超时) 在 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息
消息,时间跨度超时) 在 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] 出局,TimeSpan 超时) 在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall、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\ServiceReference2\Reference.cs:第 567 行 在 WebLab6AlbumManagementSystemClient.ViewAllArtists.Page_Load(对象
发件人,EventArgs e) 在 c:\Users\Muhammad Rehan\Documents\Visual
工作室
2013\Projects\WebLab6AlbumManagementSystemClient\ViewAllArtists.aspx.cs:line 16 在 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(对象发送者, 事件参数 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.WebException H结果=-2146233079 Message=底层连接已关闭:接收时发生意外错误。 源=系统 堆栈跟踪: 在 System.Net.HttpWebRequest.GetResponse() 在 System.ServiceModel.Channels.HttpChannelFactory1.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度 超时)InnerException:System.IO.IOException HResult=-2146232800 消息=无法从传输连接读取数据:现有 连接被远程主机强行关闭。源=系统 StackTrace:在 System.Net.Sockets.NetworkStream.Read(Byte[] 缓冲区, System.Net.PooledStream.Read(Byte[] 处的 Int32 偏移量,Int32 大小) 缓冲区,Int32 偏移量,Int32 大小)在 System.Net.Connection.SyncRead(HttpWebRequest 请求,布尔值 userRetrievedStream, Boolean probeRead) InnerException: System.Net.Sockets.SocketException HResult=-2147467259 消息=一个 现有连接被远程主机强行关闭 源=系统错误代码=10054 NativeErrorCode=10054 堆栈跟踪:在 System.Net.Sockets.Socket.Receive(Byte[] 缓冲区,Int32 偏移量,Int32 大小,SocketFlags socketFlags)在 System.Net.Sockets.NetworkStream.Read(Byte[] 缓冲区,Int32 偏移量, Int32 大小)InnerException:

【问题讨论】:

  • ArtistDataContract 吗?
  • 这是 OperationContract。我的意思是 GetAllArtists() 是 OperationContract。而 Artist 顶部没有任何内容,而 Artist 是使用数据库中的 ado.net 实体数据模型从数据库自动生成的。

标签: c# asp.net wcf


【解决方案1】:

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

【讨论】:

    【解决方案2】:
     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();
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 1970-01-01
      相关资源
      最近更新 更多