【问题标题】:Possible to have same contract, same binding, same address, but different ports?可能有相同的合同,相同的绑定,相同的地址,但不同的端口?
【发布时间】:2011-03-08 07:41:44
【问题描述】:

我有需要通过 basicHTTPBinding 进行通信的手持设备。我有一份合同,一切都按广告宣传。

我需要对其进行扩展,以便轻松支持更改为测试环境、培训,当然还有生产。我采用了端口路由,认为我可以通过端口差异公开不同的端点,并根据端口决定我想要从哪个数据库获取信息。

我似乎无法完成这项工作,到目前为止,还没有发现任何信息表明可以完成这项工作。由于端口是可选的,它可能不是。

有人做过这样的事吗?

【问题讨论】:

    标签: .net wcf wcf-endpoint


    【解决方案1】:

    虽然你不能用端口做你想做的事,但你可以用不同的路径来完成。例如将“/prod”或“/test”附加到您的基址。我提供了一个示例来说明这一点。

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    
    namespace WCFTest
    {
        class Program
        {
            static void Main()
            {
                List<Uri> baseAddresses = new List<Uri> { new Uri("http://localhost:1000/Prod"), new Uri("http://localhost:1000/Test") };
                ServiceHost wcfHost = new ServiceHost(typeof(SimpleWCF), new Uri[] {new Uri("http://localhost:1000")});
    
                foreach (ServiceEndpoint endpoint in SimpleWCF.CreateEndpoints(baseAddresses.ToArray()))
                {
                    wcfHost.AddServiceEndpoint(endpoint);
                }
    
                ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();
                metadataBehavior.HttpGetEnabled = true;
                wcfHost.Description.Behaviors.Add(metadataBehavior);
    
                wcfHost.Open();
                Console.ReadLine();
                wcfHost.Close();
            }
        }
    
        [ServiceContract]
        public interface ISimpleWCF
        {
            [OperationContract]
            string TestMethod();
        }
    
        public class SimpleWCF : ISimpleWCF
        {
            /// <summary>
            /// Thread Synchronization Object.
            /// </summary>
            private static readonly object _syncRoot = new object();
    
            /// <summary>
            /// Static Instance of Class.
            /// </summary>
            private static volatile SimpleWCF _current;
    
            /// <summary>
            /// Initializes a new instance of the <see cref="WebDataExchange"/> class.
            /// </summary>
            public SimpleWCF()
            {
                this.Contract = ContractDescription.GetContract(typeof(ISimpleWCF), GetType());
            }
    
            /// <summary>
            /// Gets or sets the contract.
            /// </summary>
            /// <value>The contract.</value>
            private ContractDescription Contract { get; set; }
    
            /// <summary>
            /// Gets the current instance of the SimpleWCF Object.
            /// </summary>
            /// <value>The current SimpleWCF Object.</value>
            public static SimpleWCF Current
            {
                get
                {
                    if (_current != null)
                    {
                        return _current;
                    }
    
                    lock (_syncRoot)
                    {
                        if (_current == null)
                            _current = new SimpleWCF();
    
                    }
    
                    return _current;
                }
            }
    
            /// <summary>
            /// Creates an Enpoint Collection.
            /// </summary>
            /// <param name="addresses">The addresses.</param>
            /// <returns>A Collection of ServiceEndpoints.</returns>
            public static Collection<ServiceEndpoint> CreateEndpoints(Uri[] addresses)
            {
                Collection<ServiceEndpoint> endpointCollection = new Collection<ServiceEndpoint>();
    
                foreach (Uri uriAddress in addresses)
                {
                    EndpointAddress address = new EndpointAddress(uriAddress);
    
                    BasicHttpSecurityMode securityMode = address.Uri.Scheme == Uri.UriSchemeHttps ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None;
                    BasicHttpBinding endpointBinding = new BasicHttpBinding(securityMode);
    
                    ServiceEndpoint endpoint = new ServiceEndpoint(Current.Contract, endpointBinding, address);
                    endpoint.ListenUriMode = ListenUriMode.Explicit;
                    endpointCollection.Add(endpoint);
                }
    
                return endpointCollection;
            }
    
            #region ISimpleWCF Members
    
            string ISimpleWCF.TestMethod()
            {
                if (OperationContext.Current.Channel.LocalAddress.Uri.AbsoluteUri.EndsWith("Prod"))
                    return "Hello Prod!";
                else return "Hello Test!";
            }
    
            #endregion
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      • 2014-08-06
      • 2019-08-11
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      相关资源
      最近更新 更多