zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    当Django模型迁移时,报No migrations to apply 问题时
    django--各个文件的含义
    django--创建项目
    1013. Battle Over Cities (25)
    1011. World Cup Betting (20)
    1009. Product of Polynomials (25)
    1007. Maximum Subsequence Sum (25)
    1006. Sign In and Sign Out (25)
    1008. Elevator (20)
    1004. Counting Leaves (30)
  • 原文地址:https://www.cnblogs.com/bindot/p/wcf1.html
Copyright © 2011-2022 走看看