0

Is it possible to call an external method in our WCF service by adding the dll of the method's class ?

I have an existing web application project with a method getcabfare(**), now I am trying to create a new WCF service to access that getcabfare() method by adding a reference of that existing application to my WCF project.

But I get the error

NullReferenceException not handled by user code - object reference not set to an instance of an object

This is my existing project "BasicCabApplication" which has the below method in bussiness logic layer which is working fine

namespace BasicCabApplication
{
 public class BussinessLogic
 {  
    public int getcabfare(string location)
    {
  string conn = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;
     SqlConnection con = new SqlConnection(conn);
     con.Open();
     SqlCommand cmd = new SqlCommand("Select fare from cablocations where area ='" + location +"'", con);
     int fare = Convert.ToInt16(cmd.ExecuteScalar());
     con.Close();
     return fare;
  }
}

and this is my wcf project to which I have added the reference of the above project. and the code in Service1.svc.cs is below

using BasicCabApplication;
namespace mywcftest1
{
   public class Service1 : IService1
   {
        public int GetFare(string location)
        {
           BussinessLogic bl = new BussinessLogic();
            int fare = bl.getcabfare(location);
            return fare;
        }
    }
}

I got the object bl but can't access the method getcabfare() which is throwing the nullreference exception.

Is it not possible to access the method of another project in wcf project(web service) ? or any mistake in my code....

Mark
  • 3,273
  • 2
  • 36
  • 54
prasanth
  • 1
  • 2
  • 4
    Welcome to Stack Overflow! Almost all cases of NullReferenceException are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Oct 16 '12 at 12:08

1 Answers1

0

Your Problem is here:

string conn = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;

Your wcf project has not this configuration in its config file, so it throws nullreference exception. You have to add string to your config file of wcf service with connection string and it will solve your problem

Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38