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

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

协议  宿主 

http  console

http  IIS

tcp    console

tcp    IIS

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

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

代码结构:

image

双工访问的关键点在于

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.UpdateUserBehavior" name="WCFLibrary.UpdateUser">
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:4503/UpdateUser"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="netTcpBinding" contract="WCFLibrary.IUpdateUser" bindingConfiguration="netTcpBindConfig"></endpoint>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" ></endpoint>
      </service>

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

image

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;
        }
    }
 
源代码:http://files.cnblogs.com/wengyuli/WCFTCPDuplex.rar
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值