title: getting System.NullReferenceException: 'Object reference not set to an instance of an object.' when calling a service
body: <p>I am using .net core and consumed a WCF service. When I pass Api key in header and call a service , it throws NullReferenceException error.</p>
  <p>Below is the function I am using to call a service</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public async void CallService(string reqdata, string partner)
          {
              BasicHttpBinding basicHttpBinding = null;
              EndpointAddress endpointAddress = null;
              ChannelFactory&lt;IRequestChannel&gt; factory = null;
              QuoteEngineService.MarketingSoftwareClient service = new QuoteEngineService.MarketingSoftwareClient();          
              try
              {
                  basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
                  basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
                  endpointAddress = new EndpointAddress(new Uri("https://Service-engine-dev.110apps.net/Package/TempSoftware.svc?wsdl"));
                  factory = new ChannelFactory&lt;IRequestChannel&gt;(basicHttpBinding, endpointAddress);
                  IRequestChannel channel = factory.CreateChannel();
                  HttpRequestMessageProperty httpRequestMessage = new HttpRequestMessageProperty();
                  string apikey = "OAvHGAAatytiZno";
                  httpRequestMessage.Headers.Add("apikey", apikey);
                  OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestMessage;
                 
                      service.OpenAsync();
                      var result2 = await service.PRequestAsync(reqedata, partner);
                      var data = result2;   
              }
              catch(Exception e)
              {
                  throw;
              }
              finally
              {
                  if (service.State==System.ServiceModel.CommunicationState.Opened)
                  {
                      service.CloseAsync();
                  }
              }
          }
      }</code></pre>
  </div>
  </div>
  </p>
  <p>I am getting error at</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestMessage;</code></pre>
  </div>
  </div>
  </p>
  <p><a href="https://i.stack.imgur.com/LXNVg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LXNVg.png" alt="NullReferenceException" /></a></p>
  <p><a href="https://i.stack.imgur.com/usAeq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/usAeq.png" alt="Error" /></a></p>