两个相同的服务问题

时间:2012-07-27 08:58:23

标签: c# linq web-services

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

        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;
    }

我的自动思想是因为我只定义汽车颜色和汽车引擎大小,其余字段为空,但完全相同的操作,但与客户不这样做,它将列表返回到数据网格,即使我只更新一个字段。

当我尝试重新更新更新的cartype时,我得到的错误是Object reference not set to an instance of an object.

1 个答案:

答案 0 :(得分:1)

更新时,您不需要为CarModelCarMake ...

设置任何内容

所以我看不出你怎么能找回它们。

为避免错误,无需管理真正的问题,您需要进行空检查:

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