我已经实现了我认为删除数据库(作为服务运行的乌鸦)的正确方法,基于 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。 c:\Builds\RavenDB-Unstable-v1.2\Raven.Client.Lightweight\Connection\HttpJsonRequest.cs:Raven.Client.Connection.HttpJsonRequest.ReadResponseJson() 的第 297 行:Raven.Client.Connection 的第 218 行.HttpJsonRequest.ExecuteRequest() 在 c:\Builds\RavenDB-Unstable-v1.2\Raven.Client.Lightweight\Connection\HttpJsonRequest.cs:com.BuyEfficient.Raven.Service.Program.Main(String[] 的第 161 行args) 在 c:\code\buyefficient_mvc\Com.BuyEfficient\Com.BuyEfficient.Raven .Service\Program.cs:line 39
我的问题是“如何正确设置凭据以对控制台应用程序进行身份验证”?
谢谢你,斯蒂芬
UPDATE1 代码已更新以反映已接受的答案。