可能有相同的合同,相同的绑定,同一个地址,但不同的端口?能有、绑定、端口、不同

2023-09-03 23:05:36 作者:心若一动,泪便倾城

我有一个需要通过basicHttpBinding的沟通手持设备。我有合同,一切都像宣传的那样。

I have handhelds that need to communicate via basicHTTPBinding. I have a contract and everything works as advertised.

我需要扩展它轻松地支持不断变化的过程中生产测试环境,培训和。我把端口路由,想我可以用端口的不同曝光不同的端点,以及基于端口,决定我想从信息的数据库。

I need to expand it to easily support changing to a test environment, training and of course production. I took the port route, thinking I could expose different endpoints with port differences, and based on the port, decide which database I wanted info from.

我似乎无法使这项工作,至今没有发现任何信息的任何地方,表明是可以做到的。因为端口是可选的,它可能不是。

I cant seem to make this work, and so far have found no information anywhere that indicates it can be done. Since port is optional, it may not be.

任何人都做过这样的事?

Anyone done anything like this?

推荐答案

虽然你不能做你想要的端口你可以用不同的路径完成这个任务。如添加/刺或/测试你的基址。我已经提供了一个展示这样的一个例子。

Although you can't do what you want with the port you can accomplish this with a different path. Such as appending "/prod" or "/test" to your base address. I've included an example that illustrates this.

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
    }

}
 
精彩推荐
图片推荐