WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx
《WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx》由会员分享,可在线阅读,更多相关《WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx(24页珍藏版)》请在冰点文库上搜索。
1.添加Dll引用
2.添加ClientAcessPolicy.xml
<
?
xmlversion="
1.0"
encoding="
utf-8"
>
access-policy>
<
cross-domain-access>
policy>
allow-fromhttp-request-headers="
*"
domainuri="
/>
/allow-from>
grant-to>
resourcepath="
/"
include-subpaths="
true"
/grant-to>
/policy>
/cross-domain-access>
/access-policy>
3.添加跨域访问服务类
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceWCFServer
{
[System.ServiceModel.ServiceContract]
publicinterfaceIcrossDomainService
{
[System.ServiceModel.OperationContract]
[System.ServiceModel.Web.WebGet(UriTemplate="
ClientAccessPolicy.xml"
)]
System.ServiceModel.Channels.MessageProvidePolicyFile();
}
publicclasscrossDomainService:
IcrossDomainService
publicSystem.ServiceModel.Channels.MessageProvidePolicyFile()
System.IO.FileStreamfilestream=System.IO.File.Open("
ClientAcessPolicy.xml"
System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.ReadWrite);
System.Xml.XmlReaderreader=System.Xml.XmlReader.Create(filestream);
System.ServiceModel.Channels.Messageresult=System.ServiceModel.Channels.Message.CreateMessage(System.ServiceModel.Channels.MessageVersion.None,"
"
reader);
returnresult;
}
4.添加App.config
?
configuration>
system.serviceModel>
serviceHostingEnvironmentaspNetCompatibilityEnabled="
behaviors>
endpointBehaviors>
behaviorname="
WCFServer.crossDomainServiceBehavior"
webHttp/>
/behavior>
/endpointBehaviors>
serviceBehaviors>
/serviceBehaviors>
/behaviors>
services>
servicename="
WCFServer.crossDomainService"
endpointaddress="
behaviorConfiguration="
binding="
webHttpBinding"
contract="
WCFServer.IcrossDomainService"
/>
host>
baseAddresses>
addbaseAddress="
http:
//localhost:
8731/"
/baseAddresses>
/host>
/service>
/services>
/system.serviceModel>
/configuration>
(3)设计数据库
(4)创建LinqToSQL
(5)设计基于LinqToSQL创建数据操作类
publicinterfaceImyDBControl
int?
addMytable(myTablemyTable);
List<
myTable>
searchMyTable(doubley1,doubley2);
publicclassmyDBControl:
ImyDBControl
publicint?
addMytable(myTablemyTable)
myTable.rowID=System.Guid.NewGuid();
using(testDBDataContextdb=newtestDBDataContext())
db.myTable.InsertOnSubmit(myTable);
db.SubmitChanges();
returnnull;
publicList<
searchMyTable(doubley1,doubley2)
returndb.myTable.Where(p=>
p.y>
=y1&
&
p.y<
y2).ToList();
(1+)完成WCF宿主
publicpartialclassWindow1:
Window
publicWindow1()
InitializeComponent();
System.ServiceModel.ServiceHostcrossDomainHost;
System.ServiceModel.ServiceHostmyDBControlHost;
staticintbyteSize=2147483647;
staticboolhttpGetEnabled=true;
staticstringserverURL="
;
privatevoidbutton1_Click(objectsender,RoutedEventArgse)
{
crossDomainHost=newSystem.ServiceModel.ServiceHost(typeof(CrossDomainService));
serverURL=crossDomainHost.BaseAddresses[0].AbsoluteUri;
crossDomainHost.Open();
satrtServer("
myDBControlHost"
typeof(myDBControl),typeof(ImyDBControl),refmyDBControlHost);
this.button1.Content="
服务运行中"
staticvoidsatrtServer(stringpath,TypeserviceType,TypeserviceInterface,refSystem.ServiceModel.ServiceHosthost)
stringurl=serverURL+path+"
host=newSystem.ServiceModel.ServiceHost(serviceType);
//-
System.ServiceModel.Description.ServiceMetadataBehaviorbehavior=newServiceMetadataBehavior();
behavior.HttpGetEnabled=httpGetEnabled;
behavior.HttpGetUrl=newUri(url);
host.Description.Behaviors.Add(behavior);
System.Xml.XmlDictionaryReaderQuotasreaderQuotas=newSystem.Xml.XmlDictionaryReaderQuotas();
readerQuotas.MaxStringContentLength=byteSize;
readerQuotas.MaxArrayLength=byteSize;
readerQuotas.MaxBytesPerRead=byteSize;
System.ServiceModel.BasicHttpBindingbasicHttpBinding=newSystem.ServiceModel.BasicHttpBinding();
basicHttpBinding.ReaderQuotas=readerQuotas;
basicHttpBinding.MaxReceivedMessageSize=byteSize;
host.AddServiceEndpoint(serviceInterface,basicHttpBinding,url);
host.Open();
测试
在浏览器中打入
8731/
8731/myDBControlHost/
(6)为Silverlight项目添加WCF引用
1.重新创建一个Silverlight项目
2.确保上步创建的[WCF宿主]已运行并已启动
3.在Silverlight中引用[WCF]服务
(7)开发Silverlight页面
//Canvas加载事件
privatevoidCanvas_Loaded(objectsender,RoutedEventArgse)
//保存按钮不可见
save.Visibility=Visibility.Collapsed;
//创建按钮
privatevoidcreate_Click(objectsender,RoutedEventArgse)
//清空文本框
x.Text="
y.Text="
0"
z.Text=System.DateTime.Now.ToString();
//-----------------------------------------------------------------
//保存按钮可见
save.Visibility=Visibility.Visible;
//创建按钮不可见
create.Visibility=Visibility.Collapsed;
//保存按钮
privatevoidsave_Click(objectsender,RoutedEventArgse)
//构建一个行对象
myService.myTablerow=newmySilverlight.myService.myTable();
row.x=x.Text;
row.y=double.Parse(y.Text);
row.z=DateTime.Parse(z.Text);
row.rowID=System.Guid.NewGuid();
//-------------------------------------------------------------------
//调用WCF服务完成保存
myService.ImyDBControlClientser=newmySilverlight.myService.ImyDBControlClient();
//保存完成后事件
ser.addMytableCompleted+=newEventHandler<
addMytableCompletedEventArgs>
(ser_addMytableCompleted);
//保存
ser.addMytableAsync(row);
//保存按钮不可见
//创建按钮可见
create.Visibility=Visibility.Visible;
//保存完成后
voidser_addMytableCompleted(objectsender,mySilverlight.myService.addMytableCompletedEventArgse)
MessageBox.Show("
添加完成"
);
privatevoidsearch_Click(objectsender,RoutedEventArgse)
//查询完成后的返回事件
ser.searchMyTableCompleted+=newEventHandler<
searchMyTableCompletedEventArgs>
(ser_searchMyTableCompleted);
//查询
ser.searchMyTableAsync(double.Parse(y1.Text),double.Parse(y2.Text));
//查询完成后
voidser_searchMyTableCompleted(objectsender,mySilverlight.myService.searchMyTableCompletedEventArgse)
dg.ItemsSource=e.Result;