简体   繁体   English

无法隐式转换类型&#39;System.Threading.Tasks.Task <object> 在SignalR中从&#39;到&#39;string&#39;

[英]Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string' in SignalR

I am new in SignalR . 我是SignalR的新手。

my project is that bringing up sql change on signalR and sql dependency . 我的项目是提出对signalR和sql依赖项进行sql更改。

This is sample code that used C# corner 这是使用C#角落的示例代码

Every thing is ok but i get exception through this code 一切都很好,但是我通过这段代码得到了异常

在此处输入图片说明

         using (var connection = new SqlConnection("Server=.;Database=fidilio;Trusted_Connection=True;"))
        {
            const string query = "SELECT Count(*) FROM [dbo].[MemberComment]";

            connection.Open();
            using (var command = new SqlCommand(query, connection))
            {                  
                command.Notification = null;                
                var dt = new DataTable();
                var dependency = new SqlDependency(command);
                dependency.OnChange += dependency_OnChange;
                if (connection.State == ConnectionState.Closed)
                    connection.Open();                
                var reader = command.ExecuteScalar();

                commentCount = Int16.Parse((reader.ToString()));
            }  
        }
        var context = GlobalHost.ConnectionManager.GetHubContext<NotficationHub>();

        return context.Clients.All.RecevieNotification(commentCount);

Is there any idea ? 有什么主意吗?

Unfortunately, the code you posted is incomplete and far from clear, never mind useful. 不幸的是,您发布的代码不完整,而且还很不清楚,没关系。 Note, for example, that the code posted as actual code does not actually match the code you posted as a bitmap. 请注意,例如,作为实际代码发布的代码与您作为位图发布的代码实际上不匹配。

(Please also keep in mind that bitmaps, especially those for which the description still reads "enter image description here", cannot be searched in any meaningful way by tools like the web-site's own search feature or search engines like Bing and Google). (请注意,位图,尤其是那些描述仍为“在此处输入图像描述”的位图,无法通过网站本身的搜索功能或Bing和Google等搜索引擎以任何有意义的方式进行搜索)。

However, the specific exception you got is very typical of misuse of an async method. 但是,您得到的特定异常是滥用async方法的非常典型的现象。 It seems to me that you could get the code to work by changing the return statement to look like this: 在我看来,您可以通过将return语句更改为如下所示来使代码正常工作:

return (string)context.Clients.All.RecevieNotification(commentCount).Result;

(That's the code from the text version of your question...copy/paste is also easier than trying to retype something from a bitmap. I presume you can adapt the above change to the code in the bitmap if needed). (这是问题文本版本的代码...复制/粘贴也比尝试从位图中重新键入内容容易得多。我想您可以根据需要将上述更改调整为位图中的代码)。

Note that if that actually does fix the problem, then what you really should do is change the method containing that return statement to be async Task<string> , and change the return statement to this: 请注意,如果这确实可以解决问题,那么您真正应该做的是将包含该return语句的方法更改为async Task<string> ,并将return语句更改为:

return (string)(await context.Clients.All.RecevieNotification(commentCount));

But doing so will simply push the need to use await back up to the calling method, and the caller of that method, and so on, until you get to whatever top-level method in the thread or UI event started the whole process. 但是这样做只会将使用await的需求推回到调用方法以及该方法的调用者,依此类推,直到您到达线程或UI事件中的任何顶级方法都启动了整个过程。

That is really the right thing to do, but there's not enough context here to explain how to do that in any specific way. 这真的做正确的事情,但这里有没有足够的上下文来解释如何做,在任何特定的方式。 If you do decide to fix your code that way, and need help figuring out how to correctly make your code async -aware, please post a new question, being sure to include a good, minimal , complete code example that clearly and reliably illustrates that question. 如果您确实决定以这种方式修复代码,并且需要帮助弄清楚如何正确地使代码async感知,请发布一个新问题,并确保包括一个良好, 最少完整的代码示例 ,以清楚,可靠地说明该问题。题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型&#39;void&#39;隐式转换为&#39;System.Threading.Tasks.Task&#39; - Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task' 无法隐式转换类型&#39;System.Threading.Tasks.Task - Cannot implicitly convert type 'System.Threading.Tasks.Task 无法将类型&#39;bool&#39;隐式转换为&#39;System.Threading.Tasks.Task&#39; - Cannot implicitly convert type 'bool' to 'System.Threading.Tasks.Task' 无法隐式转换类型&#39;System.Threading.Tasks.Task <String> “串” - Cannot implicitly convert type 'System.Threading.Tasks.Task<String> to 'string' 错误 CS0029:无法将类型“字符串”隐式转换为“System.Threading.Tasks.Task”<string> '</string> - Error CS0029: Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>' 无法将类型“字符串”隐式转换为“System.Threading.Tasks.Task”<string> &#39; - Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>' 无法将类型“字符串”隐式转换为“System.Threading.Tasks.Task”<string> ' 在 c#</string> - Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>' in c# 无法将类型&#39;System.Collections.Generic.List &lt;&gt;&#39;隐式转换为&#39;System.Threading.Tasks.Task &lt;&gt;&gt; - Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Threading.Tasks.Task<>> 无法将类型&#39;bool&#39;隐式转换为&#39;system.threading.tasks.task bool&#39; - Cannot implicitly convert type 'bool' to 'system.threading.tasks.task bool' 无法将类型“System.Threading.Tasks.Task”隐式转换为“Windows.Foundation.IAsyncAction”。 存在显式转换 - Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'Windows.Foundation.IAsyncAction'. An explicit conversion exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM