HostNameComparisonMode 枚举


指定将传入的消息调度到服务终结点时应该如何在 URI 比较中使用主机名。Specifies how the host name should be used in URI comparisons when dispatching an incoming message to a service endpoint.

public enum class HostNameComparisonMode
public enum HostNameComparisonMode
type HostNameComparisonMode = 
Public Enum HostNameComparisonMode


Exact 1

如果没有找到强匹配,在将传入的消息调度到此终结点时使用主机名。Uses the hostname when dispatching incoming messages to this endpoint, if no strong match was found.

StrongWildcard 0

将传入的消息调度到此终结点时,忽略主机名。Ignores the hostname when dispatching incoming messages to this endpoint. 这是默认值。This is the default value.

WeakWildcard 2

如果没有找到强匹配或精确匹配,匹配时忽略主机名。If no strong or exact match was found, ignores the hostname when matching.


下面的示例演示如何在服务端配置文件中设置 StrongWildcard 值。The following is an example of how to set StrongWildcard value in a service side configuration file.

以下是使用上述配置文件的基本服务和客户端。Here is a basic service and client that use the preceding configuration file.

public interface ISayHello
    string SayHello();

public class HelloService : ISayHello
    public string SayHello()
        return "Hello, WCF!";
// Open up a channel factory on a client application.
ChannelFactory<ISayHello> factory = new ChannelFactory<ISayHello>("BasicHttpBinding_ISayHello");

// Both of these contracts work (provided both hostnames are valid) because
// the binding configuration is set to hostNameComparisonMode="StrongWildcard".

ISayHello channel = factory.CreateChannel(new EndpointAddress("http://localhost:8000/UESamples/HelloService"));
ISayHello channel2 = factory.CreateChannel(new EndpointAddress("http://machineName/UESamples/HelloService"));


Console.WriteLine("Press <ENTER> to terminate client.");


该值用于指定当调度传入的消息时,HTTP、Net.Tcp 和 Net.Pipe 等传输使用的 URI 匹配规则。The value is used to specify the URI matching rules used by transports such as HTTP, Net.Tcp, and Net.Pipe when dispatching incoming messages. 通过上述传输之一(如 HostNameComparisonMode),使用相应的 BasicHttpBinding 属性配置标准绑定的 HostNameComparisonMode 值。Configure the value of the HostNameComparisonMode for a standard binding using one of the preceding transports, such as BasicHttpBinding, using the corresponding HostNameComparisonMode property. 通过上述传输之一(如 HostNameComparisonMode),使用相应的 HttpTransportBindingElement 属性配置绑定元素的 HostNameComparisonMode 值。Configure the value of the HostNameComparisonMode for a binding element using one of the preceding transports, such as HttpTransportBindingElement, using the corresponding HostNameComparisonMode property.

HostNameComparisonMode 的每个值都与匹配规则的一个特定类型相对应。Each value of the HostNameComparisonMode corresponds to a specific type of matching rule. 匹配规则的尝试顺序始终如下所示:The sequence of matching rules attempted is always ordered as follows:

  1. StrongWildcardStrongWildcard

  2. ExactExact

  3. WeakWildcardWeakWildcard

第一个值 StrongWildcard 在匹配时忽略主机名,它采用三个不同的匹配模式的最高优先级。The first value, StrongWildcard, ignores the host name when matching and it takes the highest precedence of the three different matching modes. 它是 WCF 系统提供的绑定或绑定元素的默认值。It is the default value for a WCF system-provided binding or binding element. 这指示可以使用任何有效的主机名访问服务终结点。This indicates that a service endpoint can be reached using any valid host name. 例如,如果 MyService 是使用 http://localhost/MyService托管的,则仍然可以使用 访问它,因为(可能是有效的)主机名 "" 被忽略。For example, if MyService is hosted using http://localhost/MyService, it is still reachable using because the (presumably valid) host name, "", is ignored. 请注意,此时端口也是通配符。Note that the port is a wildcard here as well.

如果未找到强匹配项,则第二个值 Exact,要求找到具有指定 URI 的完全匹配项,包括主机名。The second value, Exact, requires that an exact match is found with the URI specified, including the hostname, if no strong match is found. 例如,此模式并不会将简短的主机名和完全限定域名视为等效。This mode, for example, does not perform equivalence between short hostnames and fully-qualified domain names. 这样一来,在为多台主机分配了同一个 IP 地址的情况下,可以将主机名用作匹配条件,并且可以在具有不同终结点的同一台计算机上承载不同的服务。This allows hostnames to be used as match criteria in the scenarios where multiple hosts are assigned a single IP address and it enables different services to be hosted on the same machine with distinct endpoints. 请注意,此时端口也是通配符。Note that the port is a wildcard here as well.

如果未找到强匹配或精确匹配,则第三个值 WeakWildcard 通过忽略主机名来匹配。The third value, WeakWildcard, matches by ignoring the hostname if no strong or exact match was found. 此行为与 StrongWildcard 的顺序不同,而不是匹配的顺序:在尝试强绑定和精确绑定之后运行。The behavior is the same as for StrongWildcard other than the order of matching: it runs after the strong and exact bindings have been tried.


这些值在 Internet 信息服务 (IIS) 或 Windows 进程激活服务 (WAS) 宿主环境中使用时无效。These values have no effect when used inside of the Internet Information Services (IIS) or Windows Process Activation Service (WAS) hosting environment. 在这些情况下,WCF 使用宿主 WCF 服务的 IIS 网站提供的任何主机名比较模式。In those cases, WCF uses whatever hostname comparison mode is provided by the IIS Web Site hosting the WCF services.