Silverlight通过TCP协议访问双工模式的WCF(Host在控制台)

简介:

Siverlight与WCF之间的通信按照理论有以下几种方式

协议  宿主 

http  console

http  IIS

tcp    console

tcp    IIS

当然还有windows服务,winform等等,这里仅举出了常见的两种。

这次测试的是silverlight使用TCP访问寄宿在控制台上的wcf服务

代码结构:

  

双工访问的关键点在于

1,比普通的WCF服务多了一个回调契约

namespace WCFLibrary
{
    [ServiceContract(CallbackContract = typeof(IUpdateClient))]   
    public interface IUpdateUser
    
{
        [OperationContract]
        WCFModel.User Update(WCFModel.User User);
    }

    [ServiceContract]
    public interface IUpdateClient
    
{
        [OperationContract(IsOneWay = true)]
        void Say(string fromServerString);
    }
}

2,app.config配置

<service behaviorConfiguration="WCFLibrary.UpdateUserBehaviorname="WCFLibrary.UpdateUser">
        <
host>
          <
baseAddresses>
            <
add baseAddress="net.tcp://localhost:4503/UpdateUser"/>
          </
baseAddresses>
        </
host>
        <
endpoint address="" binding="netTcpBindingcontract="WCFLibrary.IUpdateUserbindingConfiguration="netTcpBindConfig"></endpoint>
        <
endpoint address="mexbinding="mexTcpBindingcontract="IMetadataExchange></endpoint>
      </
service>

3,一定要在本机的IIS根目录放置一个策略文件 

 

4,Host关键代码

class Program
    
{
        static void Main(string[] args)
        {
            MyHost.Open();
            System.Console.WriteLine("服t务?已?经-启?动ˉ...   敲?任?意a键ü停£止1服t务?");
            System.Console.ReadLine();
            MyHost.Close();
        }
    }

    public class MyHost
    
{
        static ServiceHost host = null;
        public static void Open()
        {

            host = new ServiceHost(typeof(WCFLibrary.UpdateUser));
            host.Open();

            host = new ServiceHost(typeof(WCFLibrary.AddService));
            host.Open();
        }
        public static void Close()
        {
            if (host != null && host.State == CommunicationState.Opened)
            {
                host.Close();
            }
            host = null;
        }
    } 




     本文转自wengyuli 51CTO博客,原文链接:http://blog.51cto.com/wengyuli/586759,如需转载请自行联系原作者


相关文章
|
网络协议 安全 Windows
WCF如何绑定netTcpBinding寄宿到控制台应用程序详解
新建一个WCF服务类库项目,在其中添加两个WCF服务:GameService,PlayerService
411 0
WCF如何绑定netTcpBinding寄宿到控制台应用程序详解
|
网络协议
TCP协议三次握手的执行流程,tcp的交互模式
TCP协议三次握手的执行流程,tcp的交互模式
267 0
|
Web App开发 网络协议 网络架构
揭秘TCP/IP协议:分层模式
TCP/IP与互联网 咱做技术还是得踏实,越往后走,越多的是建立下扎实的基础知识之上的。 凡事欲速则不达,一步步慢慢来,不积硅步无以至千里。
2001 0
|
开发者 开发工具 定位技术

热门文章

最新文章