有人知道这样做的目的是什么?
private async Task<bool> StoreAsync(TriviaAnswer answer) { ... }
[ResponseType(typeof(TriviaAnswer))]
public async Task<IHttpActionResult> Post(TriviaAnswer answer)
{
var isCorrect = await StoreAsync(answer);
return Ok<bool>(isCorrect);
}
Run Code Online (Sandbox Code Playgroud)
通过检查,它告诉它异步运行私有方法,但同步等待它结束.我的问题是,这有什么意义吗?或者这只是一种奇特而无用的技巧?我在研究Web API/MVC/SPA的一些代码时遇到了这个问题.
无论如何,任何见解都会有用.
我正在尝试以异步方式运行我的控制器操作。如何使用异步任务?或者如何以异步方式运行
// Db context
public class DeptContext : DbContext
{
public LagerContext(DbContextOptions<LagerContext> options)
: base(options)
{
Database.Migrate();
}
public DbSet<Department> Departments { get; set; }
public DbSet<Product> Products { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
// 这是我的接口 IDepRepository
Task<Department> GetDepartmentWithOrWithoutProducts(int deptId, bool includeProducts);
Run Code Online (Sandbox Code Playgroud)
// 还有我的 Repository 类 DepRepository
public class DepRepository : IDepRepository
{
private DeptContext db;
public DepRepository(DeptContext context)
{
db = context;
}
// I'am geting Department name with products or Without products
public async Task<Department> GetDepartmentWithOrWithoutProducts(int deptId, bool includeProducts) …Run Code Online (Sandbox Code Playgroud) 我试图找到一种方法,如何将curl命令转移到C#。
我需要从api获取令牌并将其保存到文件C:\ ... \ x.json \
然后,我想将令牌声明为变量,并将其用于另一个curl POST请求中以获取数据以与它们一起工作。
卷曲:
curl -X POST "https://example.com"
-H "accept: application/json"
-H "Content-Type: application/json"
-d {"username":"username","password":"password"}
Run Code Online (Sandbox Code Playgroud)
我目前的尝试:
static void Main(string[] args)
{
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://example.com"))
{
request.Headers.TryAddWithoutValidation("Accept", "application/json");
request.Content = new StringContent("{\"username\":\"username\",\"password\":\"password\"}", Encoding.UTF8, "application/json");
var response = await httpClient.SendAsync(request);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了几种方法,但是都没有用。这是来自curl.olsh.me,但我也收到一些等待中的错误,我不知道该怎么办。(我是C#的新手):
“ await”运算符只能在异步方法中使用。考虑使用“异步”修饰符标记该方法,并将其返回类型更改为“任务”。
curl -v输出:
我在很多地方都读过 ConfigureAwait(包括 SO 问题),以下是我的结论:
.ConfigureAwait(true). 否则,由于另一个线程访问 UI 元素,将发生 InvalidOperationException。我的问题是:
在WPF应用程序中使用Entity Framework,并想知道我是否要将数据加载错误.我在后面的代码中添加了以下内容:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var invoices = db.Invoices.AsNoTracking().ToList();
listbox1.ItemsSource = invoices;
}
Run Code Online (Sandbox Code Playgroud)
加载好,没有问题,但是在加载数据之前窗口不会显示,这很乏味.wWindow_load是加载它的最佳方法/时间吗?
我正在尝试围绕我的web api调用实现一些日志记录.没有什么太花哨,但我仍然在学习async/await,并希望在不中断性能的情况下进行日志记录.
这是我想要实现的一个例子,但不完全确定如何做到这一点.
public class Logging : ActionFilterAttribute
{
public override async Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
{
// DoLoggingHere(); ????
base.OnActionExecutingAsync(actionContext, cancellationToken);
// OrDoLoggingHere(); ????
// Also, what does the return look like if any?
}
public override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
{
// This method should/could complete before the logging completes.
base.OnActionExecutedAsync(actionExecutedContext, cancellationToken);
// Might log here too.
}
}
Run Code Online (Sandbox Code Playgroud)
基本上我不希望我的任何日志记录妨碍HTTP调用.它应该是透明的.
寻求任何帮助!
我的asp.net应用程序有时在实时服务器上关闭.所有用户都面临黄色错误屏幕.当我深入研究问题时,我找到了痕迹.
抛出了类型'System.Web.HttpUnhandledException'的异常.在System.Web.UI的System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)的System.Web.UI.Page.HandleError(Exception e)处的=====> stact trace ====>位于System.Web.UI.Page.ProcessRequest()的System.Web.UI.Page.ProcessRequest()的.Page.ProcessRequest(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)位于c的ASP.emr_patient_callbacks_patientappointments_aspx.ProcessRequest(HttpContext context)中的System.Web.UI.Page.ProcessRequest(HttpContext context)\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\05f0ecab\db8ea090\App_Web_wgoawcvo.0.cs:System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep中的第0行.在System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&completedSynchronously)的System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep步骤)执行()
此异常发生在随机而非特定位置或特定点击上.但是当我在IIS上重新启动我的应用程序时,应用程序正常工 但又出现了几个小时相同的问题.
假设我有一个void返回类型的方法,我需要await在此方法中进行操作.
public void someOperation()
{
//dostuff
var res = await someOtherOperation();
//do some other stuff that needs res.
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译此代码时,我得到了错误说someOperation必须声明async.我不明白为什么.我理解为什么如果方法有一个返回值,但是当它的时候没有void.甚至在等待操作对方法的返回值没有影响的情况下.
这已经被问到这个问题的一部分,但我找不到我想要的答案.这家伙只是提到:
async关键字启用await关键字.因此,任何使用await的方法都必须标记为异步.
async关键字启用await关键字是什么意思.我试图通过创建一个简单的例子来理解async/await的基础知识.我使用Sqlite与Async连接,我有一个这样的类:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在让我们说我要保存User到我的UserTable,当保存完成后,我希望检索它.
public async ? SaveToDb()
{
_conn.CreateTableAsync<User>();
await _conn.InsertAsync(new User(){Id=1, Name = "Bill"});
//Code that waits for the save to complete and then retrieves the user
}
Run Code Online (Sandbox Code Playgroud)
我怀疑我需要某个任务,但我不完全确定如何做到这一点.谢谢
我听说等待是异步操作.但是,由于这是一个重要的概念,为什么我在MSDN上找不到精确的定义?
我的问题不是如何编写async/await.我的问题是要了解这个概念.MSDN有这个概念,async/await但没有awaitable.
那么什么是等待的?如果是手术,包含哪些内容?
c# ×9
async-await ×4
asynchronous ×3
.net ×2
asp.net ×1
asp.net-core ×1
asp.net-mvc ×1
curl ×1
iis ×1
wpf ×1