DotNet关键知识点——WCF篇(二)

服务侧的端点定义。

1. 端点定义

WCF端点(Endpoint)包含ABC三个方面,对应endpoint记号中的三个属性:
地址(属性address)遵照WS-Addressing标准,由方式(Scheme)、主机(Machine)、端口(Port)、路径(Path)四个部分组成,依照URI的形式。

在服务宿主(Service Host)工程中包含app.config,用以定义服务的端点等信息,通常结构包含服务定义(services),绑定定义(bindings)和行为定义(behaviors)三部分。

<system.serviceModel>
    <services>
        <service name="服务类名" behaviorCofiguration="行为配置名"
            <host>
                (在IIS中基路径即存放服务文件的虚拟目录,此处示例添加基路径) 
                <baseAddress>
                    <add baseAddress = "…">
                    …
                </baseAddress>
            </host>
            <endpoint binding="wsHttpBinding(绑定类型名)"
                      contract="名空间.服务接口名
                      bindingConfiguration="绑定设置名"/> 
           
<endpoint address="mex"
                      binding="mexHttpBinding"        (其他方式包括mexNamedPipeBinding, mexTcpBinding等)
                      contract="IMetadataExchange" /> (元数据传递) (配置元数据传递通过HTTP-GET可得)
            <endpoint addres=""
                      binding="customBinding(自定义绑定类型)"
                      bindingConfiguration="自定义绑定的设置名" />
 
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="行为配置名">
                <serviceMetadata 参数配置如httpGetEnabled="True" />
            </behavior>
        </serviceBehaviors>
    </behaviors>

    <bindings>
        <customBinding>
            <binding name = "自定义绑定的设置名">
               (如下绑定元素binding element按顺序设置,不必每个出现)
               【事务流, Trasaction flow】
               【(会话)可靠性(有序性), (Session) Reliability】 
               【安全性, Security】 
               【传输, Transport】  
               【编码, Encoding】
            </binding>
        <customBinding> 
    </bindings>
    … 

等效地在代码中设置:

using System.ServiceModel;
using System.ServiceModel.Description;  /* for ServiceMetadataBehavior */
using System.ServiceModel.Channels;    /* for custom binding */

Uri uri1 = new Uri("基路径");

Uri[] baseAddress = { uri1, … };
// 添加wsHttpBinding对应端点
ServiceHost host = new ServiceHost(typeof(名空间.服务类名), baseAddresses);
// 创建binding … 
WsHttpBinding wsBinding = new WsHttpBinding();
wsBinding.某属性 = 属性值;

host.AddServiceEndPoint(typeof(名空间.服务接口名), wsBinding, "相对/绝对路径");

// 添加mex端点
ServiceMetadataBehavior mb = host.Description.Behaviors.Find(); /* check existence */
if (mb == null)
{
  mb = new ServiceMetadataBehavior();
  mb.HttpGetEnabled = true;
  host.Description.Behaviors.Add(mb);
}

host.AddServiceEndPoint(ServiceMetadataBehavior.MexContractName /* string类型,名称 */,
                        MetadataExchangeBindings.CreateMexHttpBinding(),
                        "mex");

// 设置自定义属性
BindingElementCollection bec = new BindingElementCollection();
SymmetricSecurityBindingElement ssbe = new SymmetricSecurityBindingElement();
ssbe.LocalServiceSettings.InactivityTimeout = new Timespan(xx,xx,xx);
// 按顺序添加
bec.Add(new ReliableSessionBindingElement());
bec.Add(ssbe);
bec.Add(new HttpsTransportBindingElement();
bec.Add(new TextMessageEncodingBindingElement());
CustomBinding customBinding = new CustomBinding(bec);
host.AddServiceEndPoint(typeof(…), customBinding, "…");

2. wsHttpBinding

主要属性说明:

属性名 说明 默认值
HostNameComparisonMode 连接服务时主机名的匹配方式 StrongWildcard
用合法的主机名链接服务
MaxBufferPoolSize 为缓冲管理分配的大小 65536字节
MaxReceivedMessageSize 最大消息长度 65536字节
ReaderQuotas 消息中字符串内容限制 None
MessageEncoding 消息的编码方式(MTOM或Text/XML) Text
TextEncoding 消息文本的编码方式 UTF8Encoding
TransactionFlow 支持事务流 False


这些属性本质上在Binding内部的BindingElement上起作用,详见第四篇。

你可能感兴趣的:(DotNet关键知识点——WCF篇(二))