1

Basically I have an old asmx web service on one server and I have a consume app that has this as a web reference and can see the available methods. Also if I go to the web service URI I get the standard page that lists them all out.

I am trying to create a "consumer" app to prove it works as just pressing "play" in "debug" mode is not good enough as we need to give the customer examples of how to use it.

Although it works 100% when I do it as a debug run from VS 2010 on my PC and localhost:5768 I cannot get it to work from my consumer console app.

It is raising an exception saying that an object reference doesn't exist when I try to get the connection string I need from the web.config file.

This works when I run it in debug mode but NOT when my consumer app tried to run it which has a web reference to the right service URI loaded.

The code in web.confifg is just

<connectionStrings>
    <add name="APIDatabase" connectionString="SERVER=MYSERVER;DATABASE=MYDB;uid=MYUID;pwd=MYPWD;" providerName="System.Data.SqlClient" />   
</connectionStrings>

The code in my .asmx file with the webmethod in that calls SetUp that tries to get this value is below (with fluff removed)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebService
{

    [WebService(Namespace = "http://webservice.demo.net/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        // my DB object that runs all my DB queries, opens & kills connections etc
        private SqlDatabase SqlDatabase;

        [WebMethod]
        public string GetNames(string ClientHash)
        {
            // gets here then calls the SetUp method passing in "GetNames"

            // call SetUp to set up global objects - call on every web method
            this.SetUp("GetNames");  

            //.. more code
    }

    private void SetUp(string method)
    {
        // create DB object
        this.SqlDatabase = new SqlDatabase();

        // **it is erroring here** - Using System.Configuration is added at top of class and there is a manual reference added to the DLL
        **this.SqlDatabase.ConnectionString = ConfigurationManager.ConnectionStrings["APIDatabase"].ToString();**

        // more code it cannot get to due to the above error
    }  
}

This is the simple consumer application project in C# that I am running on my PC

I've addeded the web reference as a proper reference to the console app and I can see all the web methods it has exposed including GetNames

I get to the web service call the method I want e.g, GetNames, but then it errors on the SetUp method as it tries to set the connection string for my SqlDatabase object (my data class).

This all works 100% in DEMO, pressing play, running on localhost etc.

However when I try and run the test console app on my PC connecting to the webservice I get the following error.

Start 09/10/2014 22:05:23

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at WebService.Service1.SetUp(String method) in C:\Users\me\Documents\Visual Studio 2010\Projects\WebService\WebService\WebService.asmx.cs:line 170 at WebService.Service1.GetNames(String ClientHash) in C:\Users\me\Documents\Visual Studio 2010\Projects\WebService\WebService\WebService.asmx.cs:line 44 at TestWebService.Program.Main(String[] args) in C:\Users\rreid\Documents\Visual Studio 2010\Projects\WebService\TestWebService\Program.cs: line 21

The code in my consumer app is below.

using System;
using System.Collections;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;

namespace TestWebService
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Start " + DateTime.Now.ToString());

            // Create my web service object
            MyWebService.Service1 MyWebService = new MyWebService.Service1();

            string customerID = "6D104928-7633-4998-90C5-C01ABD4E010B"; // test a customer I know works

            // this is line 21 in my code (see ethe rror message above)
            // It errors in the SetUp method in the asmx file in the SetUp method when trying to get a connection string from the app.config file and set it to the database object I use
            string response = MyWebService.GetNames(customerID);

            Console.WriteLine("Stop " + DateTime.Now.ToString());

        } 
     }
}

So I am following a guide (I've taken over the project) > http://support2.microsoft.com/kb/308359

And it seems to work in that the web service is found, the method can be run but a sub method in the project is failing.

-Am I supposed to rename web.config to something else e.g app.config? -Am I supposed to hardcode connection strings or can they be handled - I am not doing anything I have not done before in regards to getting configuration settings? -Do I need to add in extra code to my consumer application to allow IT to run SQL or get configuration options?

I am not sure what I am doing wrong as the code all seems correct. Plus no one at work can help me but are constantly on my back asking for it to be done!

MonkeyMagix
  • 677
  • 2
  • 10
  • 30
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Oct 10 '14 at 12:43
  • http://i.imgur.com/RxtXaUP.jpg ;) –  Oct 10 '14 at 12:43

2 Answers2

2
ConfigurationManager.ConnectionStrings["APIDatabase"].ToString();

Since you say this is the line that is erroring, one can only assume that "APIDatabase" was not found in your .config file. Calling .ToString() on a null object will cause that error. If you are able to edit and test the code, you should split it out and check that you were able to get a connection string.

ConnectionStringSettings csItem = ConfigurationManager.ConnectionStrings["APIDatabase"];
if (csItem == null)
    throw new Exception("Connection string not found for \"APIDatabase\"");

SqlDatabase.ConnectionString = csItem.ConnectionString;

The web.config should be set up with a connection string entry like so:

<configuration>
    <connectionStrings>
       <add name="APIDatabase" 
            providerName="YOUR PROVIDER" 
            connectionString="YOUR CONNECTION STRING" />
    </connectionStrings>
</configuration>

EDIT: Just noticed you supplied your web.config connection string entry in your question. Make sure it is located under the configuration element.

Double check that the server is supplying the correct web.config file. It's been awhile since I have used IIS, but I remember it seeming like there were several different versions of web.config in the UI. Make sure you added it to the correct one.

Am I supposed to rename web.config to something else e.g app.config?

Only if you are using an EXE to communicate with the web service. EXEs use app.config in the project and then when built get renamed to MyExecutableName.exe.config. If you're using a WinForm or Console app, then it won't read your web.config and will instead look for a .config like I described.

TyCobb
  • 8,909
  • 1
  • 33
  • 53
  • I was talking about RENAMING the config file IN the WebService projects (that works in demo mode on local host). There is no web.config file in the consumer app at all I am just trying to run a webmethod from it which in turn is accessing the connection string from the web.config file in the web service app. The connection string is UNDER the configuration node, there are just some configsection nodes first with sectiongroups that refer to system.web.extensions etc. Also all parts of your connection string ARE in the node. NB it works fine in DEBUG (localhost) only through webservice it errors – MonkeyMagix Oct 10 '14 at 08:59
  • What I need is someway to debug this. Can I jump from a consumer console app to a breakpoint IN my web method/web service? Seems odd that it works in debug mode, I can add the webservice, see the methods but then it errors on a simple get config node IN the webservice. – MonkeyMagix Oct 10 '14 at 09:01
  • Bascially I have got it working - BUT - only by hardcoding the connection string in the SetUp method IN the Web Service .asmx file! Why DEBUG mode works and can get connection strings from the web.config file I DO NOT KNOW. Why a consumer app errors on this line I do not know (Or can find a way to debug) but by doing this >>>> // can only get it to work by hardcoding the connection string in my code otherwise it errors this.SqlDatabase.ConnectionString = "SERVER=MyServer;DATABASE=MyDB;uid=MyLogin;pwd=MyPSWD"; Why it cannot use web.config with my consumer app I don't know? Ideas? – MonkeyMagix Oct 10 '14 at 11:54
  • @MonkeyMagix well essentially you are verifying the assumption TyCobb is stating. The connection string can't be found (which is why it works when hard coding it) there's usually a web.config pr project configuration. e.g. one for debug and one for release. One reason for this is because connection strings usually differ between environments it's thus likely that you are using different configurations in the scenarios you have and it has nothing to do with the consumer app so don't worry about how to debug that, just debug the web as suggested by TyCobb – Rune FS Oct 10 '14 at 12:48
0

It is now working and using

ConfigurationManager.ConnectionStrings["APIDatabase"].ToString();

to get the connection string from the webservice code

Things I did

I added the WebServiceName.Program to the Startup object of my test consumer app.

I always removed the web reference from the consumer app in-between rebuilds of the WebService and copying it to the live site I don't know exactly why it is now able to get the value from the config file and not the hard-coded string as before.

I removed this from my web.config file which was at the top of it and seems to work without it

<configSections>
      <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">          
          <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          </sectionGroup>
        </sectionGroup>
      </sectionGroup>
    </configSections>  

So maybe it was that XML that was preventing it from being accessible?

Anyway works now!

MonkeyMagix
  • 677
  • 2
  • 10
  • 30