Solved

Stumped on Web Service Error in .NET

Posted on 2014-04-21
11
421 Views
Last Modified: 2014-04-21
Hello,

I am completely stumped and baffled by this "Object reference not set to an instance of an object." error I'm receiving when I try to call a Webservice from my .NET code.

Here's what's odd:

1) It works fine in development and on staging. When I publish it live, I get the error.

2) I have two very similar methods in my Web Service that I'm calling. One works, the other doesn't.

Since I can't debug live, I put in code to write to a text file during each step of the process. It turns out, it's erroring out when I call the WebService method; it's not even getting into the Web service method. but, like I said, a very similar web service method is being called from a different .aspx page, and that works fine.

Here are the lines of code that are calling the Web Service methods. The first one is the one that doesn't work, the second is the one that does.

Bad:
     Log.AddToLogFile("Getting ready to call new WebService().DisplayProduct() ... genericId: " + genericId + "; systemId: " + systemId, null);
     rightSide.Text += new WebService().DisplayProduct(genericId, systemId);

Open in new window

The log gets updated, but then it immediately errors out on the next line.

Good:
     Log.AddToLogFile("Getting ready to call new WebService().DisplayGallery() ... genericId: " + genericId + "; systemId: " + systemId, null);
     rightSide.Text += new WebService().DisplayGallery(myGallery, genericId, systemId);

Open in new window

Again, this calls the method with no problem.

Method That Isn't Being Called:
    [WebMethod(EnableSession = true)]
    public string DisplayProduct(string genericId, string systemId)
    {
        Log.AddToLogFile("\nGot into WebService().DisplayProduct(). Getting ready to call the overloaded method: genericId: " + genericId + "; systemId: " + systemId, null);
        return DisplayProduct(genericId, systemId, null, null);
    }

Open in new window

It doesn't even get here because no log file entry is being created.

Method That Does Get Called:
    [WebMethod(EnableSession = true)]
    public string DisplayGallery(List<Gallery> myGallery, string genericId, string systemId)
    {
        Log.AddToLogFile("\nGot into WebService().DisplayGallery(). Getting ready to call the overloaded method: genericId: " + genericId + "; systemId: " + systemId, null);
        return DisplayGallery(myGallery, genericId, systemId, null, null);
    }

Open in new window

This gets called with no issues.

This makes absolutely no sense. Not just because one is working and the other isn't, but that this is only happening on the live server and it was working before.

Please help!
0
Comment
Question by:infotechelg
  • 6
  • 3
  • 2
11 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40012765
Are you referring to the same webservice or does production have a different web service URL than say staging and dev?



Just an FYI I would create an instance of a webservice:
WebService ws = new WebService();

then call ws.DisplayProduct(...);
ws.DisplayGallery(...);

etc.

I would try pointing to the production URL on dev and see if you get the same results . . . it could be the webservice itself wasn't updated.  

Take baby steps, run the code with the parameters on the productionDB.  Do you get expected results?

Then invoke the webservice from the box itself passing in the ids manually, do you get the expected XML?

And finally make sure the production box is pointing to the webservice you just tested.
0
 

Author Comment

by:infotechelg
ID: 40012772
Thanks for the reply. I guess I'm not sure what you mean by "making sure the production box points to the web service". The Web Service (.asmx) is part of my .NET project that gets compiled. It's not separate or anything.

But your idea of running the Webservice manually is good. I'll try that.
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40012781
The reference to the asmx can be re-pointed to any url that runs that web service.

EG:

I have a development box running webservice.asmx that I can add more functions to without breaking existing functionality.

When it comes time for production, I change the url to the production box with the same webservice.asmx.  If I don't update the asmx file, the new functionality won't be there.
0
 

Author Comment

by:infotechelg
ID: 40012833
I'm trying to access the Web Services through the browser so I can test (by the URL: http://myurl.com/Services/WebService.asmx) but am getting this error:

Unable to handle request.

Am I missing something from the .asmx file or the web.config file needed to make this work?
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40012867
just a note . . . you can only hit it from localhost . . . so you'll need to remote into the machine in order to test.

But if you can't hit the webservice.asmx that's a great place to start.  You should always get back a page with at least the functions available to be called.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 19

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 500 total points
ID: 40012954
I think the problem is that you're calling a method that is overloaded and not overloading it properly.

Are you decorating the method with the appropriate OperationContract attribute?

[WebMethod(EnableSession = true)]
[OperationContract(Name="DisplayProductByTwoIDs")]
    public string DisplayProduct(string genericId, string systemId)
    {
        Log.AddToLogFile("\nGot into WebService().DisplayProduct(). Getting ready to call the overloaded method: genericId: " + genericId + "; systemId: " + systemId, null);
        return DisplayProduct(genericId, systemId, null, null);
    }

Open in new window


Then, when you call it:

Log.AddToLogFile("Getting ready to call new WebService().DisplayProduct() ... genericId: " + genericId + "; systemId: " + systemId, null);
     rightSide.Text += new WebService().DisplayProductByTwoIDs(genericId, systemId);

Open in new window


Otherwise, it won't work. You can't have identical named overloaded methods exposed via a web service.
0
 

Author Comment

by:infotechelg
ID: 40013063
Dan7el, I will certainly try that. However, the method that does work is overloaded, and that's not an issue. Also, if that were the case, wouldn't it error-out everywhere, not just in production?

EDIT: Dan7el, "OperationContract" isn't coming up in Intellisense and is returning an error.
0
 

Author Comment

by:infotechelg
ID: 40013108
"I would try pointing to the production URL on dev and see if you get the same results"

Sorry, but how do I do that? :)
0
 
LVL 19

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
ID: 40013275
You need to:
using System.ServiceModel;

for the OperationContract. This is typical for WCF services, but from what I can tell, it applies to ASMX (web services) as well.

Here's the deal: Overloading works but not with real services. If you are in any way using the DLL directly and not via a service reference, then overloading without the OperationiContract attribute will simply work.

My guess is that you are inadvertently using the DLL rather than the actual service in your non-production work. However, when you try to use it in production, you are, in fact, actually using the service and the "overload" fails. You need to basically "rename" the method and use the newly named method.

It's a big pain, I know.
0
 

Author Comment

by:infotechelg
ID: 40013284
Ok, thanks Dan7el. I'll give it a shot.
0
 

Author Comment

by:infotechelg
ID: 40013314
Dan7el, well...good and bad news. The bad news is I'm still receiving the error. But, the good news is that the code actually got into the Web Service this time as the log file populated with text!

So, now it's just a matter of tracking that down.

Thanks!!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

These instructions are based on installing Owncloud on your new raspberry pi connected with a usb HDD. What do you need Part A? A Raspberry Pi, model B. A boot SD card for the Raspberry Pi. A usb HDD An Ethernet cable to connect to the lo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now