2010-08-21 66 views
0

我創建了一個簡單的WCF演示: 服務器端:有關WCF奇怪的錯誤 - System.ServiceModel.CommunicationException了未處理

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

然後,運行ServerSide,當我嘗試運行CLientSide時,出現以下錯誤消息: 發生錯誤接收對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: 
+0

你也可以發佈你的服務合同的細節嗎? 另外,你有沒有嘗試過而不添加一個顯式的端點,並且看看它是否與默認端點一起工作 - 只是爲了試試看看錯誤可能在哪裏? – InSane 2010-08-21 05:37:17

回答

2

是您的機器稱爲locahost?通常情況下,這些機器被稱爲LOCA主機 - 那麼試試這個網址爲您服務(包括服務器和客戶端):

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

隨着這一變化,我可以沒有任何重新創建您的樣品無論如何。

+0

我真的很感激你的幫助,實際上,這應該是我的英語課。我感到慚愧, :( – Ferde 2010-08-22 02:41:58