zoukankan      html  css  js  c++  java
  • WCF note1

    Summary of WCF

    Client to use service

    1. use ChannelFactory to create proxy to use service. Client code show below.
    using System;
    using System.ServiceModel;
    using Artech.WcfServices.Service.Interface;
    using System.ServiceModel.Channels;
    namespace Artech.WcfServices.Client
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorservice"))
                {
                    ICalculator calculator = channelFactory.CreateChannel();
                    using (OperationContextScope contextScope = new OperationContextScope(calculator as IClientChannel))
                    {
                        string sn = "{DDA095DA-93CA-49EF-BE01-EF5B47179FD0}";
                        string ns = "http://www.artech.com/";
                        AddressHeader addressHeader = AddressHeader.CreateAddressHeader("sn", ns, sn);
                        MessageHeader messageHeader = addressHeader.ToMessageHeader();
                        OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader);
                        Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, calculator.Add(1, 2));
                    }
                }
                Console.Read();
            }
        }
    }
    

    we put our endpoing in config file

    <configuration>
      <system.serviceModel>
        <client>
          <endpoint name="calculatorservice"
                    address="http://127.0.0.1:3721/calculatorservice"
                    binding="ws2007HttpBinding"
                    contract="Artech.WcfServices.Service.Interface.ICalculator"/>
          </client>
      </system.serviceModel>
    </configuration>
    
    1. service Contract definition
    using System.ServiceModel;
    namespace Artech.WcfServices.Service.Interface
    {
        [ServiceContract(Name = "CalculatorService", Namespace ="http://www.artech.com/")]
        public interface ICalculator
        {
            [OperationContract]
            double Add(double x, double y);
        }
    }
    
    1. service definition and use host the service
    using Artech.WcfServices.Service.Interface;
    using System.ServiceModel;
    namespace Artech.WcfServices.Service
    {
        [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
        public class CalculatorService : ICalculator
        {
            public double Add(double x, double y)
            {
                return x + y;
            }
        }
    }
    

    service host

      using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
                {
                    host.Open();
                    Console.Read();
                }
    

    we put our endpoint definition in the config file
    service host config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <system.serviceModel>
            <services>
                <service name="Artech.WcfServices.Service.CalculatorService">
                  <endpoint address="http://127.0.0.1:3721/calculatorservice"
                            binding="ws2007HttpBinding"
                            contract="Artech.WcfServices.Service.Interface.ICalculator">
                    <headers>
                      <sn xmlns="http://www.artech.com/">{DDA095DA-93CA-49EF-BE01-EF5B47179FD0}</sn>
                    </headers>
                  </endpoint>
                </service>
            </services>
        </system.serviceModel>
    </configuration>
    

    Linstenuri vs uri

    1. if the listenUriMode of endpoint set to Unique, three answers: 1. if http, the listenUri will add a guid after the Uri; 2. if tcp,and portshare is disable, it will use another port to take as uri. 3. if tcp and portshare is enable, it will use the same uri but add guid after it.
  • 相关阅读:
    JavaScript定时器及相关面试题
    单线程JavaScript
    webpack基础入门
    SQL Server 服务器器信息备份(二)--权限备份
    SQL Server 服务器器信息备份(一)--login新建脚本备份
    Raid与DAN、SAN、NAS基础
    AlwaysOn可用性组功能测试(三)--其他测试
    AlwaysOn可用性组功能测试(二)--SQL Server群集故障转移对AlwaysOn可用性组的影响
    AlwaysOn可用性组功能测试(一)--AlwaysOn故障转移测试
    AlwaysOn可用性组测试环境安装与配置(一)--SQL群集环境搭建
  • 原文地址:https://www.cnblogs.com/kongshu-612/p/5554997.html
Copyright © 2011-2022 走看看