我已经实现了删除数据库的正确方法(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()中 在c:\ Builds \ RavenDB-Unstable-v1.2 \ Raven.Client.Lightweight \ Connection \ HttpJsonRequest.cs:行297中。 在c:\ Builds \ RavenDB-Unstable-v1.2 \ Raven.Client.Lightweight \ Connection \ HttpJsonRequest.cs:行218中的Raven.Client.Connection.HttpJsonRequest.ReadJsonInternal(Func`1 getResponse)。 在c:\ Builds \ RavenDB-Unstable-v1.2 \ Raven.Client.Lightweight \ Connection \ HttpJsonRequest.cs:行161中的Raven.Client.Connection.HttpJsonRequest.ExecuteRequest()。 在c:\ code \ buyefficient_mvc \ Com.BuyEfficient \ Com.BuyEfficient.Raven.Service \ Program.cs:第39行
我的问题是“如何正确设置凭据,以便控制台应用程序已通过身份验证”。
谢谢, 斯蒂芬 更新1 代码更新以反映接受的答案。