I have this error: "Cannot implicitly convert type 'bool' to 'system.threading.tasks.task bool'" in my service implementation code. Could you correct my code please.
我有这样的错误:“不能在我的服务实现代码中隐式地将类型'bool'转换为'system.threading.tasks.task bool'”。你能纠正我的代码吗?
public Task<bool> login(string usn, string pwd)
{
DataClasses1DataContext auth = new DataClasses1DataContext();
var message = from p in auth.Users
where p.usrName == usn && p.usrPass == pwd
select p;
if (message.Count() > 0)
{
return true;
}
else
{
return false;
}
}
2 个解决方案
#1
12
You need to be specific whether you want this operation happen asynchronously or not.
您需要具体说明是否要将此操作异步发生。
As an example for Async Operation
:
作为异步操作的示例:
public async Task<bool> login(string usn, string pwd)
{
DataClasses1DataContext auth = new DataClasses1DataContext();
var message = await (from p in auth.Users
where p.usrName == usn && p.usrPass == pwd
select p);
if (message.Count() > 0)
{
return true;
}
else
{
return false;
}
}
If you don't need it to be an Async operation, try this:
如果您不需要它作为异步操作,请尝试以下操作:
public bool login(string usn, string pwd)
{
DataClasses1DataContext auth = new DataClasses1DataContext();
var message = from p in auth.Users
where p.usrName == usn && p.usrPass == pwd
select p;
if (message.Count() > 0)
{
return true;
}
else
{
return false;
}
}
Note: async
and await
are compatible with .net 4.5 and C# 5.0 and more
注意:async和await与.net 4.5和C#5.0及更多版本兼容
#2
4
If you add Task.FromResult
, you can fake it into compiling and working even though your method is not async
. I had to do this when hooking up Identity, which is all async
, to a legacy back end.
如果添加Task.FromResult,即使您的方法不是异步,也可以将其伪装成编译和工作。我必须在将Identity(异步同步)连接到传统后端时执行此操作。
Example:
例:
public override Task<bool> IsEmailConfirmedAsync(string userId)
{
var profile = UserProfileType.FetchUserProfile(AtlasBusinessObject.ClientId.ToString(), decimal.Parse(userId));
Task.FromResult(profile.EmailAddress.NullIfEmpty() != null);
}
#1
12
You need to be specific whether you want this operation happen asynchronously or not.
您需要具体说明是否要将此操作异步发生。
As an example for Async Operation
:
作为异步操作的示例:
public async Task<bool> login(string usn, string pwd)
{
DataClasses1DataContext auth = new DataClasses1DataContext();
var message = await (from p in auth.Users
where p.usrName == usn && p.usrPass == pwd
select p);
if (message.Count() > 0)
{
return true;
}
else
{
return false;
}
}
If you don't need it to be an Async operation, try this:
如果您不需要它作为异步操作,请尝试以下操作:
public bool login(string usn, string pwd)
{
DataClasses1DataContext auth = new DataClasses1DataContext();
var message = from p in auth.Users
where p.usrName == usn && p.usrPass == pwd
select p;
if (message.Count() > 0)
{
return true;
}
else
{
return false;
}
}
Note: async
and await
are compatible with .net 4.5 and C# 5.0 and more
注意:async和await与.net 4.5和C#5.0及更多版本兼容
#2
4
If you add Task.FromResult
, you can fake it into compiling and working even though your method is not async
. I had to do this when hooking up Identity, which is all async
, to a legacy back end.
如果添加Task.FromResult,即使您的方法不是异步,也可以将其伪装成编译和工作。我必须在将Identity(异步同步)连接到传统后端时执行此操作。
Example:
例:
public override Task<bool> IsEmailConfirmedAsync(string userId)
{
var profile = UserProfileType.FetchUserProfile(AtlasBusinessObject.ClientId.ToString(), decimal.Parse(userId));
Task.FromResult(profile.EmailAddress.NullIfEmpty() != null);
}