ServerManager serverManager = new ServerManager();
//get the site (e.g. default)
Site site = serverManager.Sites.FirstOrDefault(s => s.Name == "ERP3");
//Protocol hhtp, https....
httpcontext.current.response.write(site.Bindings.ElementAt(0).EndPoint.Address);
//Puerto
httpcontext.current.response.write(site.Bindings.ElementAt(0).EndPoint.Port);
It happens when you try to call use an object property or method for an object which is null. Use the debugger (or add code checks) to see which object in your expression is null starting maybe with site.
If you can't debug live you can add code such as :
if (site==null) throw new Exception("site is null");
if (site.Binbings==null) throw new Exception("Bindings is null");
to find out which object is causing the issue. EndPoint is perhaps null if not binding is explicitely defined. Ypu may want also to handle having 0 to n bindings for your site etc...
HttpContext.Current only exists in the context of the Web application, the client does not exist HttpContext,You need to make sure that httpcontext is on the server and not the client.
Best regards,
Peng Ding
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
75 Points
513 Posts
object reference not set to an instance object
Jul 08, 2020 06:18 AM|zhyanadil.it@gmail.com|LINK
i get error on high lighted code
i get error on that high light
object reference not set to an instance object
All-Star
48720 Points
18184 Posts
Re: object reference not set to an instance object
Jul 08, 2020 06:49 AM|PatriceSc|LINK
Hi,
It happens when you try to call use an object property or method for an object which is null. Use the debugger (or add code checks) to see which object in your expression is null starting maybe with site.
Member
75 Points
513 Posts
Re: object reference not set to an instance object
Jul 08, 2020 07:38 AM|zhyanadil.it@gmail.com|LINK
my demo don't have any problem with c# console but when i run it on asp.net web service i get this exception
All-Star
48720 Points
18184 Posts
Re: object reference not set to an instance object
Jul 08, 2020 08:38 AM|PatriceSc|LINK
If you can't debug live you can add code such as :
if (site==null) throw new Exception("site is null");
if (site.Binbings==null) throw new Exception("Bindings is null");
to find out which object is causing the issue. EndPoint is perhaps null if not binding is explicitely defined. Ypu may want also to handle having 0 to n bindings for your site etc...
Member
75 Points
513 Posts
Re: object reference not set to an instance object
Jul 08, 2020 08:54 AM|zhyanadil.it@gmail.com|LINK
i have exception at both of them
if (site.Bindings == null) throw new Exception("Bindings is null");
if (site == null) throw new Exception("site is null");
Member
75 Points
513 Posts
Re: object reference not set to an instance object
Jul 08, 2020 09:03 AM|zhyanadil.it@gmail.com|LINK
can you run that demo at the first post on your side on asp.net web service?
None
0 Points
4 Posts
Re: object reference not set to an instance object
Jul 24, 2020 07:07 AM|Peng Ding|LINK
Hi,
HttpContext.Current only exists in the context of the Web application, the client does not exist HttpContext,You need to make sure that httpcontext is on the server and not the client.
Best regards,
Peng Ding