[Programming WCF Services]Chapter 1. WCF Essentials - Metadata Exchange

Posted Silly Programer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Programming WCF Services]Chapter 1. WCF Essentials - Metadata Exchange相关的知识,希望对你有一定的参考价值。

1.HTTP-GET WCF 方式

通过Http的方式提供metadata

1.1.配置文件方式

<system.serviceModel>
  <services>
    <service name = "MyService" behaviorConfiguration = "MEXGET">
      <host>
        <baseAddresses>
          <add baseAddress = "http://localhost:8000/"/>
        </baseAddresses>
      </host>
      ...
    </service>

    <service name = "MyOtherService" behaviorConfiguration = "MEXGET">
      <host>
        <baseAddresses>
          <add baseAddress = "http://localhost:8001/"/>
        </baseAddresses>
      </host>
      ...
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name = "MEXGET">
        <serviceMetadata httpGetEnabled = "true"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

 一般通过HTTP的基地址即可以访问元数据,也可以指定不同的地址:

<behavior name = "MEXGET">
  <serviceMetadata httpGetEnabled = "true" httpGetUrl = "MyMEXAddress"/>
</behavior>

 

 

1.2.代码方式

ServiceHost host = new ServiceHost(typeof(MyService));
ServiceMetadataBehavior metadataBehavior;
metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if(metadataBehavior == null)
{
Debug.Assert(BaseAddresses.Any(baseAddress=>baseAddress.Uri.Scheme == "http"));
metadataBehavior = new ServiceMetadataBehavior();
metadataBehavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(metadataBehavior);
}
host.Open();

 

 

2.Metadata Exchange Endpoint

平台无关的元数据交换方式,支持多种协议

<services>
  <service name = "MyService" behaviorConfiguration = "MEX">
    <host>
      <baseAddresses>
        <add baseAddress = "net.tcp://localhost:8001/"/>
        <add baseAddress = "net.pipe://localhost/"/>
      </baseAddresses>
    </host>
    <endpoint
    address = "MEX"
    binding = "mexTcpBinding"
    contract = "IMetadataExchange"
/>
    <endpoint
    address = "MEX"
    binding = "mexNamedPipeBinding"
    contract = "IMetadataExchange"
/>
    <endpoint
    address = "http://localhost:8000/MEX"
    binding = "mexHttpBinding"
    contract = "IMetadataExchange"
/>
    ...
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name = "MEX">
      <serviceMetadata/>
    </behavior>
  </serviceBehaviors>
</behaviors>

 

以上是关于[Programming WCF Services]Chapter 1. WCF Essentials - Metadata Exchange的主要内容,如果未能解决你的问题,请参考以下文章

How To Easily Call WCF Services Properly z

[转]WCF Data Services OData

如何消费WCF Data Services定义的服务操作

Windows authentication for WCF web services error

使用 Amazon Web Services (EC2) 和 c# Windows Service/WCF 进行远程调试

如何在 WPF 应用程序中使用 WCF RIA SERVICES?