【问题标题】:Unable to use methods from Domain Service class in WCF Ria?无法在 WCF Ria 中使用域服务类中的方法?
【发布时间】:2012-02-15 12:09:34
【问题描述】:

我正在尝试构建一个 Silverlight 应用程序,该应用程序在文本框中获取用户的名字、姓氏、密码、电子邮件地址,然后将这些添加到数据库中。

为此,我使用WCF Ria Services

我遵循的步骤是:

在我的项目中添加了ADO.NET Entity Data Model,然后添加了Domain Service class(在Web 部分中)。

现在我的 DomainService 类中有一些预定义的方法,例如插入、更新方法。我知道如何在DataGrid 中显示数据,但这不是我想要的。

我想要的是自定义所有这些:

当用户单击提交按钮时,其中应该有类似AddInfo(all parameters) 的方法,可以将所有数据添加到我的 sql server 数据库 {目前 LocalHost}。

简而言之通过自定义方法访问您的数据库,以使用 WCF Ria 服务在 sql server 中添加数据

我知道在 .net 表单中工作时非常简单。但是 Silverlight 和 WCF ria 怎么样?

请提出建议。

【问题讨论】:

    标签: c# sql wcf silverlight wcf-ria-services


    【解决方案1】:

    简单来说,通过自定义方法访问您的数据库以添加 使用 WCF Ria 服务的 sql server 中的数据

    你应该做的是在服务器端编写一个自定义方法。

    在服务器端,您有一个应该从 LinqToEntitiesDomainService<TContext> 继承的 DomainService 类。

    只需在这个类中添加一个带有Invoke属性的方法,例如:

    [Invoke]
    public void AddNewUser(string name, string firstName, int age)
    {
        // Put logic here to add the user to the DB
    }
    

    将用户添加到数据库的逻辑非常简单,只需新建一个Entity,将其添加到上下文中并调用context.SubmitChanges();

    当您编译客户端 RIA 服务项目时,与您的 DomainService 对应的自动生成的代理类将包含您的新方法,您可以使用以下方法调用它:

    yourDomainContext ctx = new yourDomainContext();
    ctx.AddNewUser("dsd", "ds", 42).Completed += (sender, e) =>
    {
         // Called asynchronously when the job is done
    };
    

    【讨论】:

      【解决方案2】:

      如果您的域服务上已经有 Insert 方法,您应该可以从客户端调用:

      //add your new data to the context
      MyDomainServiceContext.Entity.Add(myEntity); //(where "Entity" is your entity Type)
      //send all the changes to the server
      MyDomainServiceContext.SubmitChanges();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        相关资源
        最近更新 更多