WCF学习笔记(1)——Hello World

本文将建立一个silverlight与wcf通讯的简单实例。以下是详细步骤:


 

  新建一个Silverlight应用程序,项目名称WCFtest,本例使用的是Silverlight 4版本。


  在WCFtest.Web上添加一个WCF服务,名称ServiceTest.svc,将WCF服务寄宿在Web中,VS默认生成IServiceTest.cs接口和ServiceTest.svc文件。


 

  打开IServiceTest.cs,添加接口方法SayHello,参数name返回string型结果。

    [ServiceContract]//接口标识,服务契约
public interface IServiceTest
{
[OperationContract]//方法名标识
void DoWork();

[OperationContract]
string SayHello(string name);
}

  打开ServiceTest.svc.cs文件,实现SayHello接口方法,传入name,返回Hello,name的结果。

    public class ServiceTest : IServiceTest
{
public void DoWork()
{
}

#region IServiceTest 成员


public string SayHello(string name)
{
return string.Format("Hello,{0}", name);
}

#endregion
}

  重新生成项目,否则可能会出现发现服务失败的情况,在WCFtest的Silverlight项目上,添加服务应用。点击"发现"按钮,出现刚刚添加的ServiceTest服务,修改命名空间名称ServiceReferenceTest。


  VS自动生成Service References文件夹和ServiceReferences.ClientConfig文件。


  打开WCFtest的Silverlight项目的MainPage.xmal,为了便于演示,添加一个TextBox,一个Button,一个TextBlock

    <Grid x:Name="LayoutRoot" Background="White">
<TextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="12,41,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="12,70,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" Width="120" />
</Grid>

  修改WCFtest.Web,配置web固定端口12345。


  修改ServiceReferences.ClientConfig文件,修改端口号12345,终结点endpoint的地址 http://localhost:12345/ServiceTest.svc

        <client>
<endpoint address="http://localhost:12345/ServiceTest.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IServiceTest" contract="ServiceReferenceTest.IServiceTest"
name="BasicHttpBinding_IServiceTest" />
</client>

  回到Sliverlight的MainPage.xaml中,添加button1的点击Click事件。

        private void button1_Click(object sender, RoutedEventArgs e)
{
Binding binding = new BasicHttpBinding();
EndpointAddress endPoint = new EndpointAddress("http://localhost:12345/ServiceTest.svc");
ServiceReferenceTest.ServiceTestClient client = new ServiceReferenceTest.ServiceTestClient(binding, endPoint);
client.SayHelloCompleted += new EventHandler<ServiceReferenceTest.SayHelloCompletedEventArgs>(client_SayHelloCompleted);
client.SayHelloAsync(this.textBox1.Text);
}

Binding binding = new BasicHttpBinding();绑定通讯方式,这里是BasicHttp方式;

EndpointAddress endPoint = new EndpointAddress("http://localhost:12345/ServiceTest.svc");指定svc服务路径;

            client.SayHelloCompleted += new EventHandler<ServiceReferenceTest.SayHelloCompletedEventArgs>(client_SayHelloCompleted);指定调用SayHello方法完成后的回调事件,将结果显示在textblock1上;

        void client_SayHelloCompleted(object sender, ServiceReferenceTest.SayHelloCompletedEventArgs e)
{
this.textBlock1.Text = e.Result;
}

 

client.SayHelloAsync(this.textBox1.Text);异步调用SayHello方法,将textBox1的text内容作参数传给SayHello方法。


  F5运行,在textBox中输入内容,点击button,调用SayHello方法,将输入的内容作为name参数传递给SayHello方法,SayHello方法处理后的结果返回给客户端,textBlock显示返回的结果。


WCF学习笔记目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值