代码改变世界

WCF中tcp简单配置

2019-03-25 21:11  NONONONONOA  阅读(349)  评论(0编辑  收藏  举报
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyWCFServicebehavior">
          <serviceDebug httpHelpPageEnabled="false"/>
          <serviceMetadata httpGetEnabled="false"/>
          <serviceTimeouts transactionTimeout="00:10:00"/>
          <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000"/>
        </behavior>
        <!--<behavior name="CalculatorServicebehavior">
          <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>
      <service name="MyWCF.Service.MyWCFService" behaviorConfiguration="MyWCFServicebehavior">
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:11111/CalculatorService"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="tcpbinding" contract="MyWCF.Interfac.IMyWCF"/>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
      </service>
      <!--<service name="SOA.WCF.Service.MathService" behaviorConfiguration="MathServicebehavior">
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:11111/MathService"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="tcpbinding" contract="SOA.WCF.Interface.IMathService"/>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
      </service>-->
    </services>
    <!--<behaviors>
      <serviceBehaviors>
        <behavior name="MathServicebehavior">
          <serviceDebug httpHelpPageEnabled="false"/>
          <serviceMetadata httpGetEnabled="false"/>
          <serviceTimeouts transactionTimeout="00:10:00"/>
          <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="httpbinding"/>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="SOA.WCF.Service.MathService" behaviorConfiguration="MathServicebehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:11113/MathService"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="httpbinding" contract="SOA.WCF.Interface.IMathService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>-->

  </system.serviceModel>

下面展示代码接口代码:

  [ServiceContract]
    public interface IMyWCF
    {
        [OperationContract]
        void Show();
    }

展示实现类代码:

 public class MyWCFService : IMyWCF
    {
        public void Show()
        {
            Console.WriteLine("调用显示方式");
        }
    }

端口激活

   List<ServiceHost> hosts = new List<ServiceHost>()
            {
                new ServiceHost(typeof(MyWCFService))
            };
            foreach (var host in hosts)
            {
                host.Opened += (s, e) => Console.WriteLine($"服务端已经打开:{host.GetType().Name}");
                host.Opening += (s, e) => Console.WriteLine($"服务端正在打开:{host.GetType().Name}");
                host.Open();
            }

实体信息:特性类名称[DataContract]。属性特性:DataMember