Silverlight+WCF 新手实例 象棋 WCF通讯轮询(十七)

阅读: 评论:0

Silverlight+WCF 新手实例 象棋 WCF通讯轮询(十七)

Silverlight+WCF 新手实例 象棋 WCF通讯轮询(十七)

上节说到:

关于双向通讯,官方提供了N种可以双向的,不过今天要用到的,

是pollingDuplexHttpBinding,一个扩展的轮询机制的双向通讯,当你也可以尝试用上面的通讯方式去试一试。

既然是扩展,就说明默认没有,那我们首先就要添加扩展了,用的是默认官方是提供的DLL,就在我们安装的Silverlight4的目录里:

正常路径为:C:Program FilesMicrosoft SDKsSilverlightv4.0LibrariesServerSystem.ServiceModel.PollingDuplex.dll

 

这一节我们来实现PollingDuplexHttpBinding,轮询机制的双向通讯。

 

以下开始内容不上图片,参考Silverlight+WCF 新手实例 象棋 WCF通讯跨域(十五)

我们再开一个项目来讲解,有了Hellow,有了World,这节就叫HellowWorld

文件—》新建->项目-》Silverlight应用程序-》起名叫:HellowWorld

确定后还是:HellowWorld和HellowWorld.web应用程序,两个项目

 

我们对着解决方案右键,添加新建项目:建立WCF 服务应用程序->输入名称为:HellowWorldService

接着我们把默认的Service1.cs和Service1.svc删除:

删除后,我们新建一个新的服务,叫Service.svc

我们提前修改下服务的端口号为12321,这样添加服务引用后,不用再改配置文件的端口。

OK,这时项目情况如下:

接下来我们要为项目添加DLL,对着项目引用右键-》添加引用:

选择浏览,并定位到:C:Program FilesMicrosoft SDKsSilverlightv4.0LibrariesServerSystem.ServiceModel.PollingDuplex.dll

回车确定,添加引用完后,我们需要修改下服务的配置文件“fig”

由于轮询为扩展的,所以需要在配置文件里添加两个节点:

 

fig <? 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 ="HellowWorldService.Service"   >
               < endpoint  address =""  binding ="pollingDuplexHttpBinding"  contract ="HellowWorldService.IService"   />
               < 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 >

 

大伙照着把添加的开始和添加的结束那段Copy过去就完事了。

OK,配置完事后,我们要写服务端代码了,双向通讯,但然少不了要调用客户端的代码了。

看们看下IService.cs文件

[ServiceContract(CallbackContract = typeof (ICallBack))]
     public   interface  IService
    {
        [OperationContract(IsOneWay  =   true )]
         void  SayHellow( string  name);
    }
     public   interface  ICallBack
    {
        [OperationContract(IsOneWay = true )]
         void  ShowWorld( string  worldName);
    }

 

看,接口代码相当的少,就算少还是要说明一下的:

而且你敲属性的时候,是有智能提示的:

IService:服务端接口,当然就是客户端调用了。注意头顶上那个CallbackContract=typeof(ICallBack),这里指定了回调接口。

ICallBack:回调接口,我新加的,是给客户端实现,然后服务端调用。这个名称你可以随便起,和typeof里的对应上就行了。

是不是发现多了一个(IsOneWay = true)属性,什么意思?就是单向调用,不需要返回值。

所以官方推荐,如果你的函数类型返回值为void时,最好加上。

 

接着我们要实现IService接口的方法了,那ICallBack要不要实现?当然不要,都说留给客户端实现了。

哦,那我们就实现IService接口方法去了:

public   class  Service : IService
    {
         #region  IService 成员
         public   void  SayHellow( string  name)
        {
            name = " hellow " + name;
            ICallBack callBack  =  OperationContext.Current.GetCallbackChannel < ICallBack > ();
            callBack.ShowWorld(name);
        }
         #endregion
    }

 

看到方法没有,有一句很长的代码,来获取ICallBack接口,然后调用了那个ShowWorld方法了。

这个代码记死也行:OperationContext.Current.GetCallbackChannel<ICallBack>();

反正把ICallBack换成你自己的接口名称,就是了。然后就可以调用了。

话说ICallBack方法是留给客户端实现的,我们服务端这里先调用着先,反正你得按接口实现,按接口办事,放心的很。

 

那三行代码总来来说就是:

1。客户端调用服务端的SayHellow(传入“路过秋天”);

2。服务端收到调用,自然会知道对方从哪条路上来的,所以能够GetCallbackChannel了。

3。接约定好的接口,我调用了你的ShowWorld方法,同时把加了“hellow:路过秋天“传过去。

至此,服务端代码写完了。是不是相当相当的简单,只要理解好了。

不过服务端还是有点事,什么事?加那个跨域文件啊,谁让你独立一个服务出来。

加就加了,还是新建一个&#l文件,内容为:

<? xml version="1.0" encoding="utf-8" ?>
< access-policy >
     < cross-domain-access >
         < policy >
             < allow-from  http-request-headers ="*" >
                 < domain  uri ="*" />
             </ allow-from >
             < grant-to >
                 < resource  path ="/"  include-subpaths ="true" />
             </ grant-to >
         </ policy >
     </ cross-domain-access >
</ access-policy >

 

这下服务端事件就全搞完了,接下来看客户端的了。

记得先添加服务引用-》发现->引用名称叫:HellowWorldService

OK,接着我们还是要弄一个和上两次一样的界面,来调用,从上节那里Copy来xaml的代码,-_-..这界面重复三次了:

< Grid  x:Name ="LayoutRoot"  Background ="White" >
         < Button  Content ="WCF 调用"  Height ="23"  HorizontalAlignment ="Left"  Margin ="84,111,0,0"  Name ="btnCallWCF"  VerticalAlignment ="Top"  Width ="75"  Click ="btnCallWCF_Click"   />
         < TextBox  Height ="23"  HorizontalAlignment ="Left"  Margin ="84,71,0,0"  Name ="txtName"  VerticalAlignment ="Top"  Width ="120"   />
         < TextBlock  Height ="23"  HorizontalAlignment ="Left"  Margin ="228,71,0,0"  Name ="tbMsg"  Text ="显示的内容"  VerticalAlignment ="Top"   />
     </ Grid >

 

后台代码调用除了差不多也就是有一点小变化:

我们不是实例一个BasicHttp通道了,而是实例化一个PollingDuplex通道了。并设置了下每次轮询建立的有效时间为20分钟。

  private   void  btnCallWCF_Click( object  sender, RoutedEventArgs e)
        {
            PollingDuplexHttpBinding binding  =   new  PollingDuplexHttpBinding()
            {
                InactivityTimeout  =  TimeSpan.FromMinutes( 20 )
            };
             // Binding binding =new BasicHttpBinding();
            EndpointAddress endPoint  =   new  EndpointAddress( " localhost:12321/Service.svc " );
            HellowWorldService.ServiceClient client  =   new  HellowWorldService.ServiceClient(binding, endPoint);
            client.SayHellowAsync(txtName.Text);

            client.ShowWorldReceived  +=   new  EventHandler < HellowWorldService.ShowWorldReceivedEventArgs > (client_ShowWorldReceived);
        }

         void  client_ShowWorldReceived( object  sender, HellowWorldService.ShowWorldReceivedEventArgs e)
        {
            tbMsg.Text  =  e.worldName;
        }

 

客户端的接口实现是哪句啊?

看出来没,这两句就是那个ICallBack接口的实现了,当用户调用ShowWorld时候,就是tbMsg.Text=e.参数的时候了。

 client.ShowWorldReceived  +=   new  EventHandler < HellowWorldService.ShowWorldReceivedEventArgs > (client_ShowWorldReceived);
        }

         void  client_ShowWorldReceived( object  sender, HellowWorldService.ShowWorldReceivedEventArgs e)
        {
            tbMsg.Text  =  e.worldName;
        }

 

一切就绪:F5运行,输入"路过秋天"

回车调用,OK,结果出来了。

 

OK,WCF通讯基础到此就结束了,下节开始大干特干的应用于了。

提供源码下载:点击下载

 

版权声明:本文原创发表于博客园,作者为路过秋天,原文链接:

.html

本文发布于:2024-01-31 01:44:23,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170663666424435.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:象棋   实例   新手   通讯   Silverlight
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23