WCF中的HostNameComparisonMode

时间:2014-04-12 18:21:48

标签: wcf wcf-binding

我在我的控制台应用程序中自托管了一个WCF服务,并尝试了解basicHttpBinding的hostNameComparisonMode绑定属性。这是我托管的WCF:

// Test for StrongWildCard
        ServiceHost serviceHostForStrong = new ServiceHost(typeof(Service), new Uri("http://test1:8887/Service"));
        BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
        basicHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
        EndpointAddress endpointAddress = new EndpointAddress("http://abc1:8887/Service/ForStrong");
        ServiceEndpoint serviceEndpoint = 
            new ServiceEndpoint(ContractDescription.GetContract(typeof(IServiceContract)),
                basicHttpBinding,
                endpointAddress);
        serviceHostForStrong.AddServiceEndpoint(serviceEndpoint);
        serviceHostForStrong.Open();

        // Test for Exact
        ServiceHost serviceHostForExact = new ServiceHost(typeof(Service), new Uri("http://test2:8888/Service"));
        basicHttpBinding = new BasicHttpBinding();
        basicHttpBinding.HostNameComparisonMode = HostNameComparisonMode.Exact;
        endpointAddress = new EndpointAddress("http://abc2:8888/Service/ForExact");
        serviceEndpoint =
            new ServiceEndpoint(ContractDescription.GetContract(typeof(IServiceContract)),
                basicHttpBinding,
                endpointAddress);
        serviceHostForExact.AddServiceEndpoint(serviceEndpoint);
        serviceHostForExact.Open();

        // Test for Weak
        ServiceHost serviceHostForWeak = new ServiceHost(typeof(Service), new Uri("http://test3:8889/Service"));
        basicHttpBinding = new BasicHttpBinding();
        basicHttpBinding.HostNameComparisonMode = HostNameComparisonMode.WeakWildcard;
        endpointAddress = new EndpointAddress("http://abc3:8889/Service/ForWeak");
        serviceEndpoint =
            new ServiceEndpoint(ContractDescription.GetContract(typeof(IServiceContract)),
                basicHttpBinding,
                endpointAddress);
        serviceHostForWeak.AddServiceEndpoint(serviceEndpoint);
        serviceHostForWeak.Open();

        Console.WriteLine("Service is running....Press any key to exit");

这是我的客户端代码:

EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8887/Service/ForStrong/ABC"); // Why it doesn't match with StrongWildCard
        BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
        ChannelFactory<IServiceContract> channelFactory = new ChannelFactory<IServiceContract>(basicHttpBinding, endpointAddress);

        IServiceContract proxy = channelFactory.CreateChannel(endpointAddress);
        Console.WriteLine("Data received: " + proxy.GetData());
        Console.ReadKey();

据我所知http://kennyw.com/?p=109,网址"http://localhost:8887/Service/ForStrong/ABC"应与StrongWildCard匹配。但是,当我尝试运行此代码时,它会在客户端发出异常:

An unhandled exception of type 'System.ServiceModel.EndpointNotFoundException' occurred in mscorlib.dll Additional information: The message with To 'http://localhost:8887/Service/ForStrong/ABC' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher.  Check that the sender and receiver's EndpointAddresses agree.

我在这里遗漏了什么,或者我的理解是不正确的?几个支持问题:

  1. 你能给我一些例子(以此为基础) 会使用WeakWildCard网址吗?
  2. 我如何知道哪个服务器端URL 满足了我的要求?
  3. 谢谢!

0 个答案:

没有答案