服务器端逻辑 客户端逻辑 都准备好了
现在在解决方案中添加个silverlight项目来试试调用
在解决方案"akini"中新建一个"silverlight 应用程序"项目 名称叫"akini.sl"
在刚才添加的项目"akini.sl"中添加一个名为"ServiceReferences.ClientConfig"的xml文件,其中代码如下:
<?xml version="1.0"?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="basicHttpBindingConfig" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost:1660/WcfPortal.svc" binding="basicHttpBinding" contract="WcfPortal.IWcfPortal" bindingConfiguration="basicHttpBindingConfig" name="basicHttpBindingIWcfPortal" /> </client> </system.serviceModel> </configuration>
其中http://localhost:1660的端口号 按照项目"akini.WcfService"来设置
查看项目"akini.WcfService"的属性 点击"web"页 选择"特定端口" 并输入端口号
再在项目"akini.WcfService"中添加名为"WcfPortal.svc"的svc文件,其代码如下:
<% @ServiceHost Service="Csla.Server.Hosts.Silverlight.WcfPortal" %>
再在添加名为"clientaccesspolicy.xml"的xml文件,其代码如下:
<?xml version="1.0" encoding="utf-8" ?> <access-policy> <cross-domain-access> <policy> <allow-from http-request-headers="*"> <domain uri="http://*"/> <domain uri="https://*" /> </allow-from> <grant-to> <resource path="/" include-subpaths="true"/> </grant-to> </policy> </cross-domain-access> </access-policy>
再打开"Web.config"文件 添加如下代码 其中数据库连接 请修改
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <connectionStrings> <add name="akiniEntities_SSO" connectionString="metadata=res://*/SSO.SSO.csdl|res://*/SSO.SSO.ssdl|res://*/SSO.SSO.msl;provider=System.Data.SqlClient;provider connection string='Data Source=.\sqlexpress;Initial Catalog="D:\DOCUMENTS AND SETTINGS\XUL\MY DOCUMENTS\VISUAL STUDIO 2010\PROJECTS\AKINI\AKINI.DATA\AKINI.MDF";Integrated Security=True;MultipleActiveResultSets=True'" providerName="System.Data.EntityClient" /> <add name="akiniEntities_HR" connectionString="metadata=res://*/HR.HR.csdl|res://*/HR.HR.ssdl|res://*/HR.HR.msl;provider=System.Data.SqlClient;provider connection string='Data Source=.\sqlexpress;Initial Catalog="D:\DOCUMENTS AND SETTINGS\XUL\MY DOCUMENTS\VISUAL STUDIO 2010\PROJECTS\AKINI\AKINI.DATA\AKINI.MDF";Integrated Security=True;MultipleActiveResultSets=True'" providerName="System.Data.EntityClient" /> </connectionStrings> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BinaryBinding_IWcfPortal" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> </binding> </basicHttpBinding> </bindings> <services> <service name="Csla.Server.Hosts.Silverlight.WcfPortal" behaviorConfiguration="WcfPortalBehavior"> <endpoint address="" binding="basicHttpBinding" contract="Csla.Server.Hosts.Silverlight.IWcfPortal" bindingConfiguration="BinaryBinding_IWcfPortal"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="WcfPortalBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
现在我们在UI上添加些控件来展示数据:
打开项目"akini.sl"的"MainPage.xaml"文件,添加几个简单的空间 代码如下:
<UserControl x:Class="akini.sl.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <ListBox Height="300" HorizontalAlignment="Right" Name="lstbPst" VerticalAlignment="Center" Width="150" Grid.Column="0" /> <Button Content="取Pst数据" Grid.Column="1" Height="23" HorizontalAlignment="Left" Name="btnGetData" VerticalAlignment="Center" Width="75" Click="btnGetData_Click" /> </Grid> </UserControl>
另外在cs代码中添加"btnGetData_Click"事件代码,如下:
private void btnGetData_Click(object sender, RoutedEventArgs e) { try { lstbPst.Items.Clear(); sso_pstlist pstlist; sso_pstlist.GetAll((o, ex) => { if (ex.Error != null) MessageBox.Show(ex.Error.ToString(), "Data error", MessageBoxButton.OK); else { pstlist = ex.Object; foreach (sso_pst item in pstlist) { lstbPst.Items.Add(item.pst); } } }); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
在解决方案改成多启动 "akini.sl"和"akini.WcfService"为启动 OK 开始运行
在调试之前 先向sso_pst表中添加2条数据来测试
本题系列如下:
silverlight与CSLA的快速应用01-序
silverlight与CSLA的快速应用02-新建解决方案
silverlight与CSLA的快速应用03-各项目间的关系
silverlight与CSLA的快速应用04-数据库设计
silverlight与CSLA的快速应用05-安装CslaExtension
silverlight与CSLA的快速应用06-创建实体模型
silverlight与CSLA的快速应用07-利用CslaExtension自动生成CSLA代码
silverlight与CSLA的快速应用08-客户端的CSLA代码
silverlight与CSLA的快速应用09-来个silverlight项目先试试
已发布此系列讲座的演示源码 有兴趣的TX 请下载
已修改成为使用mssql 方便大家调试
如有疑问或者提问 请留言 或者加入QQ群:73585592