【1】:建立Silverlight应用程序。
【2】:在Silverlight中的Web中,添加WCF.
using System; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Activation; namespace SilverWCF.Web { [ServiceContract(Namespace = "")] [SilverlightFaultBehavior] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class Service1 { [OperationContract] public void DoWork() { // 在此处添加操作实现 return; } [OperationContract]//这个很重要 ,每个函数前面都必须写 public string getstring() { return "这是WCF提供的数据"; } // 在此处添加更多操作并使用 [OperationContract] 标记它们 } }
【4】:把Web生成以下。
【5】为Silverlight添加服务引用
【6】:编写代码
添加引用:
using SilverWCF.ServiceReference1;
写代码:
private void button1_Click(object sender,RoutedEventArgs e) { Service1Client client = new Service1Client();//建立服务 client.getStringCompleted += new EventHandler<getStringCompletedEventArgs>(client_getStringCompleted);//为下面的异步调用注册一个回调函数,当输入完 client.getStringCompleted +=时候,按两下Tab键,会自动补全 client.getStringAsync();//异步调用 client.CloseAsync();//关闭服务 } void client_getStringCompleted(object sender,getStringCompletedEventArgs e) { if (e.Error==null) { MessageBox.Show(e.Result);//获取WCF提供的结果 } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。