惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 牛奶哥

C#点滴 - 深拷贝与浅拷贝 C#点滴 - 抽象类与接口区别 C#点滴 - ?与?? C#点滴 - 值类型与引用类型 C#点滴 - 类型转换 - 牛奶哥 C#点滴 - 关于String C#点滴 – 内建基本类型 C#点滴 – CLR, CTS…等等基本概念 - 牛奶哥 SharePoint 2010 学习资料 初学SilverLight - (3)简单布局 - 牛奶哥 初学SilverLight - (2)第一个SilverLight程序 初学SilverLight - (1)开发环境准备 WCF热带鱼书学习手记 - Security (1) 概述 关于在SharePoint中管理大文件的一些想法 WCF热带鱼书学习手记 - Service Contract Overload - 牛奶哥 WCF热带鱼书学习手记 - client coding - 牛奶哥 WCF热带鱼书学习手记 - endpoint - 牛奶哥 WCF热带鱼书学习手记 - ABC - 牛奶哥 WCF热带鱼书学习手记 - Host Type - 牛奶哥
WCF热带鱼书学习手记 - metadata
牛奶哥 · 2009-12-23 · via 博客园 - 牛奶哥

WCF – MetaData

WCF的metadata描述了客户端如何同服务进行交互。通过metadata,我们可以利用一些工具生成proxy代码,比如SvcUtil.exe,客户端编程基本上是基于这些proxy的

服务有两种方案可以发布自己的元数据。一种是基于HTTP-GET协议提供元数据;另一种则为元数据交换方式,它往往使用一个专门的终结点,称之为元数据交换终结点。元数据交换终结点与其它终结点相似,仍然包含了地址、绑定与契约,但是使用的服务契约为WCF提供的接口IMetadataExchange。


实际上,这两种发布元数据的方式代表了它使用了两种不同的标准协议,前者为HTTP/GET请求,后者为WS-MetadataExchange(MEX)。在WCF,以MetadataExchangeClientMode枚举类型表示这两种元数据交换模式:

public enum MetadataExchangeClientMode
{
    MetadataExchange,
    HttpGet
} 

1. HTTP-GET

起用了这种方式的metadata发布,客户端可以通过浏览器察看并确认元数据。同前面几篇文章介绍的一样,启用方法也是2种:编成和配置。

配置元数据发布是通过添加ServiceBehavior来实现的,例子如下:

<system.serviceModel>
    <services>
      <service name="MyNamespace.MyService" behaviroConfiguration="MEXGET">
        <endpoint contract = "MyNamespace.IMyService"
                  binding  = "wsHttpBinding"
                  address  = "http://localhost:8000/MyService"
        />
      </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MEXGET">
                <serviceMetadata httpGetEnabled="true"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

编程方式:

ServiceHost host = new ServiceHost(typeof(MyService));
ServiceMetadataBehavior metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if(metadataBehavior == null)
{
    metadataBehavior = new ServiceMetadataBehavior();
    metadataBehavior.HttpGetEnabled = true;
    host.Description.Behaviors.Add(metadataBehavior);
}
Binding wsHttpBinding = new WSHttpBinding();
host.AddServiceEndpoint(typeof(IMyService),
                        wsHttpBinding,
                        new Uri("http://localhost:8086/MyService/"));
host.Open();
...            
host.Close();

2. Metadata-Exchange

这种方式是通过定义特定的endpoint的方式来发布元数据,这种endpoint被称作元数据终结点或者MEX终结点。WCF为MEX终结点的ABC分别给出了各自的定义。

  • A - 类似于普通的endpoint地址。
  • B - 基于http, https, tcp和icp协议的binding。例如,mexTcpBinding, mexNamePipeBinding, mexHttpBinding….
  • C - IMetadataExchange接口。其实现由WCF自动提供。

以下是一个配置Mex endpoint的例子:

<system.serviceModel>
    <services>
      <service name="MyNamespace.MyService" behaviroConfiguration="MEX">
        <endpoint contract = "MyNamespace.IMyService"
                  binding  = "wsHttpBinding"
                  address  = "http://localhost:8000/MyService"
        />
        <endpoint contract = "IMetadataExchange"
                          binding  = "mexHttpBinding"
                          address  = "http://localhost:8000/MEX"
        />
      </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MEX">
                <serviceMetadata/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

同样可以通过编程的方式来启动mex endpoint:

BindingElement bindingElement = new TcpTransportBindingElement();
CustomBinding binding = new CustomBinding(bindingElement);
Uri tcpBaseAddress = new Uri("net.tcp://localhost:9000");
ServiceHost host = new ServiceHost(typeof(MyService), tcpBaseAddress);
if(metadataBehavior == null)
{
    metadataBehavior = new ServiceMetadataBehavior();
    host.Description.Behaviors.Add(metadataBehavior);
}
host.AddServiceEndpoint(typeof(IMetadataExchange), binding, "MEX");
host.Open();
...            
host.Close();