c# - wcf 客户端 ip 作为 ipv6

标签 c# wcf iis-7 wcf-client

我正在使用下一段代码获取 wcf 服务上的客户端 ip:

        OperationContext context = OperationContext.Current;
        System.ServiceModel.Channels.MessageProperties prop = context.IncomingMessageProperties;
        System.ServiceModel.Channels.RemoteEndpointMessageProperty endpoint = prop[System.ServiceModel.Channels.RemoteEndpointMessageProperty.Name] as System.ServiceModel.Channels.RemoteEndpointMessageProperty;
        string ip = endpoint.Address;

当此代码在 iis6/server2003 上运行时一切正常,endpoint.Address 返回 ipv4。 但在我最近更新到 iis7/server2008 endpoint.Address 后返回 ipv6。

是否仍然可以在 iis7/server2008 上获得 ipv4?

谢谢!

最佳答案

这与其说是 WCF 中的变化,不如说是网络方面的变化。您的客户端已使用其 IPv6 连接到服务器,这是存储在消息上下文中的地址。如果您需要获取 IPv4,请使用以下代码段:

    IPAddress ipAddress = IPAddress.Parse(ipv6);
    IPHostEntry ipHostEntry = Dns.GetHostEntry(ipAddress);
    foreach (IPAddress address in ipHostEntry.AddressList)
    {
           if(address.AddressFamily == AddressFamily.InterNetwork)
                  Console.WriteLine(address);
    }

这会将您的 IPv6 转换为 IPv4。

关于c# - wcf 客户端 ip 作为 ipv6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3253872/

相关文章:

wcf - OPTIONS 预检请求未到达 IIS 托管服务

c# - 如何捕获退出 Winforms 应用程序的事件?

wcf - 如何为生成的 WCF 代理类创建 IntelliSense 兼容文档?

c# - IIS 7 找不到 Microsoft.CodDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider DLL

asp.net - 当用户使用 CAS SSO 注销时,ASP.NET MVC 触发 HttpRequestValidationException

c# - WCF 3.5 与 4.0 RESTful 服务性能对比

c# - 如何使用 Microsoft.VisualBasic 引用在 MonoDevelop 中使用 IsNumeric 方法?

c# - 从 IQueryable<T> 谓词获取 Func<T, bool>

c# - 如何确定上传的文件是 UTF-8 还是 UTF-16?

c# - 加密 WCF 消息密码