zoukankan      html  css  js  c++  java
  • WCF技术剖析 Two

    WCF终结点和寻址之--AddressHead信息匹配代码

    Contracts契约

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.ServiceModel;
     6 
     7 namespace Artech.WcfServices.Contracts
     8 {
     9     [ServiceContract(Name="CalculatorService",Namespace="http://www.artech.com/")]
    10     public interface ICalculator
    11     {
    12         
    13         [OperationContract]
    14         double Add(double x, double y);
    15 
    16         [OperationContract]
    17         double Subtract(double x, double y);
    18         
    19         [OperationContract]
    20         double Multiply(double x, double y);
    21 
    22         [OperationContract]
    23         double Divide(double x, double y);
    24     }
    25 }
    View Code

    Services服务,

    完全匹配属性:[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using Artech.WcfServices.Contracts;
     6 using System.ServiceModel;
     7 
     8 namespace Artech.WcfServices.Services
     9 {
    10     [ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]
    11     public class CalculatorService : ICalculator
    12     {
    13         public double Add(double x, double y)
    14         {
    15             return x + y;
    16         }
    17 
    18         public double Subtract(double x, double y)
    19         {
    20             return x - y;
    21         }
    22 
    23         public double Multiply(double x, double y)
    24         {
    25             return x * y;
    26         }
    27 
    28         public double Divide(double x, double y)
    29         {
    30             return x / y;
    31         }
    32     }
    33 }
    View Code

    Hosting主机注册,

    需要匹配的信息

    string headevalue = "licensed user";
    string headerNmae = "userType";
    string headNamespace = "http://www.artech.com/";

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.ServiceModel;
     6 using System.ServiceModel.Description;
     7 using Artech.WcfServices.Contracts;
     8 using Artech.WcfServices.Services;
     9 using System.ServiceModel.Channels;
    10 
    11 namespace Artech.WcfServices.Hosting
    12 {
    13     class Program
    14     {
    15         static void Main(string[] args)
    16         {
    17             using (ServiceHost servicehost = new ServiceHost(typeof(CalculatorService),
    18                 new Uri("http://127.0.0.1:9999/CalculatorService")))
    19             {
    20                 string headevalue = "licensed user";
    21                 string headerNmae = "userType";
    22                 string headNamespace = "http://www.artech.com/";
    23 
    24                 AddressHeader header = AddressHeader.CreateAddressHeader(headerNmae, headNamespace, headevalue);
    25 
    26                 EndpointAddress endpointAddress = new EndpointAddress
    27                     (new Uri("http://127.0.0.1:9999/CalculatorService"), header);
    28 
    29                 ServiceEndpoint serviceEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(ICalculator)), new
    30                 WSHttpBinding(), endpointAddress);
    31 
    32                 servicehost.Description.Endpoints.Add(serviceEndpoint);
    33                 
    34                 ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
    35                 behavior.HttpGetEnabled = true;
    36                 servicehost.Description.Behaviors.Add(behavior);
    37 
    38                 servicehost.Opened += delegate
    39                 {
    40                     Console.WriteLine("Service begin to listen via the Address:{0}",
    41             servicehost.BaseAddresses[0].ToString());
    42                 };
    43 
    44                 servicehost.Open();
    45 
    46                 Console.ReadLine();
    47 
    48             }
    49     
    50         }
    51     }
    52 }
    View Code

    客户端调用

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <client>
          <endpoint name="CalculatorService" address="http://127.0.1.1:9999/CalculatorService" binding ="wsHttpBinding"
                    contract="Artech.WcfServices.Contracts.ICalculator" >
            <headers>
              <userType xmlns="http://www.artech.com/">chenJian</userType>
            </headers>
          </endpoint>
        </client>
      </system.serviceModel>
    </configuration>
    View Code

    客户端-App.Config配置

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using Artech.WcfServices.Contracts;
     6 using System.ServiceModel;
     7 using System.ServiceModel.Channels;
     8 
     9 namespace ClientConsole
    10 {
    11     class Program
    12     {
    13         static void Main(string[] args)
    14         {
    15             using (ChannelFactory<ICalculator> channelFactory = new
    16                 ChannelFactory<ICalculator>("CalculatorService"))
    17             {
    18                 ICalculator proxy = channelFactory.CreateChannel();
    19                 using (proxy as IDisposable)
    20                 {
    21                    Console.WriteLine("x+y={0}", proxy.Add(1, 2));
    22                     Console.WriteLine("x-y={0}", proxy.Subtract(1, 2));
    23 
    24 
    25 
    26                     Console.ReadLine();
    27 
    28                 }
    29             }
    30         }
    31     }
    32 }
    View Code
  • 相关阅读:
    博客阅读计数优化
    博客阅读简单计数
    博客后台富文本编辑
    博客分类统计
    Django关联关系查询
    上下篇博客,按月归档
    浅谈闭包以及常见面试题
    浅谈前端缓存(转至大佬)
    post请求头的常见类型
    浅谈RegExp 对象的方法
  • 原文地址:https://www.cnblogs.com/chinaagan/p/3341780.html
Copyright © 2011-2022 走看看