title: Object reference not set to an instance of an object - WCF Service and Delegates (WCF Hosted before Delegate is instantiated)
body: <p>I am using delegates to allow my service to grab a string that is stored in my form.
  When I run the code my service log states the following:</p>
  
  <p><code>System.NullReferenceException: Object reference not set to an instance of an object.</code></p>
  
  <p>Pointing towards <code>diData.DIDataCompressed = GetDIData();</code></p>
  
  <p>I tried setting a breakpoint at <code>DataServer = new ModelDataService();</code> in my form's constructor. And noticed that my WCF service was started (I got the popup from WcfSvcHost in tray and also in the WCF Service Host window it says ModelDataService has been started) before my form's constructor's code was run, so the delegate had obviously not been instantiated. <strong>Update: The Service is being started before the Main() method in my Program.cs is called (the method that starts the form). Also, my solution's only start up project is my form!</strong></p>
  
  <p>How can I get my WCF Service to only start when my form loads (so I can set the delegate correctly)?</p>
  
  <p>Here's my WCF Service Code:</p>
  
  <pre><code>[ServiceBehavior(UseSynchronizationContext = false, InstanceContextMode = InstanceContextMode.Single)]
  public class ModelDataService : IModelData
  {
      public delegate string GetData();
      public GetData GetDIData { get; set; }
  
      public ModelDataService()
      {
      }
  
      public DIData GetDData()
      {
          DIData diData = new DIData();
  
          diData.DIDataCompressed = GetDIData(); // **** error points here
  
          return diData;
      }
  }
  
  [DataContract]
  public class DIData
  {
      [DataMember]
      public string DIDataCompressed;
  }
  </code></pre>
  
  <p>And my form code that should start the service:</p>
  
  <pre><code>public partial class ScraperForm : Form
  {
      ServiceHost Host;
      ModelDataService DataServer;
  
      string DIData;
  
      public ScraperForm()
      {
          InitializeComponent();
  
          #region Start Data Server
          DataServer = new ModelDataService(); // I set breakpoint here
          DataServer.GetDIData = new ModelDataService.GetData(this.GetDIData);
  
          Host = new ServiceHost(DataServer, new Uri[]
              {
                  new Uri("http://localhost:8000")
              });
  
          Host.AddServiceEndpoint(typeof(IModelData),
              new BasicHttpBinding(),
              "ModelData");
  
          Host.Open();
  
          #endregion
  
          DIData = "";
      }
  
      public string GetDIData() 
      {
          return DIData; // This is updated on a timer
      }
  </code></pre>
  
  <p>My App.config</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
  
      &lt;appSettings&gt;
          &lt;add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /&gt;
      &lt;/appSettings&gt;
      &lt;system.web&gt;
          &lt;compilation debug="true" /&gt;
      &lt;/system.web&gt;
      &lt;!-- When deploying the service library project, the content of the config file must be added to the host's 
    app.config file. System.Configuration does not support config files for libraries. --&gt;
      &lt;system.serviceModel&gt;
          &lt;services&gt;
              &lt;service name="SoccerModelService.ModelDataService"&gt;
                  &lt;endpoint address="" binding="basicHttpBinding" 
                            bindingConfiguration="BasicHttpBinding" 
                            contract="SoccerModelService.IModelData"
                            name ="BasicHttpBinding_IModelData"&gt;
                      &lt;identity&gt;
                          &lt;dns value="localhost"/&gt;
                      &lt;/identity&gt;
                  &lt;/endpoint&gt;
                  &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&gt;
                  &lt;host&gt;
                      &lt;baseAddresses&gt;
                          &lt;add baseAddress="http://localhost:8000/ModelData/"/&gt;
                          &lt;!--//localhost:8733/Design_Time_Addresses/ModelDataService/Service1/" /&gt; --&gt;
                      &lt;/baseAddresses&gt;
                  &lt;/host&gt;
              &lt;/service&gt;
              &lt;!--&gt;&lt;service name="SoccerModelService.ModelDataService" behaviorConfiguration="debug"&gt;
              &lt;/service&gt; --&gt;
          &lt;/services&gt;
          &lt;bindings&gt;
              &lt;basicHttpBinding&gt;
                  &lt;binding name="BasicHttpBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"&gt;
                      &lt;readerQuotas maxDepth="32" maxStringContentLength="8388608" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
                  &lt;/binding&gt;
              &lt;/basicHttpBinding&gt;
          &lt;/bindings&gt;
          &lt;client&gt;
              &lt;endpoint address="http://localhost:8000/ModelData/" binding="basicHttpBinding"
                  bindingConfiguration="BasicHttpBinding" contract="SoccerModelService.IModelData"
                  name="EndPoint" behaviorConfiguration="EndpointBehaviour" /&gt;
                  &lt;!--&lt;endpoint address="http://localhost:8000/ModelData" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding" name="EndPoint" behaviorConfiguration="EndpointBehaviour" /&gt; --&gt;
          &lt;/client&gt;
          &lt;behaviors&gt;
              &lt;serviceBehaviors&gt;
                  &lt;behavior&gt;
                      &lt;!-- To avoid disclosing metadata information, 
            set the values below to false before deployment --&gt;
                      &lt;serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/&gt;
                      &lt;!-- To receive exception details in faults for debugging purposes, 
            set the value below to true.  Set to false before deployment 
            to avoid disclosing exception information --&gt;
                      &lt;serviceDebug includeExceptionDetailInFaults="False" /&gt;
                  &lt;/behavior&gt;
                  &lt;behavior name="debug"&gt;
                      &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
                  &lt;/behavior&gt;
              &lt;/serviceBehaviors&gt;
              &lt;endpointBehaviors&gt;
                  &lt;behavior name="EndpointBehaviour"&gt;
                      &lt;dataContractSerializer maxItemsInObjectGraph="2147483647" /&gt;
                  &lt;/behavior&gt;
              &lt;/endpointBehaviors&gt;
          &lt;/behaviors&gt;
      &lt;/system.serviceModel&gt;
  
      &lt;!-- Trace--&gt;
      &lt;system.diagnostics&gt;
          &lt;sources&gt;
              &lt;source name="System.ServiceModel" switchValue="Warning" propagateActivity="true" &gt;
                  &lt;listeners&gt;
                      &lt;add name="xml"/&gt;
                  &lt;/listeners&gt;
              &lt;/source&gt;
  
              &lt;source name="myUserTraceSource" switchValue="Warning, ActivityTracing"&gt;
                  &lt;listeners&gt;
                      &lt;add name="xml"/&gt;
                  &lt;/listeners&gt;
              &lt;/source&gt;
          &lt;/sources&gt;
  
          &lt;sharedListeners&gt;
              &lt;add name="xml"
                   type="System.Diagnostics.XmlWriterTraceListener"
                   initializeData="TraceLog.svclog" /&gt;
          &lt;/sharedListeners&gt;
          &lt;trace autoflush="true" /&gt;
      &lt;/system.diagnostics&gt;
  
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Thanks for your help!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I added a load event to my form. I am still getting the same error. I tried setting a breakpoint on the Main() method in my Program.cs for my form (the method that starts the form) and the service is started before the main method in Program.cs is even called! </p>
  
  <p>Could the problem be that a new instance of the service is created every time my client calls the service? I have set it up to be a singleton but have I done it incorrectly?</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I thought that I may have accidentally made my project a WCF Service Application, and not a WCF Service Library (which I could host inside a form). The bin for my project contains a .dll in the project's name. I believe that this means that it is indeed a Library. Please correct me if I'm wrong.</p>
  
  <p>Thanks again!</p>