5

I am receiving a FaultException from a WCF service as follows when it is invoked:

2012-04-02 16:26:00.3593|Error|System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: The type initializer for 'vService.CheckService' threw an exception. (Fault Detail is equal to An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.TypeInitializationException: The type initializer for 'vService.CheckService' threw an exception. ----> System.NullReferenceException: Object reference not set to an instance of an object.
   at vService.CheckService..cctor() in d:\working\code\VioletServer\vService\CheckService.cs:line 14
   --- End of inner ExceptionDetail stack trace ---
   at vService.CheckService..ctor()
   at CreatevService.CheckService()
   at System.ServiceModel.Dispatcher.InstanceProvider.GetInstance(InstanceContext instanceContext, Message message)
   at System.ServiceModel.Dispatcher.InstanceBehavior.GetInstance(InstanceContext instanceContext, Message request)
   at System.ServiceModel.InstanceContext.GetServiceInstance(Message message)
   at System.ServiceModel.Dispatcher.InstanceBehavior.EnsureServiceInstance(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)
  ...).

Line 14 of CheckService.cs is

private static string connStr = ConfigurationManager.ConnectionStrings["violetdb"].ConnectionString;

What does the exception mean in this context and how can I resolve it?


//CheckService.cs
public class CheckService : ICheckService
{
    private static string connStr = ConfigurationManager.ConnectionStrings["violetdb"].ConnectionString;
    MessageRepository _repo = new MessageRepository(connStr);

    public CheckService(){}

    public CheckService(MessageRepository repo)
    {
        _repo = repo;
    }

    public void SendMessage(string sender, string recipient, string messagetext)
    {
        _repo.DeliverMessage(sender,recipient,messagetext);
    }
}
Animesh
  • 4,926
  • 14
  • 68
  • 110

1 Answers1

4

Most likely your WCF service does not have a connection string named "violetdb" in its application configuration file.

A TypeInitializerException is thrown in this circumstance, because you have a static field which is being initialised ahead of type construction, and because the ConnectionStrings["violetdb"] call is returning null, and it is throwing an NullReferenceException.

Bottom line, check the <connectionStrings> section of your configuration file and ensure that the connection string exists.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
  • I had the `violetdb` connection string in `` section in the `web.config` file. Since the project is a class library, I just added an `app.config` file and copied the connection string over. I still get the exception. – Animesh Apr 02 '12 at 11:32
  • Where are you hosting your application? Is it self hosted (i.e. a console app, or windows service? Is it hosted in IIS? – Matthew Abbott Apr 02 '12 at 12:38
  • It is self hosted in a console application. – Animesh Apr 02 '12 at 12:41
  • Is it possible to debug what is happening with the service? – Animesh Apr 02 '12 at 13:31
  • 1
    Matthew, I set the connection string manually and found that it worked. I find it odd that the connection string did not get picked up from app.config. I have never seen this happen. Do you have insight into this? – Animesh Apr 02 '12 at 15:08
  • Sure, I would start by simply running your console application and see if the exception is thrown when you call the service. If not, click `Debug` > `Exceptions` and check the `Thrown` checkbox for "Common Language Runtime Exceptions" – Matthew Abbott Apr 02 '12 at 15:09
  • Okay, I will check that. Appreciate your help. – Animesh Apr 02 '12 at 15:16