Why it's gives me a "An unhandled exception of type 'System.NullReferenceException' occurred in IP.dll"?

NETANEL.S

Member
Joined
Feb 14, 2019
Messages
21
Programming Experience
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;

        }

        }
    }
 
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.
 
Back
Top Bottom