2016-09-16 52 views
1

我在my集線器這種方法的一個實例過載:SignalR不調用服務器端方法,如果它有一個希望將類

Public Sub SaveFields(ByVal changeSignal As String) 
    Dim foo As Integer = 5 
End Sub 

如果我把它從客戶端這樣的:

testHub.server.saveFields("abc"); 

然後SaveFields被調用成功。但是,如果我有一個Class這樣的:

Public Class WOChangeSignal 
    Public WOID As Integer 
    Public FieldUpdates As Dictionary(Of String, String) 
End Class 

並添加過載SaveFields這樣的:

Public Sub SaveFields(ByVal changeSignal As WOChangeSignal) 
    Dim foo As Integer = 5 
End Sub 

Public Sub SaveFields(ByVal changeSignal As String) 
    Dim foo As Integer = 5 
End Sub 

然後我的

testHub.server.saveFields("abc"); 

通話將unsuccssful,就像我的電話

testHub.server.saveFields({ 
    WOID: 1234, 
    FieldUpdates: [ 
     {Key: 2, Value: 4}, 
     {Key: 3, Value: 5} 
    ] 
}); 

因爲這些嘗試都沒有實際調用服務器端方法。因此,我認爲問題是預期Class的實例的超載。所以,我的問題如下:爲什麼SignalR不會調用任何重載,如果我添加一個預期爲WOChangeSignal參數的超載?

+0

請參閱本關於重載Web方法: [http://stackoverflow.com/questions/3921142/net-overload-webmethods-possible](http://stackoverflow .com/questions/3921142/net-overload-webmethods-possible) – alwaysVBNET

+0

查看接受的答案:http://stackoverflow.com/questions/15686859/signalr-hub-overloads – supertopi

+0

@supertopi,我明白參數的數量必須不同,因此需要在代碼中進行更改,但是我想知道是什麼導致了我所描述的行爲,或者簡單地說:發生了什麼,導致系統無法執行任何重載? –

回答

1

事實證明,這是原因:

/// <summary> 
    /// Searches the specified <paramref name="hub">Hub</paramref> for the specified <paramref name="method"/>. 
    /// </summary> 
    /// <remarks> 
    /// In the case that there are multiple overloads of the specified <paramref name="method"/>, the <paramref name="parameters">parameter set</paramref> helps determine exactly which instance of the overload should be resolved. 
    /// If there are multiple overloads found with the same number of matching parameters, none of the methods will be returned because it is not possible to determine which overload of the method was intended to be resolved. 
    /// </remarks> 
    /// <param name="hub">Hub to search for the specified <paramref name="method"/> on.</param> 
    /// <param name="method">The method name to search for.</param> 
    /// <param name="descriptor">If successful, the <see cref="MethodDescriptor"/> that was resolved.</param> 
    /// <param name="parameters">The set of parameters that will be used to help locate a specific overload of the specified <paramref name="method"/>.</param> 
    /// <returns>True if the method matching the name/parameter set is found on the hub, otherwise false.</returns> 
    public bool TryGetMethod(HubDescriptor hub, string method, out MethodDescriptor descriptor, IList<IJsonValue> parameters) 
    { 
     string hubMethodKey = BuildHubExecutableMethodCacheKey(hub, method, parameters); 

     if (!_executableMethods.TryGetValue(hubMethodKey, out descriptor)) 
     { 
      IEnumerable<MethodDescriptor> overloads; 

      if (FetchMethodsFor(hub).TryGetValue(method, out overloads)) 
      { 
       var matches = overloads.Where(o => o.Matches(parameters)).ToList(); 

       // If only one match is found, that is the "executable" version, otherwise none of the methods can be returned because we don't know which one was actually being targeted 
       descriptor = matches.Count == 1 ? matches[0] : null; 
      } 
      else 
      { 
       descriptor = null; 
      } 

      // If an executable method was found, cache it for future lookups (NOTE: we don't cache null instances because it could be a surface area for DoS attack by supplying random method names to flood the cache) 
      if (descriptor != null) 
      { 
       _executableMethods.TryAdd(hubMethodKey, descriptor); 
      } 
     } 

     return descriptor != null; 
    } 

正如我們所看到的,如果不是正好有一個匹配,然後描述將是無效的。事實證明,這是由設計,並給出的理由如下:

如果只找到一個匹配,那是「可執行」的版本, 否則無的方法可以,因爲我們不退換」知道 其中一個竟是被瞄準

相關問題