关于 WCF 的奇怪错误 - System.ServiceModel.CommunicationException 未处理

发布于 2024-09-15 04:57:35 字数 5776 浏览 1 评论 0原文

我创建了一个简单的 WCF 演示: 服务器端:

namespace ServerSide
{
    class Program
    {
        static void Main(string[] args)
        {
            System.ServiceModel.ServiceHost host = 
                new System.ServiceModel.ServiceHost(typeof(HelloIndigo.HelloIndigoService), new Uri("http://locahost:8000/HelloIndigo"));
            host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService), new System.ServiceModel.BasicHttpBinding(), "HelloIndigoService");
            host.Open();

            Console.Write("Terminate Server");
            Console.ReadLine();
        }
    }
}

客户端:

    namespace ClientSide
    {
        class Program
        {
            static void Main(string[] args)
            {
                System.ServiceModel.EndpointAddress ep =
                    new System.ServiceModel.EndpointAddress("http://locahost:8000/HelloIndigo/HelloIndigoService");
                IHelloIndigoService proxy = 
                    System.ServiceModel.ChannelFactory<IHelloIndigoService>.CreateChannel(new System.ServiceModel.BasicHttpBinding(), ep);
                string s = proxy.HelloIndigo();
                Console.WriteLine(s);
                Console.ReadLine();
            }
        }
     }

然后,运行服务器端,当我尝试运行客户端时,我收到以下错误消息: 接收 http://locahost:8000/HelloIndigo/HelloIndigoService 的 HTTP 响应时出错。这可能是由于服务端点绑定不使用 HTTP 协议。这也可能是由于服务器中止 HTTP 请求上下文(可能是由于服务关闭)。有关更多详细信息,请参阅服务器日志。

以及错误详细信息:

    System.ServiceModel.CommunicationException was unhandled
      Message="An error occurred while receiving the HTTP response to http://locahost:8000/HelloIndigo/HelloIndigoService. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details."
      Source="mscorlib"
      StackTrace:
        Server stack trace: 
           at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
           at System.ServiceModel.Channels.HttpChannelFactory.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.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
           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 ClientSide.IHelloIndigoService.HelloIndigo()
           at ClientSide.Program.Main(String[] args) in E:\My_Test\Test_for_VS2008\WCF_TestLessonOne_Client\ClientSide\Program.cs:line 16
           at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: System.Net.WebException
           Message="The underlying connection was closed: An unexpected error occurred on a receive."
           Source="System"
           StackTrace:
                at System.Net.HttpWebRequest.GetResponse()
                at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
           InnerException: System.IO.IOException
                Message="Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."
                Source="System"
                StackTrace:
                     at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
                     at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
                     at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
                InnerException: System.Net.Sockets.SocketException
                     Message="An existing connection was forcibly closed by the remote host"
                     Source="System"
                     ErrorCode=10054
                     NativeErrorCode=10054
                     StackTrace:
                          at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
                      at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
                 InnerException: 

I created a simple WCF demo:
Server Side:

namespace ServerSide
{
    class Program
    {
        static void Main(string[] args)
        {
            System.ServiceModel.ServiceHost host = 
                new System.ServiceModel.ServiceHost(typeof(HelloIndigo.HelloIndigoService), new Uri("http://locahost:8000/HelloIndigo"));
            host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService), new System.ServiceModel.BasicHttpBinding(), "HelloIndigoService");
            host.Open();

            Console.Write("Terminate Server");
            Console.ReadLine();
        }
    }
}

Client Side:

    namespace ClientSide
    {
        class Program
        {
            static void Main(string[] args)
            {
                System.ServiceModel.EndpointAddress ep =
                    new System.ServiceModel.EndpointAddress("http://locahost:8000/HelloIndigo/HelloIndigoService");
                IHelloIndigoService proxy = 
                    System.ServiceModel.ChannelFactory<IHelloIndigoService>.CreateChannel(new System.ServiceModel.BasicHttpBinding(), ep);
                string s = proxy.HelloIndigo();
                Console.WriteLine(s);
                Console.ReadLine();
            }
        }
     }

Then, run ServerSide, when I tried to run the CLientSide, I got following error msg:
An error occurred while receiving the HTTP response to http://locahost:8000/HelloIndigo/HelloIndigoService. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

And the error details:

    System.ServiceModel.CommunicationException was unhandled
      Message="An error occurred while receiving the HTTP response to http://locahost:8000/HelloIndigo/HelloIndigoService. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details."
      Source="mscorlib"
      StackTrace:
        Server stack trace: 
           at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
           at System.ServiceModel.Channels.HttpChannelFactory.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.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
           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 ClientSide.IHelloIndigoService.HelloIndigo()
           at ClientSide.Program.Main(String[] args) in E:\My_Test\Test_for_VS2008\WCF_TestLessonOne_Client\ClientSide\Program.cs:line 16
           at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: System.Net.WebException
           Message="The underlying connection was closed: An unexpected error occurred on a receive."
           Source="System"
           StackTrace:
                at System.Net.HttpWebRequest.GetResponse()
                at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
           InnerException: System.IO.IOException
                Message="Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."
                Source="System"
                StackTrace:
                     at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
                     at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
                     at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
                InnerException: System.Net.Sockets.SocketException
                     Message="An existing connection was forcibly closed by the remote host"
                     Source="System"
                     ErrorCode=10054
                     NativeErrorCode=10054
                     StackTrace:
                          at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
                      at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
                 InnerException: 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向日葵 2024-09-22 04:57:35

您的机器名为 locahost 吗?通常,这些计算机称为本地主机 - 因此请尝试使用此 Url 来提供您的服务(在服务器端和客户端):

new Uri("http://localhost:8000/HelloIndigo")

通过此更改,我能够重新创建您的示例,而不会出现任何问题。

Is your machine called locahost ?? Typically, those machines are called localhost - so try this Url for your service (both on server and client side):

new Uri("http://localhost:8000/HelloIndigo")

With this change, I was able to re-create your sample without any issues whatsoever.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文