WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx

上传人:b****4 文档编号:7920595 上传时间:2023-05-09 格式:DOCX 页数:24 大小:1.49MB
下载 相关 举报
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第1页
第1页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第2页
第2页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第3页
第3页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第4页
第4页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第5页
第5页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第6页
第6页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第7页
第7页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第8页
第8页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第9页
第9页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第10页
第10页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第11页
第11页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第12页
第12页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第13页
第13页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第14页
第14页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第15页
第15页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第16页
第16页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第17页
第17页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第18页
第18页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第19页
第19页 / 共24页
WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx_第20页
第20页 / 共24页
亲,该文档总共24页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx

《WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx》由会员分享,可在线阅读,更多相关《WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx(24页珍藏版)》请在冰点文库上搜索。

WCF与Linq To SQL与Silverlight架构例子打印版文档格式.docx

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;

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 广告传媒

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2