代码之家  ›  专栏  ›  技术社区  ›  Xavier Poinas

RIA方法未出现在Silverlight中

  •  0
  • Xavier Poinas  · 技术社区  · 14 年前

    我有一个域服务,有几个方法。

    其中一个返回类型为string,没有参数:

    public string MyMethod1() { }
    

    我可以从Silverlight调用这个。

    其中一个返回类型为void,参数为域对象(我正在使用LinqTosqlDomainService,此对象是DataContext的一部分):

    public void MyMethod2(MyDomainObject object) { }
    

    我也可以从Silverlight调用这个。

    另一个具有字符串的返回类型和域对象的参数:

    public string MyMethod3(MyDomainObject object) { }
    

    无法从Silverlight调用此函数 ,因为该方法不会在代理上生成。

    为什么不生成它,我该怎么做呢?

    1 回复  |  直到 14 年前
        1
  •  2
  •   Joe McBride    14 年前

    尝试添加 [Invoke] 操作的属性。

    我创建了一个使用以下契约定义的示例应用程序,并且能够在Silverlight应用程序中生成所有三个方法。

    这是DBML文件。 LINQ to SQL DBML file

    这里是定义的服务。

    [EnableClientAccess()]
    public class DomainService1 : LinqToSqlDomainService<DataClasses1DataContext>
    {
        public Player GetPlayer()
        {
            throw new NotImplementedException();
        }
    
        public void MyMethod(Player player)
        {
        }
    
        [Invoke]
        public string MyMethod2(Player player)
        {
            return String.Empty;
        }
    }
    

    这是Silverlight项目中生成的代码:

    /// <summary>
    /// Gets an EntityQuery instance that can be used to load <see cref="Player"/> entities using the 'GetPlayer' query.
    /// </summary>
    /// <returns>An EntityQuery that can be loaded to retrieve <see cref="Player"/> entities.</returns>
    public EntityQuery<Player> GetPlayerQuery()
    {
        this.ValidateMethod("GetPlayerQuery", null);
        return base.CreateQuery<Player>("GetPlayer", null, false, false);
    }
    
    /// <summary>
    /// Invokes the 'MyMethod' method of the specified <see cref="Player"/> entity.
    /// </summary>
    /// <param name="player">The <see cref="Player"/> entity instance.</param>
    public void MyMethod(Player player)
    {
        player.MyMethod();
    }
    
    /// <summary>
    /// Asynchronously invokes the 'MyMethod2' method of the domain service.
    /// </summary>
    /// <param name="player">The value for the 'player' parameter of this action.</param>
    /// <param name="callback">Callback to invoke when the operation completes.</param>
    /// <param name="userState">Value to pass to the callback.  It can be <c>null</c>.</param>
    /// <returns>An operation instance that can be used to manage the asynchronous request.</returns>
    public InvokeOperation<string> MyMethod2(Player player, Action<InvokeOperation<string>> callback, object userState)
    {
        Dictionary<string, object> parameters = new Dictionary<string, object>();
        parameters.Add("player", player);
        this.ValidateMethod("MyMethod2", parameters);
        return ((InvokeOperation<string>)(this.InvokeOperation("MyMethod2", typeof(string), parameters, true, callback, userState)));
    }
    
    /// <summary>
    /// Asynchronously invokes the 'MyMethod2' method of the domain service.
    /// </summary>
    /// <param name="player">The value for the 'player' parameter of this action.</param>
    /// <returns>An operation instance that can be used to manage the asynchronous request.</returns>
    public InvokeOperation<string> MyMethod2(Player player)
    {
        Dictionary<string, object> parameters = new Dictionary<string, object>();
        parameters.Add("player", player);
        this.ValidateMethod("MyMethod2", parameters);
        return ((InvokeOperation<string>)(this.InvokeOperation("MyMethod2", typeof(string), parameters, true, null, null)));
    }