zoukankan      html  css  js  c++  java
  • silverlight与wcf双向通讯 例子

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


      新建Silverlight应用程序,名称WCFtest。解决方案中添加WCF服务应用程序,名称WcfServiceTest,WCF服务将不再寄宿在Web中。


      删除VS自动生成的IService1.cs和Service1.svc文件,修改WCF服务固定端口12345。


      在WCF服务应用程序WcfServiceTest上,添加应用,选择浏览定位到 C:Program FilesMicrosoft SDKsSilverlightv4.0LibrariesServer System.ServiceModel.PollingDuplex.dll,PollingDuplex是一种扩展的双工轮询通讯方法,在安装的 Silverlight SDK中可以找到。


        添加一个WCF服务,名称ServiceTest。

    修改WCF服务应用程序WcfServiceTest的Web.config文件。

    复制代码
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>

    <system.web>
    <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>
    <behaviors>
    <serviceBehaviors>
    <behavior>
    <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
    <serviceMetadata httpGetEnabled="true"/>
    <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
    <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
    </serviceBehaviors>
    </behaviors>
    <!--这里是添加的开始-->
    <services>
    <service name="WcfServiceTest.ServiceTest" >
    <endpoint address="" binding="pollingDuplexHttpBinding" contract="WcfServiceTest.IServiceTest" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    </service>
    </services>
    <extensions>
    <bindingExtensions>
    <add name="pollingDuplexHttpBinding" type="System.ServiceModel.Configuration.PollingDuplexHttpBindingCollectionElement,System.ServiceModel.PollingDuplex, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    </bindingExtensions>
    </extensions>
    <!--这里是添加的结束-->
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>

    </configuration>
    复制代码

    此处的修改,添加了两个终结点,使用服务配置编辑器打开可以看到它们


    服务配置编辑器,可以在开始菜单中找到

    右键点击其属性,复制其目标地址

    右键Wcf服务应用程序中的Web.config文件,选择打开方式,点击添加,程序名输入"服务配置编辑器的路径",友好名称"服务配置编辑器"

    选择Web.config文件的打开方式,就可以选择服务配置编辑器打开了;


       修改IServiceTest.cs文件。

    复制代码
        [ServiceContract(CallbackContract = typeof(ICallBack))]//指定回调接口ICallBack
    public interface IServiceTest
    {
    [OperationContract(IsOneWay = true)]//单向调用,不需要返回值
    void SayHellow(string name);
    }

    public interface ICallBack
    {
    [OperationContract(IsOneWay = true)]//单向调用,不需要返回值
    void ShowHello(string hello);
    }
    复制代码

    CallbackContract=typeof(ICallBack),指定了回调接口;

    (IsOneWay = true),单向调用,不需要返回值。

      修改ServiceTest.svc.cs文件,实现IService接口的SayHello方法,传进name参数,处理为My name is [name],作为参数再调用指定的回调接口ICallBack中的ShowHello方法,ICallBack中的所有方法则留给客户端实现,这里不需 要实现它。

    复制代码
        public class ServiceTest : IServiceTest
    {
    #region IServiceTest 成员

    public void SayHellow(string name)
    {
    name = string.Format("My name is {0}.", name);
    ICallBack callBack = OperationContext.Current.GetCallbackChannel<ICallBack>();
    callBack.ShowHello(name);
    }

    #endregion
    }
    复制代码

    当客户端调用服务端的SayHellow方法时,服务端收到调用,获取客户端实例的通道。

    按照约定好的回调接口,调用ShowHello方法,同时把参数传递过去,ICallBack中的方法留给客户端实现,服务端这里只管调用。


       你可能会遇到跨域问题,解决方法是为WCF应用程序WcfServiceTest添加跨域文件clientaccesspolicy.xml。

    clientaccesspolicy.xml内容如下:

    按 Ctrl+C 复制代码
    按 Ctrl+C 复制代码

      重新生成解决方案,否则可能会遇到发现wcf服务有误的情况,为Silverlight项目添加服务引用,点击发现可以查找到刚刚添加的ServiceTest服务,修改命名空间ServiceReferenceTest。


      在Silverlight项目的MainPage.xaml上,为了便于演示,添加一个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>
    复制代码

      添加button1的点击Click事件。

    复制代码
            private void button1_Click(object sender, RoutedEventArgs e)
    {
    //扩展的轮询机制的双向通讯
    PollingDuplexHttpBinding binding = new PollingDuplexHttpBinding()
    {
    //每次轮询建立的有效时间为20分钟
    InactivityTimeout = TimeSpan.FromMinutes(20)
    };
    //基础的http请求方式
    //Binding binding =new BasicHttpBinding();
    //svc服务地址
    EndpointAddress endPoint = new EndpointAddress("http://localhost:12345/ServiceTest.svc");
    ServiceReferenceTest.ServiceTestClient client = new ServiceReferenceTest.ServiceTestClient(binding, endPoint);
    //异步调用SayHellow方法
    client.SayHellowAsync(this.textBox1.Text);
    //调用完成后ShowHello回调事件
    client.ShowHelloReceived += new EventHandler<ServiceReferenceTest.ShowHelloReceivedEventArgs>(client_ShowHelloReceived);
    }
    复制代码

    new PollingDuplexHttpBinding();实例化一个PollingDuplex通道,PollingDuplex为双向工作轮询通信机制;

    InactivityTimeout = TimeSpan.FromMinutes(20);每次轮询建立的有效时间为20分钟;

    new EndpointAddress("http://localhost:12345/ServiceTest.svc");指定svc服务的终结点地址;

    client.SayHellowAsync(this.textBox1.Text);将textBox1的text内容作为参数,异步调用服务端的SayHellow方法;

     client.ShowHelloReceived += new EventHandler<ServiceReferenceTest.ShowHelloReceivedEventArgs> (client_ShowHelloReceived);调用服务端的SayHellow方法,方法将调用回调接口ICallBack中的 ShowHello方法,这里指定ShowHello调用完成的事件;

            void client_ShowHelloReceived(object sender, ServiceReferenceTest.ShowHelloReceivedEventArgs e)
    {
    //实现void ShowHello(string hello)方法,参数e.hello
    this.textBlock1.Text = string.Format("Hello! {0}", e.hello);
    }

    这里就是对ICallBack接口中ShowHello方法的实现,可使用参数e.hello,当客户端调用服务端的SayHello方法 时,SayHello方法中调用指定的回调接口ICallBack中的ShowHello方法,客户端完善调用到ShowHello方法完成后的事件,显 示结果Hello![e.hello],在textBlock1上;


      F5运行,在textbox1中输入名字name,点击button1,调用服务端的SayHello方法带参数name,SayHello方 法处理为Mynameis[name],作为参数再调用指定的回调接口ICallBack中的ShowHello方法,客户端实现调用ShowHello 方法完成后的事件, 处理为Hello![e.hello],后输出结果到textBlock1中。

  • 相关阅读:
    很好的学习idea工具的教程
    事件绑定
    接口出现问题
    IDEA快捷方式
    源代码编译安装Python3.5.2
    CentOS7使用无线网卡
    MySql5.7.12设置log-bin
    报表传递参数控制数据权限
    python将png转为pkm
    WebGL纹理详解——压缩纹理的使用
  • 原文地址:https://www.cnblogs.com/wangcq/p/3549773.html
Copyright © 2011-2022 走看看