How does one return a custom type of a non-entity object using the WCF Ria service? In fact, it is very easy to define the serializable type in the way of WCF. Method: add an empty domain to the silverlightapplication1.web project.
Service, named myservice. Add a method to write an invoke. Note that [invoke] is required for this domainservice.
Attribute indicates that this is not an entity-related operation, nor is it a query, insert, update, or delete operation. For more information, see the msdn page.
The [invoke] method can return any type. Here, Let it return a non-entity custom type. First:
Top custom type
1 [Datacontract]
2 Public Class Mytestclass
3 {
4 [Datamember]
5 Public String [] Mylist { Get ; Set ;}
6
7 [Datamember]
8 Public Int Mydata { Get ; Set ;}
9 }
Then:
Write a domainservice
1 [Enableclientaccess ()]
2 Public Class Myappservice: domainservice
3 {
4 [Invoke]
5 Public Mytestclass test ( String A, String B)
6 {
7 // Todo :....
8 Return New Mytestclass (); // Return custom type
9 }
10
11 Protected Override Void Onerror (domainserviceerrorinfo errorinfo)
12 {
13 Base . Onerror (errorinfo );
14
15 Logger. logexception (errorinfo. Error. Message, errorinfo. Error. stacktrace );
16 }
17 }
After compilation, you can view the hidden file generated_code \ * in the foreground Silverlight project corresponding to riaservice (you can view the properties of the project, including the WCF Ria service link \*. web. g. CS. The local proxy and local type of the WCF are automatically generated. You can call the following code in Silverlight:
*. XAML. CS
1 Mytestcontext CTX = New Mytestcontext ();
2
3 CTX. Test ( " A " , " B " , Invokeoperation =>
4 {
5 If (Invokeoperation. haserror)
6 {
7 This . Label1.text = Invokeoperation. Error. message;
8 }
9 Else
10 {
11 Mytestclass result = Invokeoperation. value; // Get the result of the custom type
12 // Todo :.....
13 }
14 }, Null );
It is no problem to publish and deploy IIS. Just add the. xap and. xaml mime types. This article is the Mainz original in the blog Park (http://www.cnblogs.com/mainz/), if you see this article in silver light China (silverlightchina) or other sites, please note that is reproduced, many links in the article has been lost, pay attention to the subsequent content of this blog. If you do not understand Silverlight + WCF Ria service + EF, you can view other blog posts in the blog. I also have the Silverlight SeriesArticleCan be read.