• 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
  • 相关阅读:
    马云教会我一件事:拉出来的还可以再坐回去
    使用google MAP座標搜尋改善用戶體驗
    PHP树不需要递归
    PHP操作MongoDB技術總結
    10个你可能从未用过的PHP函数
    台企招聘一名PHP程序員
    002HC32F460(华大)+Air724UG(4G GPRS)基本控制篇(阿里云物联网平台)在阿里云物联网平台上一型一密动态注册设备(HC32F460+Air724UG)
    173华大单片机HC32F460系列flash存储方案
    001HC32F460(华大)+Air724UG(4G GPRS)基本控制篇(阿里云物联网平台)C#,网页,android,微信小程序,单片机等使用MQTT接入阿里云物联网平台
    003HC32F460(华大)+Air724UG(4G GPRS)基本控制篇(阿里云物联网平台)在阿里云物联网平台上一型一密动态注册设备(Android)
  • 原文地址:https://www.cnblogs.com/bindot/p/wcf1.html
Copyright © 2020-2023  润新知