The domain service method does not compile; claims "Return types must be an entity ..."

I have a WCF RIA domain service that contains a method that I would like to call when the user clicks a button:

[Invoke] public MyEntity PerformAnalysis(int someId) { return new MyEntity(); } 

However, when I try to compile, I get 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 suppose I am missing something simple here. Can someone please tell me how can I create an invokable method that returns a single MyEntity object?

+4
source share
5 answers

This question was answered by YasserMohamedMCTS at the Silverlight Forum .

+2
source

The code you have is:

 [Invoke] public MyEntity PerformAnalysis(int someId) { return new MyEntity(); } 

great, but you also need IEnumerable to do this job:

 public IEnumerable<MyEntity> GetMyEntities() { throw new NotImplementedException(); } 

This means that for the WCF RIA service to return custom types, it must have at least one method for this custom type that returns an IEnumerable of this type.

+4
source

just add [Query] on top of the call method.

+1
source

Sometimes you need to take out the class constructor and it will compile without errors.

Here is an example that compiles correctly.

 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) { .... } 
0
source

Create your own class in a server-side project, for example:

 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 a server-side project, for example:

  public Custom GetCustom() { return new Custom(); } 

add this code to one of your pages in the 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(); } } } 
-1
source

Source: https://habr.com/ru/post/1306434/


All Articles