【问题标题】:Get the result of post json data to wcf rest service获取将 json 数据发布到 wcf 休息服务的结果
【发布时间】:2017-05-29 04:23:36
【问题描述】:

我有休息服务。

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

这里是小鬼:

  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());

【问题讨论】:

    标签: c# json rest wcf


    【解决方案1】:

    就用这个吧:

    byte[] a=    Proxy1.UploadData("http://localhost:47026/NewsRepository.svc/AddNews", "POST", ms.ToArray());
    
    
                string result = System.Text.Encoding.UTF8.GetString(a);
    

    【讨论】:

      猜你喜欢
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 2012-10-11
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多