WCF 数据服务 BeginSaveChanges 不保存 Silverlight 应用程序中的更改

发布于 2024-09-03 03:38:05 字数 3281 浏览 3 评论 0原文

我在让 WCF 数据服务在 Silverlight 中工作时遇到了很大的麻烦。我用的是VS2010 RC。

我一直在努力解决需要使用 clientaccesspolicy.xml & 的跨域问题。 crossdomain.xml 文件位于 Web 服务器根文件夹中,但我无法让它工作。我已经采取了将 Silverlight Web 应用程序和在同一项目中使用 WCF 数据服务来解决此问题,但这里的任何建议都会很好。

但现在我实际上可以看到来自数据库的数据并显示在 Silverlight 内的数据网格中,我以为我的麻烦已经结束了 - 但事实并非如此。我可以编辑数据并且内存中实体正在更改,但是当我调用 BeginSaveChanges (使用适当的异步 EndSaveChanges 调用)时,我没有收到任何错误,但没有数据更新在数据库中。

这是我的 WCF 数据服务代码:

public class MyDataService : DataService<MyEntities>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }

    protected override void OnStartProcessingRequest(ProcessRequestArgs args)
    {
        base.OnStartProcessingRequest(args);
        HttpContext context = HttpContext.Current;
        HttpCachePolicy c = HttpContext.Current.Response.Cache;
        c.SetCacheability(HttpCacheability.ServerAndPrivate);
        c.SetExpires(HttpContext.Current.Timestamp.AddSeconds(60));
        c.VaryByHeaders["Accept"] = true;
        c.VaryByHeaders["Accept-Charset"] = true;
        c.VaryByHeaders["Accept-Encoding"] = true;
        c.VaryByParams["*"] = true;
    }
}

我从 Scott Hanselman 的文章 在 30 分钟内为 StackOverflow 创建 OData API(包括 XML 和 JSON)

这是我的 Silverlight 应用程序中的代码:

private MyEntities _wcfDataServicesEntities;
private CollectionViewSource _customersViewSource;
private ObservableCollection<Customer> _customers;

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
    {
        _wcfDataServicesEntities = new MyEntities(new Uri("http://localhost:7156/MyDataService.svc/"));
        _customersViewSource = this.Resources["customersViewSource"] as CollectionViewSource;
        DataServiceQuery<Customer> query = _wcfDataServicesEntities.Customer;
        query.BeginExecute(result =>
        {
            _customers = new ObservableCollection<Customer>();
            Array.ForEach(query.EndExecute(result).ToArray(), _customers.Add);
            Dispatcher.BeginInvoke(() =>
            {
                _customersViewSource.Source = _customers;
            });
        }, null);
    }
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    _wcfDataServicesEntities.BeginSaveChanges(r =>
    {
        var response = _wcfDataServicesEntities.EndSaveChanges(r);
        string[] results = new[]
        {
            response.BatchStatusCode.ToString(),
            response.IsBatchResponse.ToString()
        };
        _customers[0].FinAssistCompanyName = String.Join("|", results);
    }, null);
}

我收到的响应字符串数据绑定到我的网格正常并显示“-1|False”。

我的目的是在这里进行概念验证,然后进行适当的关注点分离,将其变成一个简单的业务线应用程序。

我在这上面花了很多时间。我快被逼疯了有什么想法如何让它发挥作用吗?

I'm having a hell of a time getting WCF Data Services to work within Silverlight. I'm using the VS2010 RC.

I've struggled with the cross domain issue requiring the use of clientaccesspolicy.xml & crossdomain.xml files in the web server root folder, but I just couldn't get this to work. I've resorted to putting both the Silverlight Web App & the WCF Data Service in the same project to get past this issue, but any advice here would be good.

But now that I can actually see my data coming from the database and being displayed in a data grid within Silverlight I thought my troubles were over - but no. I can edit the data and the in-memory entity is changing, but when I call BeginSaveChanges (with the appropriate async EndSaveChangescall) I get no errors, but no data updates in the database.

Here's my WCF Data Services code:

public class MyDataService : DataService<MyEntities>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }

    protected override void OnStartProcessingRequest(ProcessRequestArgs args)
    {
        base.OnStartProcessingRequest(args);
        HttpContext context = HttpContext.Current;
        HttpCachePolicy c = HttpContext.Current.Response.Cache;
        c.SetCacheability(HttpCacheability.ServerAndPrivate);
        c.SetExpires(HttpContext.Current.Timestamp.AddSeconds(60));
        c.VaryByHeaders["Accept"] = true;
        c.VaryByHeaders["Accept-Charset"] = true;
        c.VaryByHeaders["Accept-Encoding"] = true;
        c.VaryByParams["*"] = true;
    }
}

I've pinched the OnStartProcessingRequest code from Scott Hanselman's article Creating an OData API for StackOverflow including XML and JSON in 30 minutes.

Here's my code from my Silverlight app:

private MyEntities _wcfDataServicesEntities;
private CollectionViewSource _customersViewSource;
private ObservableCollection<Customer> _customers;

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
    {
        _wcfDataServicesEntities = new MyEntities(new Uri("http://localhost:7156/MyDataService.svc/"));
        _customersViewSource = this.Resources["customersViewSource"] as CollectionViewSource;
        DataServiceQuery<Customer> query = _wcfDataServicesEntities.Customer;
        query.BeginExecute(result =>
        {
            _customers = new ObservableCollection<Customer>();
            Array.ForEach(query.EndExecute(result).ToArray(), _customers.Add);
            Dispatcher.BeginInvoke(() =>
            {
                _customersViewSource.Source = _customers;
            });
        }, null);
    }
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    _wcfDataServicesEntities.BeginSaveChanges(r =>
    {
        var response = _wcfDataServicesEntities.EndSaveChanges(r);
        string[] results = new[]
        {
            response.BatchStatusCode.ToString(),
            response.IsBatchResponse.ToString()
        };
        _customers[0].FinAssistCompanyName = String.Join("|", results);
    }, null);
}

The response string I get back data binds to my grid OK and shows "-1|False".

My intent is to get a proof-of-concept working here and then do the appropriate separation of concerns to turn this into a simple line-of-business app.

I've spent hours and hours on this. I'm being driven insane. Any ideas how to get this working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情泪▽动烟 2024-09-10 03:38:06

如果您想要更改跟踪,您需要生成支持它的客户端类,并且需要使用 DataServiceCollection 来存储结果。一个很好的描述在这里:http://msdn.microsoft.com/en-我们/library/ee373844.aspx

If you want change tracking you need to generate the client side classes with support for it and you need to use DataServiceCollection to store your results. A nice description is here : http://msdn.microsoft.com/en-us/library/ee373844.aspx

何以心动 2024-09-10 03:38:05

原来我是个白痴。您必须在数据上下文上调用 UpdateObject 来显式声明对象已更新。现在这很糟糕。为什么它不能跟踪变化?

Turns out I'm being a idiot. You must call UpdateObject on the data context to explicitly declare that an object has been updated. Now that sucks. Why can't it track changes?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文