这是我的服务代码:
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;
}
}
}
当我要对我的实体进行新记录时,我需要进行调试,错误发生但更新实体工作正常:
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);
}
});
}
并且此代码包括在我的客户端上初始化服务:
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;
}
添加新对象时它不起作用,当我添加 host.open() 时,异常没有给我任何细节;在构造函数上显示服务已停止的异常详细信息。