0

任何人都可以解决这个问题我有两个几乎相同的服务,但是在更新完成后一个在获取请求上失败了?

        public void UpdateCarType(string carregistration, CarType cartype)
        {
            var findcar = cartypes.Where(s => s.CarRegistration == carregistration).FirstOrDefault();

            if (findcar != null)
            {
                findcar.CarRegistration = cartype.CarRegistration;
                findcar.CarModel = cartype.CarModel;
                findcar.CarMake = cartype.CarMake;
                findcar.CarColour = cartype.CarColour;
                findcar.CarEngineSize = cartype.CarEngineSize;
                findcar.CarHireCostPerDay = cartype.CarHireCostPerDay;

            }

这是运营合同:

    [OperationContract]
    [WebInvoke(Method = "PUT", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/UpdateCarType/{carregistration}")]
    void UpdateCarType(string carregistration, CarType cartype);

这是客户端更新和获取请求:

更新:

    private void button29_Click(object sender, RoutedEventArgs e)
    {
        string uriupdatestaff = string.Format("http://localhost:8002/Service/UpdateCarType/{0}", textBox30.Text);
        StringBuilder sb = new StringBuilder();
        sb.Append("<CarType>");
        sb.AppendLine("<CarRegistration>" + textBox30.Text + "</CarRegistration>");
        sb.AppendLine("<CarColour>" + this.comboBox6.Text + "</CarColour>");
        sb.AppendLine("<CarEngineSize>" + this.comboBox7.Text + "</CarEngineSize>");
        sb.AppendLine("</CarType>");
        string NewStudent = sb.ToString();
        byte[] arr = Encoding.UTF8.GetBytes(NewStudent);
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uriupdatestaff);
        req.Method = "PUT";
        req.ContentType = "application/xml";
        req.ContentLength = arr.Length;
        Stream reqStrm = req.GetRequestStream();
        reqStrm.Write(arr, 0, arr.Length);
        reqStrm.Close();
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        MessageBox.Show(resp.StatusDescription);
        reqStrm.Close();
        resp.Close();
    }

得到:

    {
        string uriGetdates = "http://localhost:8002/Service/CarType";
        XDocument xDoc = XDocument.Load(uriGetdates);
        var dates = xDoc.Descendants("CarType")
            .Select(n => new
            {
                CarRegistration = n.Element("CarRegistration").Value,
                CarModel = n.Element("CarModel").Value,
                CarMake = n.Element("CarMake").Value,
                CarColour = n.Element("CarColour").Value,
                CarEngineSize = n.Element("CarEngineSize").Value,
                CostPerDay = n.Element("CarHireCostPerDay").Value,
            })
            .ToList();
        dataGrid10.ItemsSource = dates;
    }

我的自动想法是因为我只定义汽车颜色和汽车发动机尺寸,其余字段为空,但完全相同的操作但与客户不这样做,即使我只更新一个字段,它也会将列表返回到数据网格中.

我得到的错误是Object reference not set to an instance of an object.当我尝试重新列出更新的车型时。

4

1 回答 1

1

更新时,您不会为CarModel和设置任何内容CarMake...

所以我不明白你怎么能找回它们。

为了避免错误,而不管理真正的问题,您需要空检查:

CarRegistration = n.Element("CarRegistration")!= null ? n.element("CarRegistration").Value : string.Empty,
                //etc.

顺便说一句,您可以使用 Xml.Linq 语法而不是 stringBuilder 来编写您的 Xml !

var car = new XElement("CarType");
car.Add(new XElement("CarRegistration"), textBox30.Text);
//etc.
var NewStudent = car.ToString();//NewStudent for a car, weird ;)
于 2012-07-27T10:19:44.863 回答