WCF两种方式

http://www.ilanever.com/article/sitedetail.html?id=164

1. 显示添加服务行为,为服务自动提供基于HTTP-GET的元数据。2. 采用元数据交换终结点发布元数据。
目录
 

1. 显示添加服务行为,为服务自动提供基于HTTP-GET的元数据。

一旦启用了基于HTTP-GET的元数据交换,在浏览器中就可以通过HTTP基地址进行访问。如果一切正确,就会获得一个服务帮助页面。

为服务添加服务行为可以采用配置方式,也可以采用编程方式。

采用配置方式:

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

采用编程方式:

ServiceHost host = new ServiceHost(typeof(MyService),new Uri("http://localhost:10000"));

ServiceMetadataBehavior metadataBehavior;
metadataBehavior = host.Description.Behaviors.Find();
if(metadataBehavior == null)
{
    metadataBehavior = new ServiceMetadataBehavior();
    metadataBehavior.HttpGetEnabled = true;
    host.Description.Behaviors.Add(metadataBehavior);
}

host.Open();

2. 采用元数据交换终结点发布元数据。

采用MEX终结点发布元数据,没有必要启用HTTP-GET选项,但是即使启用了也不会造成影响。

元数据交换终结点采用的契约为IMetadataExchange,WCF自动为服务宿主提供了该接口的实现。

采用配置方式:

<system.ServiceModel>
    <services>
        <service name="MyService" behaviorConfiguration = "MEXGET">
            <host>
                <baseAddresses>
                    <add baseAddress = "net.tcp://localhost:10000"/>
                    <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:10001/Mex"
                binding = "mexHttpBinding"
                contract = "IMetadataExchange"
            />
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name = "MEX">
                <serviceMetadata/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.ServiceModel>

采用编程方式:

BindingElement bindingElement = new TcpTransportBindingElement();
CustomBinding binding = new CustomBinding(bindingElement);

Uri tcpBaseAddress = new Uri("net.tcp://localhost:10000");
ServiceHost host = new ServiceHost(typeof(MyService));

ServiceMetadataBehavior metadataBehavior;
metadataBehavior = host.Description.Behaviors.Find();
if(metadataBehavior == null)
{
    metadataBehavior = new ServiceMetadataBehavior();
    host.Description.Behaviors.Add(metadataBehavior);
}
host.AddServiceEndpoint(typeof(IMetadataExchange),binding,"MEX");
host.Open();
原文地址:https://www.cnblogs.com/thaughtZhao/p/4363438.html