Silverlight中调用WebService-发送邮件测试实例
Silverlight 能够构建强大的企业级应用程序,关键和Silverlight2.0强大数据通信 是分不开的,包括SOAP服务的访问,RESTful服务的访问,以及基 于Http协议和Socket通信等。在Silverlight中一些基本的功能都适用于Http和Https通信。
其中主要特点:
A:始终允许同域调用
B:如果正确配置负载有Web服务的Web服务器,可以跨域访问
C:仅仅支持Get和Post谓词
D:所有请求都是异步的-这也是SL用户体验最为吸引人一个特点
E:只有"00确定"和"404为找到"状态可用
我们看到B支持跨域访问,Silverlight默认情况是请求的URL进行严格限制的,只允许访问同一子目录下的URL资源。禁止跨域访问的,例如当前我们在本地的Web程序中引用新浪网提供天气预报服务http://sina.com/service.asmx ,默认情况下会抛出一个异常.
这个异常抛出的主要原因是在完成事件Complete引用的参数为空,解决方案:在需要被访问的站点根目录下添加两个策略文件-两个XML文件 clientaccesspolicy.xml,crossdomain.xml即可,注意两个XML文件最好使用默认的UTF-8编码格式,如果修改在 程序中极易出现错误,且不容易发现.主要是因为请求都是异步的发送客户端的数据格式XML编码就是UTF-8格式。
这是昨天遇到一个问题,下面做一个简单的在Silverlight程序中调用本地WebService(非跨域)实例.
(1)创建WebService并实现WebService:-
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1 // WebService访问数据
2 [WebMethod]
3 public List < BookEnticy > GetAllBookEnticy()
4 {
5 List < BookEnticy > getBooklist = new List < BookEnticy > {
6 new BookEnticy {BookId = 120 ,BookName = " 我的非正常生活! " } ,
7 new BookEnticy {BookId = 121 ,BookName = " 中央警备司令部 " } ,
8 new BookEnticy {BookId = 122 ,BookName = " 警戒线 " }
9
10 } ;
11 // 导入System.Data.Linq 空间
12 IDbConnection getcon = new SqlConnection ( " server=local;database=TestLinq;uid=sa;pwd=sa " );
13 DataContext getcontent = new DataContext(getcon);
14
15 GetServerClintDataWS newdata = new GetServerClintDataWS();
16 List < string > getlist = from getlistinfo in newdata.GetAllBookEnticy()
17 select getlistinfo.BookName;
18 return getBooklist;
19 }
(2)Silverlight中引用并调用
选中SL项目右击弹出的菜单选择 添加服务引用,SL应用程序中会自动生成一个文件夹
当添加引用后,自动添加SL程序中对应该服务的本地代理类 ,代理类是基于System.ServiceModel空间种ClintBase构建的,另外一个配置文件 ServiceReferences.ClintConfig是客户端配置文件,用来配置访问服务地址,绑定契约等信息
Silverlight只支持异步调用,调用完成奖触发一个Complete事件,最终将在OnComplete事件中进行数据绑定
根据上面步骤用Silverlight简单发邮件功能:
A:创建WebService并实现:核心代码如下
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> [WebMethod]
/**/ /// 发送Email
/// 参数:fromaddress-发件人 toaddress-收件人 标题-title content-内容
public bool SendEmail( string fromaddress, string toaddress, string title, string content)
{
try
{
MailMessage newmail = new MailMessage();
newmail.From = new MailAddress(fromaddress);
newmail.To.Add( new MailAddress(toaddress)); // 因为可以抄送 对应多人
newmail.Subject = title;
newmail.Body = content;
newmail.IsBodyHtml = false ;
// use SmtpClint send Email
SmtpClient getclint = new SmtpClient();
getclint.EnableSsl = true ;
getclint.Send(newmail);
return true ;
}
catch (Exception se)
{
return false ;
}
return false ;
}
在Asp.net实现发送邮件功能必须引用System.net.Mail空间,发送邮件细节请参考MSDN:http://msdn.microsoft.com/zh-cn/library/system.net.mail.mailmessage.aspx ,WebService 中发送邮件方法总共接受四个参数:FromAddress-发件人 ToAddress-收件人 Title:标题 Body:内容,在建立WebService发送邮件自前需要建立一个邮件服务器,一般使用Gmail可以使用自己的账号,在根目录Web.Config 中配置,详细代码如下:
< system.net >
< mailSettings >
< smtp >
< network host = " smtp.gmail.com " port = " 587 " userName = " ChenkaiHome@gmail.com " password = "" />
</ smtp >
</ mailSettings >
</ system.net >
配置完必须在Silverlight中添加服务引用
(2)Silverlight做前台页面:
效果图-(简化版):
页面Silverlight代码:
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> < Grid x:Name ="LayoutRoot" Background ="Bisque" >
< Canvas >
< Grid >
< Grid.RowDefinitions >
< RowDefinition Height ="70" ></ RowDefinition >
< RowDefinition Height ="70" ></ RowDefinition >
< RowDefinition Height ="80" ></ RowDefinition >
< RowDefinition Height ="*" ></ RowDefinition >
</ Grid.RowDefinitions >
< Grid.ColumnDefinitions >
< ColumnDefinition Width ="80" ></ ColumnDefinition >
< ColumnDefinition Width ="*" ></ ColumnDefinition >
</ Grid.ColumnDefinitions >
< TextBlock Text ="收件人:" Grid.Row ="0" Grid.Column ="0" TextAlignment ="Center" HorizontalAlignment ="Center" VerticalAlignment ="Center" FontSize ="14" ></ TextBlock >
< TextBox x:Name ="address" Grid.Row ="0" Grid.Column ="1" Height ="40" Width ="250" FontSize ="14" TextAlignment ="Left" HorizontalAlignment ="Center" VerticalAlignment ="Center" > </ TextBox >
< TextBlock Text ="标 题:" Grid.Row ="1" Grid.Column ="0" FontSize ="14" TextAlignment ="Center" HorizontalAlignment ="Center" VerticalAlignment ="Center" ></ TextBlock >
< TextBox x:Name ="title" Grid.Row ="1" Grid.Column ="1" Height ="40" Width ="250" FontSize ="14" TextAlignment ="Left" HorizontalAlignment ="Center" VerticalAlignment ="Center" ></ TextBox >
< TextBlock Text ="内 容:" Grid.Row ="2" Grid.Column ="0" FontSize ="14" HorizontalAlignment ="Center" VerticalAlignment ="Center" ></ TextBlock >
< TextBox TextWrapping ="Wrap" x:Name ="content" Grid.Row ="2" Grid.Column ="1" ></ TextBox >
< Button Content ="Send" Grid.Row ="3" Grid.Column ="0" Height ="45" Click ="Button_Click" ></ Button >
</ Grid >
</ Canvas >
</ Grid >
当点击Send触发事件后台处理代码:
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> // send Email
private void Button_Click( object sender, RoutedEventArgs e)
{
string fromaddress = " dkjay@163.com " ;
ServiceReference2.SendEmailTestSoapClient getclint = new TestWebServiceSL.ServiceReference2.SendEmailTestSoapClient();
getclint.SendEmailCompleted += new EventHandler < TestWebServiceSL.ServiceReference2.SendEmailCompletedEventArgs > (getclint_SendEmailCompleted);
}
void getclint_SendEmailCompleted( object sender, TestWebServiceSL.ServiceReference2.SendEmailCompletedEventArgs e)
{
// 判断并提示
if (e.Error != null )
{
HtmlPage.Window.Alert( " Send Suceess! " );
}
else
{
HtmlPage.Window.Alert( " Send Faile! " );
}
}