2016-11-23 83 views
3

我遵循this教程,並設法在10%到15%的時間內正常工作。服務總線WCF中繼「傳輸數據時發生錯誤。」

這是我的客戶,在異常發生的主要方法:

static void Main(string[] args) 
     { 
      Console.WriteLine("Client start..."); 
      ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Tcp; 

      string serviceNamespace = "nameSpaceFromAzure"; 
      //Console.Write("Your SAS Key: "); 
      string sasKey = "myKeyFromAzure=";//Console.ReadLine(); 

      Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, "EchoService"); 

      TransportClientEndpointBehavior sasCredential = new TransportClientEndpointBehavior(); 
      sasCredential.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", sasKey); 

      ChannelFactory<IEchoChannel> channelFactory = new ChannelFactory<IEchoChannel>("RelayEndpoint", new EndpointAddress(serviceUri)); 

      channelFactory.Endpoint.Behaviors.Add(sasCredential); 

      IEchoChannel channel = channelFactory.CreateChannel(); 
      channel.Open(); 

      Console.WriteLine("CLIENT-Enter text to echo (or [Enter] to exit):"); 
      string input = Console.ReadLine(); 
      while (input != String.Empty) 
      { 
       try 
       { 
        Console.WriteLine("Server echoed: {0}", channel.Echo(input)); 
       } 
       catch (Exception e) 
       { 
        Console.WriteLine("Error: " + e.Message); 
       } 
       input = Console.ReadLine(); 
      } 

      channel.Close(); 
      channelFactory.Close(); 

     } 

我知道的命名空間和密鑰輸入正確,因爲有時候這工作。然而,大多數時候,我得到這個異常

System.ServiceModel.CommunicationException was unhandled 
    HResult=-2146233087 
    Message=An error occurred while transmitting data. 
    Source=mscorlib 
    StackTrace: 
    Server stack trace: 
     at Microsoft.ServiceBus.ClientWebSocketConnection.EndRead() 
     at Microsoft.ServiceBus.SocketMessageHelper.ReadBytesAsyncResult.ReadComplete(Boolean calledSynchronously) 
    Exception rethrown at [0]: 
     at Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result) 
     at Microsoft.ServiceBus.SocketMessageHelper.ReceiveMessageAsyncResult.<>c__DisplayClass12_0.<GetAsyncSteps>b__3(ReceiveMessageAsyncResult thisPtr, IAsyncResult r) 
     at Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.StepCallback(IAsyncResult result) 
    Exception rethrown at [1]: 
     at Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result) 
     at Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.RunSynchronously() 
     at Microsoft.ServiceBus.SocketMessageHelper.ReceiveMessage(IConnection connection, TimeSpan timeout) 
     at Microsoft.ServiceBus.WebSocketConnectionInitiator.SendRelayedConnectAndReceiveResponse(ClientWebSocketConnection connection, TimeoutHelper timeoutHelper) 
     at Microsoft.ServiceBus.WebSocketConnectionInitiator.Connect(Uri uri, TimeSpan timeout) 
     at Microsoft.ServiceBus.ConnectivityModeConnectionInitiator.Connect(Uri uri, TimeSpan timeout) 
     at Microsoft.ServiceBus.Channels.BufferedConnectionInitiator.Connect(Uri uri, TimeSpan timeout) 
     at Microsoft.ServiceBus.Channels.ConnectionPoolHelper.EstablishConnection(TimeSpan timeout) 
     at Microsoft.ServiceBus.Channels.ClientFramingDuplexSessionChannel.OnOpen(TimeSpan timeout) 
     at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) 
     at Microsoft.ServiceBus.Channels.LayeredChannel`1.OnOpen(TimeSpan timeout) 
     at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) 
     at System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout) 
     at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) 
     at System.ServiceModel.Channels.CommunicationObject.Open() 
    Exception rethrown at [2]: 
     at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) 
     at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) 
     at System.ServiceModel.ICommunicationObject.Open() 
     at Microsoft.ServiceBus.Samples.Program.Main(String[] args) in C:\Users\xpto\Documents\Visual Studio 2015\Projects\EchoService\EchoClient\Program.cs:line 38 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly 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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 
     HResult=-2146232800 
     Message=More data was expected, but EOF was reached. 
     Source=Microsoft.ServiceBus 
     StackTrace: 
     Server stack trace: 
     Exception rethrown at [0]: 
      at Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result) 
      at Microsoft.ServiceBus.ServiceBusClientWebSocket.EndReceive(IAsyncResult result) 
      at Microsoft.ServiceBus.ClientWebSocketConnection.EndRead() 
     InnerException: 
      HResult=-2146233087 
      Message=ServiceBusClientWebSocket was expecting more bytes 
      InnerException: 

我在做什麼錯?

回答

1

原來的教程建議「多次啓動項目」,這是行爲的原因。當我啓動服務時,在獲取控制檯輸出啓動客戶端之後,所有事情都按預期工作。

相關問題