I have a WCF RIA Domain Service that contains a method I'd like to invoke when the user clicks a button:
[Invoke]
public MyEntity PerformAnalysis(int som开发者_运维技巧eId)
{
return new MyEntity();
}
However, when I try to compile I'm given the following error:
Operation named 'PerformAnalysis' does not conform to the required signature.
Return types must be an entity, collection of entities, or one of the
predefined serializable types.
The thing is, as far as I can tell, MyEntity is an entity:
[Serializable]
public class MyEntity: EntityObject, IMyEntity
{
[Key]
[DataMember]
[Editable(false)]
public int DummyKey { get; set; }
[DataMember]
[Editable(false)]
public IEnumerable<SomeOtherEntity> Children { get; set; }
}
I figure I'm missing something simple here. Could someone please tell me how I can create an invokable method that returns a single MyEntity object?
The code that you have here:
[Invoke]
public MyEntity PerformAnalysis(int someId)
{
return new MyEntity();
}
is fine, but you also need a IEnumerable to make this work:
public IEnumerable<MyEntity> GetMyEntities()
{
throw new NotImplementedException();
}
What that means is that for WCF RIA service to return custom types it needs to have at least one method for that custom type that returns an IEnumerable of that type.
This question was answered by YasserMohamedMCTS over on the Silverlight Forum.
simply add [Query] attribute on top of the invoke method.
Sometimes you have to take out a Class Constructor and it will compile without errors.
Here is a sample which compiles correct.
public class PluginControlCommandView
{
public Nullable<DateTime> CreationTime { get; set; }
public string Description { get; set; }
public Nullable<Guid> PlayerControlCommandID { get; set; }
public Nullable<Guid> EventFramePluginID { get; set; }
public Nullable<DateTime> ExecutionTime { get; set; }
public Nullable<Guid> ID { get; set; }
public Nullable<bool> IsConsole { get; set; }
public Nullable<bool> IsExecuted { get; set; }
public PluginCommands PluginCommand { get; set; }
// !!! You can see that here is a IEnumerable! :)
public IEnumerable<PluginCommandDetailView> PluginCommandDetails { get; set; }
public PluginStates PluginState { get; set; }
}
[Invoke]
public void UpdatePluginControlCommandView(PluginControlCommandView commandView)
{
....
}
Create Your own class in server side project like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using System.ComponentModel.DataAnnotations;
using System.Data.Objects.DataClasses;
namespace yournamespace
{
[DataContract]
public class Custom : EntityObject
{
[DataMember()]
[Key()]
public int id { set; get; }
[DataMember()]
public string name { set; get; }
public Custom()
{
name = "Pouya";
}
}
}
add your method to your DomainService in server side project like:
public Custom GetCustom()
{
return new Custom();
}
add these code to one of your page at client side project
public partial class Admin : Page
{
LoadOperation<Custom> operation;
Custom ali = new Custom();
public Admin()
{
InitializeComponent();
}
// Executes when the user navigates to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
operation = DomainContext.Load(DomainContext.GetCustomQuery());
operation.Completed += new EventHandler(operation_Completed);
}
void operation_Completed(object sender, EventArgs e)
{
if (!operation.HasError)
{
ali = operation.Entities.FirstOrDefault();
}
}
}
精彩评论