zoukankan      html  css  js  c++  java
  • WCF(客户端与服务)

     

    项目一:类库项目。定义服务,code实现服务接口的类。[如上图的Service]

    namespace CodesContract

    {

        [ServiceContract(Name = "CodeService", Namespace = "http://www.rxm.net")]

        public interface ICodeContract

        {

            [OperationContract]

            string GetName(string name);

        }

     

        public class CodeContract : ICodeContract

        {

     

            #region ICodeContract 成员

     

            public string GetName(string name)

            {

                return "My name is " + name;

            }

     

            #endregion

        }

    }

    项目二:控制台项目。服务寄宿的宿主,公布终结点。[如上图ServideHost,ABC]

    namespace CodesHost

    {

        class Program

        {

            static void Main(string[] args)

            {

                using (ServiceHost host = new ServiceHost(typeof(CodesContract.CodeContract)))

                {

                    host.AddServiceEndpoint(typeof(CodesContract.ICodeContract), new NetTcpBinding(), "net.Tcp://localhost:806/CodesName");

                    host.Open();

                    Console.Read();

                }

            }

        }

    }

    项目三:控制台项目。定义与服务端相同的服务,终结点地址要与服务终结点地址和通信协议相同。[如上图左边]

    namespace CodesClient

    {

        [ServiceContract(Name = "CodeService", Namespace = "http://www.rxm.net")]

        public interface ICodeContract

        {

            [OperationContract]

            string GetName(string name);

        }

     

        class Program

        {

            static void Main(string[] args)

            {

                ICodeContract proxy = ChannelFactory<ICodeContract>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.Tcp://localhost:806/CodesName"));

                string name = proxy.GetName("rxm");

                Console.WriteLine(name);

                Console.ReadKey();

            }

        }

    }

  • 相关阅读:
    存储过程的设计规则
    企业管理器里删除不需要的注册
    SQL Server 大数据量插入和索引关系
    【2011520】无法使用主机名连接数据库
    SQL Server 查看存储过程
    SQL Server dbcc inputbuffer
    如何选择行版本的隔离级别
    ObjectiveC中Selector基本概念和操作
    Objectivec的@property 详解
    objectivec 关键字和概念
  • 原文地址:https://www.cnblogs.com/hometown/p/2829584.html
Copyright © 2011-2022 走看看