如何读取 WCF 休息服务 PUT 中的 URL 正文?

How to read URL Body in PUT of WCF rest service?

我是 WCF 休息服务的新手。我正在尝试实现 PUT 方法,该方法将从客户端

获得 JSON 输入

将此视为我的 url 正文:

{"73":"456212c5-149c-4f04-a41d-47eeb8feee01","74":"4825c4be-2f58-4021-88b1-a5dcd17079b5"}

我已经实现了以下代码

    [OperationContract]
    [WebInvoke(Method = "PUT", BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/ListOfAlerts", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    void CloseAlert(String alertIDsToClose);

服务中:

SCOM_ConnectionSettings();
        Guid alertId = Guid.Empty;
        //StreamReader streamReader = new StreamReader(alertIDsToClose);
        //streamReader.
        Dictionary<string, string> alertIDs = JsonConvert.DeserializeObject<Dictionary<string, string>>(alertIDsToClose);
        #region test
        //String str = WebOperationContext.Current.IncomingRequest.Accept.ToString();

        //StreamReader reader = new StreamReader(alertIDs);
        //String res = reader.ReadToEnd();

        //NameValueCollection coll = HttpUtility.ParseQueryString(res);

        //foreach(string alertID in alertIDs)
        //{
        #endregion
        foreach (KeyValuePair<string, string> alertID in alertIDs)
        {
            alertId = new Guid(alertID.Value);
        }

        MonitoringAlert monitoringAlert = mgGroup.GetMonitoringAlert(alertId);


        ReadOnlyCollection<MonitoringAlertResolutionState> alertStates = mgGroup.GetMonitoringAlertResolutionStates();

        MonitoringAlertResolutionState closedState = null;
        foreach (MonitoringAlertResolutionState thisState in alertStates)
        {
            if (thisState.Name == "Closed")
            {
                closedState = thisState;
            }
        }

        if (monitoringAlert.ResolutionState != closedState.ResolutionState)
        {
            monitoringAlert.ResolutionState = closedState.ResolutionState;
            string comment = "closing availability alert";
            monitoringAlert.Update(comment);
        }

    }

我如何指定 json 数据在正文中可用,而不是在 url 中。我的函数如何在正确的轨道上从 Body.Am 读取数据?

请建议我...

如果您使用对象数组而不是逗号分隔的 JSON 项,那将非常简单。在 CloseAlerts.

中使用 DataContract 作为参数
[DataContract]
public class Alert
{
    [DataMember( Name = "Id",IsRequired = false)]
    public int Id { get; set; }

    [DataMember(Name = "Guid", IsRequired = false)]
    public Guid guid { get; set; }
}

现在方法签名为:

    [OperationContract]
    [WebInvoke(Method = "PUT", BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/ListOfAlerts", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string CloseAlert(Alert[] alertIDsToClose);

就是这样。测试方法如下:

Alert[] alerts = new [] { 
    new Alert {Id =1, guid = Guid.NewGuid()}, 
    new Alert {Id =2, guid = Guid.NewGuid()}
};

// Serialize with Json.net to keep more generalized
var data = JsonConvert.SerializeObject(alerts, typeof(Alert[]), new JsonSerializerSettings());

WebClient webClient = new WebClient();       
webClient.Headers["Content-type"] = "application/json";            
webClient.Encoding = Encoding.UTF8;

webClient.UploadString(baseAddress + "/ListOfAlerts", "POST", data);