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

协议  宿主 

http  console

http  IIS

tcp    console

tcp    IIS

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

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

代码结构:

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

双工访问的关键点在于

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根目录放置一个策略文件

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

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;
        }
    }
 
源代码:https://files.cnblogs.com/wengyuli/WCFTCPDuplex.rar

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-28
  • 2022-12-23
  • 2021-09-30
  • 2021-08-23
  • 2021-07-18
猜你喜欢
  • 2022-03-10
  • 2021-11-09
  • 2022-01-17
  • 2021-07-30
  • 2021-07-21
  • 2021-11-20
相关资源
相似解决方案