• WCF:初识


    结构:

     1 using System.ServiceModel;
     2 namespace MyServices
     3 {
     4     [ServiceContract]
     5     public interface IHomeService
     6     {
     7         [OperationContract]
     8         int GetLength(string name);
     9     }
    10 }
    契约
     1 namespace MyServices
     2 {
     3     public class HomeService:IHomeService
     4     {
     5         public int GetLength(string name)
     6         {
     7             return name.Length;
     8         }
     9     }
    10 }
    实现类
     1 using System;
     2 using System.ServiceModel;
     3 
     4 namespace MyServices
     5 {
     6     class Program
     7     {
     8         static void Main(string[] args)
     9         {
    10             using (ServiceHost host = new ServiceHost(typeof(HomeService)))
    11             {
    12                 try
    13                 {
    14                     host.Open();
    15                     Console.WriteLine("服务开启!");
    16                     Console.Read();
    17                 }
    18                 catch (Exception e)
    19                 {
    20                     Console.WriteLine(e.Message);
    21                 }
    22             }
    23         }
    24     }
    25 }
    服务启动
     1 using System;
     2 namespace ConsoleApplication1
     3 {
     4     class Program
     5     {
     6         static void Main(string[] args)
     7         {
     8             HomeServiceReference.HomeServiceClient client = new HomeServiceReference.HomeServiceClient();
     9             //HomeServiceClient homeServiceClient = new HomeServiceClient();
    10             var r = client.GetLength("abc");
    11             Console.WriteLine(r);
    12             Console.ReadKey();
    13         }
    14     }
    15 }
    调用

    配置:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <bindings>
          <netTcpBinding>
            <binding name="IHomeServiceBinding" />
          </netTcpBinding>
        </bindings>
    
        <behaviors>
          <serviceBehaviors>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
    
        <services>
          <service name="MyService.HomeService">
            <endpoint address="http://127.0.0.1:1920/HomeService" binding="basicHttpBinding" contract="MyService.IHomeService">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
    
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://127.0.0.1:1920"/>
              </baseAddresses>
            </host>
          </service>
        </services>
    
      </system.serviceModel>
    </configuration>
    basicHttpBinding
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="mxbehavior">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
    
        <services>
          <service name="MyService.HomeService" behaviorConfiguration="mxbehavior">
            <endpoint address="net.tcp://localhost:1921/HomeService" binding="netTcpBinding" contract="MyService.IHomeService">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
    
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:1921/HomeService"/>
              </baseAddresses>
            </host>
          </service>
        </services>
    
      </system.serviceModel>
    </configuration>
    netTcpBinding
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="mxbehavior">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <bindings>
          <netMsmqBinding>
            <binding name="msmqbinding">
              <security mode="None"/>
            </binding>
          </netMsmqBinding>
        </bindings>
        <services>
          <service name="MyService.HomeService" behaviorConfiguration="mxbehavior">
            <endpoint address="net.msmq://localhost/private/homequeue" binding="netMsmqBinding"
                      contract="MyService.IHomeService" bindingConfiguration="msmqbinding">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
    
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:1922/HomeService"/>
              </baseAddresses>
            </host>
          </service>
        </services>
    
      </system.serviceModel>
    </configuration>
    netMsmqBinding
  • 相关阅读:
    POJ_2104_K-th Number_主席树
    BZOJ_1014_[JSOI2008]火星人prefix_splay+hash
    BZOJ_1861_[Zjoi2006]Book 书架_splay
    BZOJ_2242_[SDOI2011]计算器_快速幂+扩展GCD+BSGS
    BZOJ_3239_Discrete Logging_BSGS
    BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor
    BZOJ_1552_[Cerc2007]robotic sort_splay
    BZOJ_1500_[NOI2005]维修数列_splay
    BZOJ_1251_序列终结者
    吴裕雄--天生自然ORACLE数据库学习笔记:优化SQL语句
  • 原文地址:https://www.cnblogs.com/bindot/p/wcf1.html
Copyright © 2020-2023  润新知