获取post json数据的结果到wcf rest service

时间:2017-05-29 04:23:37

标签: c# json rest wcf

我有休息服务。

 [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/AddNews", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        bool Add(News entity);

这里是imp:

  public bool Add(News entity)
        {
            try
            {
                _ctx.News.Add(entity);
                _ctx.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                // TODO log this error
                return false;
            }
        }

我将数据发布到我的服务但是我需要操作的结果,这里是bool。我可以在代码中得到结果吗?

   News student = new News
            {
                Id = Guid.NewGuid(),
                Subject = "wfwf",
                ViewerCounter = 1, // removed the "" (string)
                MainContent = "fsdsd", // renamed from "Content"
                SubmitDateTime = DateTime.Now,
                ModifiedDateTime = DateTime.Now,
                PublisherName = "sdaadasd",
                PictureAddress = "adfafsd",
                TypeOfNews = "adsadaad"
            };
            WebClient Proxy1 = new WebClient();
            Proxy1.Headers["Content-type"] = "application/json";
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer serializerToUplaod = new DataContractJsonSerializer(typeof(News));
            serializerToUplaod.WriteObject(ms, student);
             Proxy1.UploadData("http://localhost:47026/NewsRepository.svc/AddNews", "POST", ms.ToArray());

2 个答案:

答案 0 :(得分:0)

请使用:

byte[] a=    Proxy1.UploadData("http://localhost:47026/NewsRepository.svc/AddNews", "POST", ms.ToArray());


            string result = System.Text.Encoding.UTF8.GetString(a);

答案 1 :(得分:-1)

尝试使用https://www.nuget.org/

中的NuGet包