2

我正在構建一個可與SignalR HUB進行交互的Chrome擴展。從Chrome擴展連接到SignalR - error:方法無法解析

集線器工作正常,我的本地文件是用代理連接創建的。但是,我不能,因爲它不是在同一個項目,所以我不得不把它定義自己使用Chrome擴展程序的代理連接:

var connection = $.hubConnection('http://localhost:61xxx/signalr/hubs', {useDefaultPath: false}); 
var roomIndexHubProxy = connection.createHubProxy('roomIndexHub'); 

這工作得很好,如果我使用斷點的服務器OnConnected上方法它會觸發,我也看到說連接的日誌已經建立。但是,當我嘗試運行我自己的功能,我得到這個錯誤(在客戶端):

SignalR: 'test' method could not be resolved. No method found with that name. at Microsoft.AspNet.SignalR.Hubs.NullMethodDescriptor.b__0(IHub emptyHub, Object[] emptyParameters) at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.Incoming(IHubIncomingInvokerContext context) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1.ConfiguredTaskAwaiter.GetResult() at Microsoft.AspNet.SignalR.TaskAwaiterHelper.PreserveCultureAwaiter 1.GetResult() at Microsoft.AspNet.SignalR.Hubs.HubPipelineModule.<>c__DisplayClass1.<b__0>d__3.MoveNext().

SignalR: roomindexhub.test failed to execute. Error: 'test' method could not be resolved. No method found with that name.

這是我的客戶端代碼:

var connection = $.hubConnection('http://localhost:61xxx/signalr/hubs', {useDefaultPath: false}); 
var roomIndexHubProxy = connection.createHubProxy('roomIndexHub'); 

connection.logging = true; 
connection.start().done(function() { 
    // Wire up Send button to call RoomIndexHubProxy on the server. 
    console.log('Hub has started'); 
    $("#signalr").click(function(){ 
     roomIndexHubProxy.invoke('test'); 
    }); 

}); 

connection.error(function (error) { 
    console.log('SignalR error: ' + error) 
}); 

服務器端代碼(雖然很簡單) :

public class RoomIndexHub : Hub 
    { 
     public RoomIndexHub(){} 

     public override Task OnConnected() 
     { 
      return base.OnConnected(); // this works! 
     } 

     internal void test() 
     { 
      int i = 0; // this doesn't work 
     } 
    } 
} 

正如我在寫那些我看到這個'在Microsoft.AspNet.SignalR.Hubs.NullMethodDescriptor.b__0(IHUB

但是我不知道爲什麼,因爲我做了定義這種情況正在發生:

var roomIndexHubProxy = connection.createHubProxy('roomIndexHub');

我將非常感謝您的幫助!

+1

'內部類型或成員只能在同一個程序集中的文件中訪問 - 換言之,外部不可訪問。雖然我不知道任何C#。 – wOxxOm

+0

哦,嘆了口氣,這很容易..有時你需要知道在哪裏看我猜... 我很專注於客戶端這個錯誤,我錯過了這個簡單的事情。 非常感謝! – LiranBo

回答

1

documentationinternal關鍵字說:

Internal types or members are accessible only within files in the same assembly

換句話說,外部無法進入,所以只需刪除internal