.NET WCF简单实现

  1. Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口

服务端设置步骤

  1. 以寄宿WEB服务的WCF为例,即通过WEB提供WCF服务;
  2. 添加——新建项——WCF服务,就会新增svc文件并且同时新增一个名称以I开头的接口文件;
  3. 找到同步生成的接口文件,新增若干方法,以便客户端调用,其中新增方法要加特性:[OperationContract],即操作契约,否则客户端此方法不可见,接口名称上默认也有特性[ServiceContract],即服务契约,以上特性不可删除;
  4. 还有数据契约[DataContract],用于服务端返回数据实体时,在类名称上定义的特性,同时属性还须有特性[DataMember]
  5. 双击svc文件(CodeBehind指向对应的cs文件),实现接口里新增的方法;
  6. 右键svc文件,选在浏览器中查看,复制URL等会在客户端要用,就完成了服务端的设置及启动;
   //自动生成的接口文件,在这里新增方法
   [ServiceContract]
   public interface IMyWCF1
   {
       [OperationContract]
       void DoWork();

       [OperationContract] //新增一个乘法
       int Multiply(int x, int y);
   }
    public class MyWCF1 : IMyWCF1
    {
        public void DoWork()
        {
        }
        public int Multiply(int x, int y)    //实现乘法
        {
            return x * y;
        }
    }

客户端调用

  1. 引用——添加服务引用,输入服务端配置文件里的地址;
    添加服务引用
  2. 主函数里实例对象,调用对象的方法,即完成调用
 static void Main(string[] args)
        {
            /推荐的服务端对象创建赋值,关闭写法
            MyWCFClient1.MyWCF1Client client= null;
            try
            {
                client = new MyWCFClient1.MyWCF1Client();
                int n= client.Multiply(10, 4);
                Console.WriteLine(n);
                client.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                if (client!=null)
                {
                    client.Abort();
                }
            }
            Console.ReadKey();
        }

寄宿到Win Service

  1. 简单描述一下;
  2. 先新增接口以及实现接口的类,同样接口以及方法均需要加特性;
  3. new ServiceHost(typeof(实现接口的类)).Open()启动服务,如果有多个wcf服务,可以用List并循环启动;
  4. 修改配置文件,新增一下内容
<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
      
      	<!---->
        <behavior name="MyWCFDemobehavior">
          <serviceDebug httpHelpPageEnabled="false"/>
          <serviceMetadata httpGetEnabled="false"/>
          <serviceTimeouts transactionTimeout="00:10:00"/>
          <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

	<!---->
    <bindings>
      <netTcpBinding>
        <binding name="tcpbinding">
          <security mode="None">
            <transport clientCredentialType="None" protectionLevel="None"/>
          </security>
        </binding>
      </netTcpBinding>
    </bindings>

	<!---->
    <services>
      <!--类全名,behaviorConfiguration名称与上面一致-->
      <service name="WCF.Service.IMyWCFDemo" behaviorConfiguration="MyWCFDemobehavior">
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:11111/WCFService"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="tcpbinding" contract="WCF.Interface.IMyWCFDemo"/>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
原文地址:https://www.cnblogs.com/zoulei0718/p/14315579.html