Await无法使用Mongo C#驱动程序在Mongo数据库中插入数据?

时间:2015-06-30 20:02:32

标签: c# mongodb asynchronous ibm-cloud mongodb-.net-driver

Await无法使用Mongo C#驱动程序在Mongo数据库中插入数据? 我的代码中的“等待”无效。使用IBM Blue mix平台进行编码,因此无法看到错误\无法调试。 以下是我的申请代码:

using System;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Builder;
using Microsoft.Framework.DependencyInjection;
using Microsoft.AspNet.StaticFiles;

using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System.Threading.Tasks;


public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.UseServices(services =>
        {
            services.AddMvc();
        });

        app.UseFileServer(new FileServerOptions()
        {
            EnableDirectoryBrowsing = false,
        });

        app.Use(async (context, next) =>
        {
            Console.WriteLine(context.Request.Path);
            try
            {
                await next();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        });

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "Default",
                template: "{controller=Home}/{action=Login}/{id?}");
        });
    }
}
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Login()
    {
        return View();
    }

   public ActionResult BillingSystem()
    {
        return View();
    }

    public async Task<string> show()
    {
            var client = new MongoClient("mongodb://IbmCloud_rpinmmse_1rmgt667_vprhgdu9:x0s-3HQYWB0ek6_NJsH2c7iP3jfyHGEQ@ds041180.mongolab.com:41180/IbmCloud_rpinmmse_1rmgt667");
            var db = client.GetDatabase("IbmCloud_rpinmmse_1rmgt667");
            var Collection = db.GetCollection<Login>("userdata");
            var save = new Login { username = "BaijuGDP",pwd ="baiju@GD"};
            await collection.InsertOneAsync(save);




            return "GD";
    }


    public int chkuser(Login data)
    {
        if(data.username == "Admin" && data.pwd == "Admin@123")
        {
            return 1;
        }
        return 0;

    }

}
 public class Login
 {
    public ObjectId id { get; set; }
    public string username { get; set; }
    public string pwd { get; set; }
 }

当我尝试部署上面的代码时,它根本没有部署在IBM蓝色混合云中。 如果我删除此行“await collection.InsertOneAsync(save);”它会部署,但如何保存数据? 这里有什么问题? 我需要在这里添加任何参考吗? 下面是我的依赖文件,

{
    "webroot": "wwwroot",
    "dependencies": {
        "Microsoft.AspNet.StaticFiles": "1.0.0-beta3",
        "Microsoft.AspNet.Mvc": "6.0.0-beta3",
        "Kestrel": "1.0.0-beta3",
        "MongoDB.Driver": "2.0.1",
        "System.Threading":"4.0.10-beta-22816"

    },
    "commands": {
        "web-kestrel": "Microsoft.AspNet.Hosting --server Kestrel"
    }
}

0 个答案:

没有答案