MongoDB C#2.0.1驱动程序-连接突然关闭 [英] MongoDB c# 2.0.1 driver- connection suddenly closes

查看:250
本文介绍了MongoDB C#2.0.1驱动程序-连接突然关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是完整的程序代码: (除了Folderer类,只有ID和用户名-Json类型的类) 我没有db的任何用户-刚刚创建了简单集合

Here's the full program code: (Except for Follower class which is only id and username - Json-type class) I don't have any users for db - just created simple collection

class Program
{
    static void Main(string[] args)
    {
        DBClass db = new DBClass();
        db.check();
    }
}

class DBClass
{
    protected string connectionString ="mongodb://localhost:27017";
    protected IMongoClient _client;
    protected IMongoDatabase _database;
    protected IMongoCollection<Follower> collection;

    public DBClass()
    {
        _client = new MongoClient(connectionString);
        _database = _client.GetDatabase("FollowedUsers");
        collection = _database.GetCollection<Follower>("UserData");
    }

    public async void  check()
        {
        var filter = Builders<Follower>.Filter.Eq("id", "266663861");
        var result = await collection.Find(filter).CountAsync();
        Console.WriteLine(result);
        Console.ReadLine();
    }

}

}

这是我在mongod中看到的:

This is what I see in mongod:

2015-10-06T12:08:24.119+0300 I NETWORK  [initandlisten] connection
 accepted  from 127.0.0.1:50069 #2 (1 connection now open)
 2015-10-06T12:08:24.243+0300 I NETWORK  [conn2] end connection
 27.0.0.1:50069 (0 connections now open)

当我从VS运行它时,它只是终止而不会引发异常,这就是我在输出中看到的内容.程序[[11724] MongoTest.vshost.exe"已退出,代码为0(0x0). 当我调试它时,我看到它终止于这一行:

when I run it from VS it just terminates, throws no exceptions, this is what I see in the output The program '[11724] MongoTest.vshost.exe' has exited with code 0 (0x0). When I debug it I see that it terminates on this line:

var result = await collection.Find(filter).CountAsync();

我配置不正确吗?

推荐答案

您的问题与MongoDB无关. 问题原因是执行dbCheck时主线程关闭了mongoDb连接.

Your problem is not related to MongoDB. Reason of problem is main thread closes mongoDb connection while dbCheck is being executed.

您应该更改下一行:
1)

You should change next rows:
1)

公共异步任务 check()

public async Task check()

2)

db.check().等待()

这篇关于MongoDB C#2.0.1驱动程序-连接突然关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆