SignalR에서 'System.Threading.Tasks.Task <object>'형식을 암시 적으로 'string'으로 변환 할 수 없습니다.

살 라르

저는 SignalR을 처음 사용합니다.

내 프로젝트는 signalR 및 sql 종속성에 대한 SQL 변경을 가져 오는 것입니다.

C # 코너 를 사용한 샘플 코드입니다.

모든 것이 괜찮지 만이 코드를 통해 예외가 발생합니다.

여기에 이미지 설명 입력

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

아이디어가 있습니까?

피터 두니 호

불행히도 귀하가 게시 한 코드는 불완전하고 명확하지 않으며 유용하지 않습니다. 예를 들어 실제 코드로 게시 된 코드는 비트 맵으로 게시 한 코드와 실제로 일치하지 않습니다.

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

이다 정말 옳은 일하지만, 충분한 문맥은 특정 방식으로 그 작업을 수행하는 방법을 설명하기 위해 여기가 아니다. 그런 식으로 코드를 수정하기로 결정하고 코드 async인식 을 올바르게 만드는 방법을 찾는 데 도움이 필요하면 해당 질문을 명확하고 안정적으로 설명 하는 훌륭하고 최소한완전한 코드 예제 를 포함 하여 새 질문을 게시하십시오. .

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관