为什么's gives me a "类型的未处理异常'System.NullReferenceException' occurred in IP.dll"?

NETANEL

会员
已加入
2019年2月14日
留言内容
21
编程经验
3-5
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web;

namespace IP
{
    public class a
    {
        
                    public string GETip()
        {
            OperationContext context = OperationContext.Current;
            MessageProperties properties = context.IncomingMessageProperties;
            RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            string address = string.Empty;
            //http://www.simosh.com/article/ddbggghj-get-client-ip-address-using-wcf-4-5-remoteendpointmessageproperty-in-load-balanc.html
            if (properties.Keys.Contains(HttpRequestMessageProperty.Name))
            {
                HttpRequestMessageProperty endpointLoadBalancer = properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
                if (endpointLoadBalancer != null && endpointLoadBalancer.Headers["X-Forwarded-For"] != null)
                    address = endpointLoadBalancer.Headers["X-Forwarded-For"];
            }
            if (string.IsNullOrEmpty(address))
            {
                address = endpoint.Address;
            }
            return address;

        }

        }
    }
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,492
地点
悉尼,澳大利亚
编程经验
10+
Please specify which line the exception is thrown on and provide the stack trace for the exception. Also, if you get a NullReferenceException, you should be checking that line to see which reference is null before posting. Once you know that, you may even be able to solve the issue for yourself.
 
最佳 底部