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

salar

I am new in SignalR .

my project is that bringing up sql change on signalR and sql dependency .

This is sample code that used C# corner

Every thing is ok but i get exception through this code

enter image description here

         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 ?

Peter Duniho

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).

However, the specific exception you got is very typical of misuse of an async method. It seems to me that you could get the code to work by changing the return statement to look like this:

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 (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.

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SignalR error Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string'

From Dev

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

From Dev

Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task'

From Dev

Cannot implicitly convert type 'bool' to 'System.Threading.Tasks.Task'

From Dev

Cannot implicitly convert type 'System.Threading.Tasks.Task<String> to 'string'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Threading.Tasks.Task<>>

From Dev

Cannot implicitly convert type 'bool' to 'system.threading.tasks.task bool'

From Dev

Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Web.Mvc.ActionResult>' to 'System.Web.Mvc.ActionResult'

From Dev

Cannot convert from 'System.Threading.Tasks.Task' to 'System.Collections.Generic.Dictionary<string,string>'

From Dev

Cannot convert from 'System.Threading.Tasks.Task' to 'System.Collections.Generic.Dictionary<string,string>'

From Dev

cannot implicitly convert System.Type to object

From Java

Cannot convert type 'System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.IActionResult>' to 'Microsoft.AspNetCore.Mvc.OkObjectResult'

From Dev

Cannot implicitly convert type ('string', 'string') to System.Net.ICredentialsByHost

From Dev

Cannot convert from 'void' to System.Threading.Tasks.Task<System.Action>

From Dev

Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

From Dev

Cannot implicitly convert to type string to System.Net.Mail.MailAddress

From Dev

Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

From Dev

Error Cannot implicitly convert type 'string' to 'System.DateTime' on return

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable<char[]>' to 'string[]'

From Dev

Cannot implicitly convert type string to System.Drawing.Color

From Dev

Cannot implicitly convert type 'string' to 'String'

From Dev

Cannot implicitly convert 'string' to 'System.TypeCode'

From Dev

Cannot implicitly convert type long to a Object Model

From Dev

cannot implicitly convert type void to object

From Dev

"Cannot implicitly convert type 'bool[]' to 'object[]'"

From Dev

Cannot implicitly convert type System.EventHandler to System.EventHandler<object> error

From Dev

Cannot implicitly convert type 'string' to 'decimal'

From Dev

cannot implicitly convert type string to char

Related Related

  1. 1

    SignalR error Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string'

  2. 2

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

  3. 3

    Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task'

  4. 4

    Cannot implicitly convert type 'bool' to 'System.Threading.Tasks.Task'

  5. 5

    Cannot implicitly convert type 'System.Threading.Tasks.Task<String> to 'string'

  6. 6

    Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Threading.Tasks.Task<>>

  7. 7

    Cannot implicitly convert type 'bool' to 'system.threading.tasks.task bool'

  8. 8

    Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Web.Mvc.ActionResult>' to 'System.Web.Mvc.ActionResult'

  9. 9

    Cannot convert from 'System.Threading.Tasks.Task' to 'System.Collections.Generic.Dictionary<string,string>'

  10. 10

    Cannot convert from 'System.Threading.Tasks.Task' to 'System.Collections.Generic.Dictionary<string,string>'

  11. 11

    cannot implicitly convert System.Type to object

  12. 12

    Cannot convert type 'System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.IActionResult>' to 'Microsoft.AspNetCore.Mvc.OkObjectResult'

  13. 13

    Cannot implicitly convert type ('string', 'string') to System.Net.ICredentialsByHost

  14. 14

    Cannot convert from 'void' to System.Threading.Tasks.Task<System.Action>

  15. 15

    Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

  16. 16

    Cannot implicitly convert to type string to System.Net.Mail.MailAddress

  17. 17

    Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

  18. 18

    Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

  19. 19

    Error Cannot implicitly convert type 'string' to 'System.DateTime' on return

  20. 20

    Cannot implicitly convert type 'System.Linq.IQueryable<char[]>' to 'string[]'

  21. 21

    Cannot implicitly convert type string to System.Drawing.Color

  22. 22

    Cannot implicitly convert type 'string' to 'String'

  23. 23

    Cannot implicitly convert 'string' to 'System.TypeCode'

  24. 24

    Cannot implicitly convert type long to a Object Model

  25. 25

    cannot implicitly convert type void to object

  26. 26

    "Cannot implicitly convert type 'bool[]' to 'object[]'"

  27. 27

    Cannot implicitly convert type System.EventHandler to System.EventHandler<object> error

  28. 28

    Cannot implicitly convert type 'string' to 'decimal'

  29. 29

    cannot implicitly convert type string to char

HotTag

Archive