如何使用Raven.Client.Lightweight删除RavenDB数据库

时间:2012-09-19 03:42:50

标签: ravendb

我已经实现了我认为删除数据库(raven作为服务运行)的正确方法,基于1.2 Raven Studio的方式并将代码移植到Windows控制台。

static class Program
{    
    static void Main(string[] args)
    {
        try
        {
            using (var store = new DocumentStore { ConnectionStringName = "RavenDB" }.Initialize())
            {

                var metadata = new RavenJObject();

                var factory = store.JsonRequestFactory;

                const string url = "http://localhost:8080/admin/databases/raven-products?hard-delete=true";

                var credentials = CredentialCache.DefaultCredentials;

                var convention = new DocumentConvention();

                var requestParams = new CreateHttpJsonRequestParams(store.DatabaseCommands, url, "DELETE", metadata, credentials, convention);

                var request = factory.CreateHttpJsonRequest(requestParams);

                request.ExecuteRequest();

            }

        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);
        }

        Console.WriteLine("Press any key..");
        Console.ReadKey();

    }

}

执行此代码时,会引发以下异常。

  

System.Net.WebException:远程服务器返回错误:(401)未经授权。     在System.Net.HttpWebRequest.GetResponse()     在Raven.Client.Connection.HttpJsonRequest.ReadJsonInternal(Func`1 getResponse)中的c:\ Builds \ RavenDB-Unstable-v1.2 \ Raven.Client.Lightweight \ Connection \ HttpJsonRequest.cs:第297行     at Raven.Client.Connection.HttpJsonRequest.ReadResponseJson()in c:\ Builds \ RavenDB-Unstable-v1.2 \ Raven.Client.Lightweight \ Connection \ HttpJsonRequest.cs:line 218     位于c:\ Builds \ RavenDB-Unstable-v1.2 \ Raven.Client.Lightweight \ Connection \ HttpJsonRequest.cs中的Raven.Client.Connection.HttpJsonRequest.ExecuteRequest():第161行     在COM:\ code \ buyefficient_mvc \ Com.BuyEfficient \ Com.BuyEfficient.Raven中的Com.BuyEfficient.Raven.Service.Program.Main(String [] args)   .Service \ Program.cs:第39行

我的问题是“我如何正确设置凭据以便控制台应用程序经过身份验证”?

谢谢你, 斯蒂芬

UPDATE1  代码已更新以反映已接受的答案

1 个答案:

答案 0 :(得分:5)

尝试使用CredentialCache.DefaultCredentials,或在计算机上提供管理员用户的凭据。