C# 替换.NET标准中用于客户端IP地址检索的RemoteEndpointMessageProperty
该问题与现有的讨论有关:C# 替换.NET标准中用于客户端IP地址检索的RemoteEndpointMessageProperty,c#,.net-standard,.net-standard-2.0,C#,.net Standard,.net Standard 2.0,该问题与现有的讨论有关: .NET标准2.0不支持RemoteEndpointMessageProperty。有人知道是否有其他方法可以从OperationContext::IncomingMessageProperties检索客户端IP地址吗 您可以从 namespace System.ServiceModel.Channels { 使用制度; 使用global::System.Net; 公共密封类RemoteEndpointMessageProperty { 字符串地址; 国际港口; IPE
.NET标准2.0不支持RemoteEndpointMessageProperty。有人知道是否有其他方法可以从OperationContext::IncomingMessageProperties检索客户端IP地址吗 您可以从
namespace System.ServiceModel.Channels
{
使用制度;
使用global::System.Net;
公共密封类RemoteEndpointMessageProperty
{
字符串地址;
国际港口;
IPEndPoint远程端点;
IRemoteEndpointProvider remoteEndpointProvider;
初始化状态;
对象thisLock=新对象();
公共RemoteEndpointMessageProperty(字符串地址,int端口)
{
if(string.IsNullOrEmpty(address))
{
抛出新ArgumentNullException(nameof(address));
}
如果(端口IPEndPoint.MaxPort)
{
抛出新ArgumentOutOfRangeException(nameof(port));
}
this.port=端口;
this.address=地址;
this.state=初始化状态.All;
}
内部RemoteEndpointMessageProperty(IRemoteEndpointProvider remoteEndpointProvider)
{
this.remoteEndpointProvider=remoteEndpointProvider;
}
内部RemoteEndpointMessageProperty(IPEndPoint remoteEndPoint)
{
this.remoteEndPoint=remoteEndPoint;
}
公共静态字符串名
{
获取{return“System.ServiceModel.Channels.RemoteEndpointMessageProperty”;}
}
公共字符串地址
{
得到
{
if((this.state&InitializationState.Address)!=InitializationState.Address)
{
锁(这个锁)
{
if((this.state&InitializationState.Address)!=InitializationState.Address)
{
初始化(假);
}
}
}
返回此地址;
}
}
公共int端口
{
得到
{
if((this.state&InitializationState.Port)!=InitializationState.Port)
{
锁(这个锁)
{
if((this.state&InitializationState.Port)!=InitializationState.Port)
{
初始化(true);
}
}
}
返回此.port;
}
}
反对这个锁
{
获取{返回此锁;}
}
无效初始化(bool getHostedPort)
{
如果(remoteEndPoint!=null)
{
this.address=remoteEndPoint.address.ToString();
this.port=remoteEndPoint.port;
this.state=初始化状态.All;
this.remoteEndPoint=null;
}
其他的
{
if((this.state&InitializationState.Address)!=InitializationState.Address)
{
this.address=remoteEndpointProvider.GetAddress();
this.state |=InitializationState.Address;
}
如果(getHostedPort)
{
this.port=remoteEndpointProvider.GetPort();
this.state |=InitializationState.Port;
this.remoteEndpointProvider=null;
}
}
}
内部接口提供程序
{
字符串GetAddress();
int GetPort();
}
[旗帜]
枚举初始化状态
{
无=0,
地址=1,
端口=2,
全部=3
}
}
}
你取得了什么成果吗?WCF没有什么“标准”。NETCore是他们想要继续前进的方式,有几个.NET功能他们不想维护。服务器端WCF在该列表中。如果你不想转移到另一个技术堆栈,那么你完全无法确定.NETFramework的目标。我想在.net核心和.net framework项目中使用这个包。如果复制一个类,则会出现类冲突。如果需要,可以重命名该类,但该功能适用于NET Core和.NET Framework。现有的.NetFramework项目已具有RemoteEndpointMessageProperty的实例。我不喜欢映射这些对象。这是从OperationContext::IncomingMessageProperties中检索客户端IP地址的另一种方法!
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
}
}
}