title: Windows Communication Foundation Null Reference Error
body: <p>I am trying to use Windows Communication Foundation(WCF) for testing purposes in my application and I am getting a null object. I am new to using WCF, so any help would be appreciated. </p>
  
  <p>From what I understand, each host has one service type(class). I have several interfaces and classes I want to test. My goal is to make one master class that has instances of the others, so I don't have to have several hosts running. Below is some of my code.</p>
  
  <p>Host-</p>
  
  <pre><code>public class AutomationWCFHost
  {
      public ServiceHost host;
  
      public AutomationWCFHost() {
          Uri httpUrl = new Uri("http://localhost:8090/MyFun");
          host = new ServiceHost(typeof(MyFun), httpUrl);
          ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
          smb.HttpGetEnabled = true;
          smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
          host.Description.Behaviors.Add(smb);
      }
  
      public void StartWCF() {
          this.host.Open();
      }
  
      public void StopWCF(){
          this.host.Close();
  </code></pre>
  
  <p>Interfaces - </p>
  
  <pre><code>[ServiceContract]
  public interface IMyFun {
      [OperationContract]
      string Echo(string msg);
  
      IDataSources DataSources { [OperationContract]get; [OperationContract]set; }
  }
  
  
  [ServiceContract]
  public interface IDataSources {
      [OperationContract]
      void Add(DataSource dataSource);
      [OperationContract]
      void Remove(DataSource dataSource);
  }
  </code></pre>
  
  <p>Class - I am trying to make one base class so I don't have to have multiple hosts running.</p>
  
  <pre><code>public class MyFun : IMyFun {
  
      public DataSources _dataSources;
  
      public MyFun () {
          _dataSources = new DataSources();
      }
  
      public string Echo(string msg) {
          return msg;
      }
  
      public IDataSources DataSources { get { return _dataSources; } set {_dataSources = (DataSources)value;} }
  }
  
  public class DataSources : IDataSources{
  ....//Several methods and properties
  }
  </code></pre>
  
  <p>Test -</p>
  
  <pre><code>public void MyTest() {
  
          EndpointAddress address = new EndpointAddress("http://localhost:8090/MyFun");
          BasicHttpBinding binding = new BasicHttpBinding();
          ChannelFactory&lt;IMyFun&gt; factory = new ChannelFactory&lt;IMyFun&gt;(binding, address);
          IMyFun channel = factory.CreateChannel();
          Assert.AreEqual("Test", channel.Echo("Test"));
          IDataSources dataSources = channel.DataSources;
          dataSources.Add(newDataSource);         
      }
  </code></pre>
  
  <p>As I step through, it runs the echo method fine, but when it does</p>
  
  <pre><code>IDataSources dataSources = channel.DataSources;
  </code></pre>
  
  <p>dataSources is null. This makes it so </p>
  
  <pre><code>dataSources.Add(newDataSource);
  </code></pre>
  
  <p>fails due to null exception error.</p>