简体   繁体   中英

Debugging Data-Service-Request-Exception on WCF-Data-Service during add new entity

this is my service code :

using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.Linq.Expressions;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Web;

namespace RadAppSilver.Web
{
    public class DsWFS006 : DataService<WFS006Entities>
    {
        public DsWFS006()
        {
            ServiceHost host = new ServiceHost(typeof(DsWFS006));
            ServiceDebugBehavior debug = host.Description.Behaviors.Find<ServiceDebugBehavior>();
            // if not found - add behavior with setting turned on 
            if (debug == null)
            {
                host.Description.Behaviors.Add(
                     new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
            }
            else
            {
                // make sure setting is turned ON
                if (!debug.IncludeExceptionDetailInFaults)
                {
                    debug.IncludeExceptionDetailInFaults = true;
                }
            }
            host.Open();
            // This method is called only once to initialize service-wide policies.
        }

        public static void InitializeService(DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            //config.SetEntitySetPageSize("DocDetail", 30);            
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;            
        }              
    }    
}

I need to debug when I'm going to new record to my entity error happened but update entity works fine :

 private void Grid1RowEditEnded(object sender, Telerik.Windows.Controls.GridViewRowEditEndedEventArgs e)
    {
        if (e.EditAction == Telerik.Windows.Controls.GridView.GridViewEditAction.Commit)
        {
            doc.AccNo = string.IsNullOrEmpty(SelectedAcc) ? doc.AccNo : SelectedAcc;
            if (e.EditOperationType == Telerik.Windows.Controls.GridView.GridViewEditOperationType.Edit)
            {                    
                service.UpdateObject(doc);
            }
            else if (e.EditOperationType == Telerik.Windows.Controls.GridView.GridViewEditOperationType.Insert)
            {
                (this.grid1.ItemsSource as VirtualQueryableCollectionView).Add(doc);
                service.AddObject("DocDetail", doc);
            }
            service.BeginSaveChanges(OnChangesSaved, service);
        }
    }

    private void OnChangesSaved(IAsyncResult result)
    {
        Dispatcher.BeginInvoke(() =>
        {
            service = result.AsyncState as DS1.WFS006Entities;
            try
            {
                service.EndSaveChanges(result);
            }
            catch (DataServiceRequestException ex)
            {
                MessageBox.Show(ex.Response.ToString());
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show(ex.Message);
            }
        });
    }

and this code include initializing service on my client :

private void SetContext()
    {
        service = new DSEntity();

        DataServiceQuery<DS1.Accounts> queryAcc = (DataServiceQuery<DS1.Accounts>)
                                                  (service.Accounts.Select(m =>
                                                      new DS1.Accounts
                                                   {
                                                       AccNo = m.AccNo,
                                                       AccDesc = m.AccDesc
                                                   }));
        queryAcc.BeginExecute(t =>
        {
            DataServiceQuery<DS1.Accounts> state = t.AsyncState as DataServiceQuery<DS1.Accounts>;
            var executedState = state.EndExecute(t);
            ObservableCollection<DS1.Accounts> data = new ObservableCollection<DS1.Accounts>();
            foreach (var entity in executedState)
                data.Add(entity);
            AccCache = data.ToList();
        }, queryAcc);

        var view = new VirtualQueryableCollectionView() { LoadSize = 300, VirtualItemCount = 10000 };
        view.ItemsLoading += (y, e) =>
        {
            DataServiceQuery<DS1.DocDetail> query = (DataServiceQuery<DS1.DocDetail>)
                service.DocDetail.OrderBy(it => it.Item)
                                     .Where<DS1.DocDetail>(it => it.DocSerNo == 91120001)
                                     .Where(view.FilterDescriptors)
                                     .Sort(view.SortDescriptors)
                                     .Skip(e.StartIndex)
                                     .Take(e.ItemCount);
            query = query.IncludeTotalCount();
            query.BeginExecute(
                  s =>
                  {
                      DataServiceQuery<DS1.DocDetail> state = s.AsyncState as DataServiceQuery<DS1.DocDetail>;
                      var executedState = state.EndExecute(s);
                      var response = executedState as QueryOperationResponse<DS1.DocDetail>;
                      int count = (int)response.TotalCount;
                      ObservableCollection<DS1.DocDetail> data = new ObservableCollection<DS1.DocDetail>();
                      foreach (var entity in executedState)
                          data.Add(entity);
                      var dataSource = data.ToList();
                      view.VirtualItemCount = count;
                      view.Load(e.StartIndex, dataSource);
                  }, query);
        };
        grid1.ItemsSource = view;
    }

it doesn't work while add new object and exception doesn't give me any detail when I add host.open(); on constructor to show exception detail the service has been stop.

Include all the option for debugging the wcf service

1.Apply the following attribute to your service class [System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]

  1. Override the following two methods in your service class a. protected override void OnStartProcessingRequest(ProcessRequestArgs args) b,protected override void HandleException(HandleExceptionArgs args)

set the break points on these two methods and see what type of exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM