【问题标题】:Replacement of RemoteEndpointMessageProperty in .NET standard for client IP address retrieving替换 .NET 标准中的 RemoteEndpointMessageProperty 以检索客户端 IP 地址
【发布时间】:2019-07-26 09:39:26
【问题描述】:

问题与现有讨论有关:Get Client IP address using WCF 4.5 RemoteEndpointMessageProperty in load balancing situation

.NET 标准 2.0 不支持 RemoteEndpointMessageProperty。有谁知道是否有另一种方法可以从 OperationContext::IncomingMessageProperties 中检索客户端 IP 地址。

【问题讨论】:

  • 你有什么成果吗?
  • WCF 没有任何“标准”。 .NETCore 是他们想要前进的方式,有几个 .NET 功能他们不想维护。服务器端 WCF 在该列表中。如果您不想迁移到替代技术堆栈,那么您将完全坚持以 .NETFramework 为目标。

标签: c# .net-standard .net-standard-2.0


【解决方案1】:

您可以从source code 复制/粘贴课程

namespace System.ServiceModel.Channels
{
    using System;
    using global::System.Net;

    public sealed class RemoteEndpointMessageProperty
    {
        string address;
        int port;
        IPEndPoint remoteEndPoint;
        IRemoteEndpointProvider remoteEndpointProvider;
        InitializationState state;
        object thisLock = new object();

        public RemoteEndpointMessageProperty(string address, int port)
        {
            if (string.IsNullOrEmpty(address))
            {
                throw new ArgumentNullException(nameof(address));
            }

            if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }

            this.port = port;
            this.address = address;
            this.state = InitializationState.All;
        }

        internal RemoteEndpointMessageProperty(IRemoteEndpointProvider remoteEndpointProvider)
        {
            this.remoteEndpointProvider = remoteEndpointProvider;
        }

        internal RemoteEndpointMessageProperty(IPEndPoint remoteEndPoint)
        {
            this.remoteEndPoint = remoteEndPoint;
        }

        public static string Name
        {
            get { return "System.ServiceModel.Channels.RemoteEndpointMessageProperty"; }
        }

        public string Address
        {
            get
            {
                if ((this.state & InitializationState.Address) != InitializationState.Address)
                {
                    lock (ThisLock)
                    {
                        if ((this.state & InitializationState.Address) != InitializationState.Address)
                        {
                            Initialize(false);
                        }
                    }
                }
                return this.address;
            }
        }

        public int Port
        {
            get
            {
                if ((this.state & InitializationState.Port) != InitializationState.Port)
                {
                    lock (ThisLock)
                    {
                        if ((this.state & InitializationState.Port) != InitializationState.Port)
                        {
                            Initialize(true);
                        }
                    }
                }
                return this.port;
            }
        }

        object ThisLock
        {
            get { return thisLock; }
        }

        void Initialize(bool getHostedPort)
        {
            if (remoteEndPoint != null)
            {
                this.address = remoteEndPoint.Address.ToString();
                this.port = remoteEndPoint.Port;
                this.state = InitializationState.All;
                this.remoteEndPoint = null;
            }
            else
            {
                if ((this.state & InitializationState.Address) != InitializationState.Address)
                {
                    this.address = remoteEndpointProvider.GetAddress();
                    this.state |= InitializationState.Address;
                }

                if (getHostedPort)
                {
                    this.port = remoteEndpointProvider.GetPort();
                    this.state |= InitializationState.Port;
                    this.remoteEndpointProvider = null;
                }
            }
        }

        internal interface IRemoteEndpointProvider
        {
            string GetAddress();
            int GetPort();
        }

        [Flags]
        enum InitializationState
        {
            None = 0,
            Address = 1,
            Port = 2,
            All = 3
        }
    }
}

【讨论】:

  • 我想在 .net core 和 .net framework 项目中使用这个包。如果我复制一个类,我会产生类冲突。
  • 您可以根据需要重命名该类,但该功能适用​​于 NET Core 和 .NET 框架
  • 现有的 .netfraemwork 项目已经有一个 RemoteEndpointMessageProperty 的实例。我不喜欢映射这些对象的想法
  • 这是alternative way to retrieve the client IP address off a OperationContext::IncomingMessageProperties
猜你喜欢
  • 1970-01-01
  • 2018-08-21
  • 2011-09-09
  • 1970-01-01
  • 2016-01-14
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
  • 2013-04-20
相关资源
最近更新 更多