【发布时间】:2018-03-23 20:57:24
【问题描述】:
我有以下同步和异步方法
public static void Info(string message)
{
if (_logTypes.Contains(InfoLogType))
{
string applicationName, methodName, className;
ReflectClassAndMethod(out applicationName, out className, out methodName);
Write(applicationName, "Info", className, methodName, message);
}
}
public static async void InfoAsync(string message)
{
if (_logTypes.Contains(InfoLogType))
{
string applicationName, methodName, className;
ReflectClassAndMethod(out applicationName, out className, out methodName);
await WriteAsync(applicationName, "Info", className, methodName, message);
}
}
另外我的写法是这样的:
private static void Write(string applicationName, string logType, string className, string methodName, string message)
{
// construct me _event object
var collection = Database.GetCollection<Event>(typeof(Event).Name);
collection.InsertOne(_event);
}
private static Task WriteAsync(string applicationName, string logType, string className, string methodName, string message)
{
// construct my _event object
var collection = Database.GetCollection<Event>(typeof(Event).Name);
await collection.InsertOneAsync(_event);
}
使用测试控制台应用程序,我将这些同步和异步方法称为如下:
static void Main(string[] args)
{
Log.Info("Synchronous write");
Log.InfoAsync(DateTime.Now.ToString(CultureInfo.InvariantCulture));
}
这会将两个文档插入到我的 Mongo 集合中。但如果我将同步调用注释掉,则不会插入任何内容。
static void Main(string[] args)
{
//Log.Info("Synchronous write");
Log.InfoAsync(DateTime.Now.ToString(CultureInfo.InvariantCulture));
}
这不会插入任何内容。这是为什么呢?
【问题讨论】:
-
同意。我发现这个问题被正确地提出了,并且有一个实际的问题和可能的解决方案。 meta.stackexchange.com/questions/135/…
-
@smoksnes 但这些问题可以帮助声誉低于 2k 的用户。不适合像我这样的人... :(
标签: c# mongodb c#-4.0 asynchronous