rabbitmq c#alter policy of vhost

时间:2014-12-09 15:47:03

标签: c# rabbitmq

我想知道改变vhost策略的最佳方法是在C#驱动程序中。我知道我可以将以下内容发布到API:

url:http://localhost:15672/api/policies/vhost123/DLX

体:

{
    "pattern":".*", 
    "definition": {
        "dead-letter-exchange":"DLX123"
    }, 
    "priority":0, 
    "apply-to": "all"
}

有没有办法在C#驱动程序中执行此操作?

1 个答案:

答案 0 :(得分:1)

似乎没有办法通过C#接口执行此操作,但您可以通过调用API直接执行此操作。这是我的解决方案:

    internal static void DeleteDLXPolicyOnVhost()
    {
        const string policyURL = "http://localhost:15672/api/policies/vhost123/DLX";
        using (var client = new HttpClient())
        {
            var byteArray = Encoding.ASCII.GetBytes("username123:password123");
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
            var response = client.DeleteAsync(policyURL).Result;
            switch (response.StatusCode)
            {
                case HttpStatusCode.NoContent:
                    Console.WriteLine("Old DLX policy successfully deleted");
                    break;
                case HttpStatusCode.NotFound:
                    Console.WriteLine("DLX policy was not found");
                    break;
                default:
                {
                    var content = response.Content;
                    throw new Exception(string.Format("Unhandled API response code of {0}, content: {1}", response.StatusCode, content));
                }
            }
        }
    }