调用Ria Service中方法的各种方式

时间:2021-01-23 16:26:50

前端界面后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel.DomainServices.Client;
using System.Collections.ObjectModel; using SilverlightRiaService.Web; namespace SilverlightRiaService
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent(); ServiceClass sc = new ServiceClass(); sc.GetUnitType(, "admin", DateTime.Now.AddDays(-), DateTime.Now, o =>
{
if (o.HasError)
{
MessageBox.Show("error");
}
else
{
string strvalue = o.Value;
MessageBox.Show("直接调" + strvalue);
}
}, null);
sc.InvokeOperation("GetUnitType", typeof(IEnumerable<string>),
new Dictionary<string, object> { { "unit", }, { "systype", "admin" }, { "startTime", DateTime.Now.AddDays(-) }, { "endTime", DateTime.Now } }, true, evv =>
{
if (!evv.HasError)
{
var sd = evv.Value;
MessageBox.Show("Invoke" + sd.ToString());
}
}, null); /*
在Silverlight中创建数据源集合可以使用内建的ObservableCollection类,
因为ObservableCollection类既实现了INotifyPropertyChanged接口,又实现了INotifyCollectionChanged接口。
使用ObservableCollection类不但可以实现Add、Remove、Clear和Insert操作,还可以触发PropertyChanged事件。
*/ ObservableCollection<Student> stulist = new ObservableCollection<Student>();
sc.GetStudentByName("叶薇", ye =>
{
if (!ye.HasError)
{
stulist = (ObservableCollection<Student>)ye.Value;
}
}, null);
sc.InvokeOperation("GetStudentByName", typeof(ObservableCollection<Student>),
new Dictionary<string, object> { { "name", "叶朔" } }, true, shuo =>
{
if (!shuo.HasError)
{
stulist = new ObservableCollection<Student>(shuo.Value as IEnumerable<Student>);
List<Student> sdstulist = new List<Student>(shuo.Value as IEnumerable<Student>);
}
}, null);
}
}
}

Ria Service方法:

namespace SilverlightRiaService.Web
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server; [EnableClientAccess()]
public class ServiceClass : DomainService
{
public string GetUnitType(int unit, string systype, DateTime startTime, DateTime endTime)
{
return unit + systype + startTime + endTime;
} public IEnumerable<Student> GetStudentByName(string name)
{
List<Student> list = new List<Student>();
list.Add(new Student()
{
StudentName = name,
Sex = "男"
});
return list;
} }
}

Web Config 添加:

<?xml version="1.0" encoding="utf-8"?>

<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
--> <configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="DomainServiceModule" preCondition="managedHandler" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</modules>
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer> <system.web>
<httpModules>
<add name="DomainServiceModule" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
</configuration>