-1

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.

Below is the function I am using to call a service

public async void CallService(string reqdata, string partner)
        {
            BasicHttpBinding basicHttpBinding = null;
            EndpointAddress endpointAddress = null;
            ChannelFactory<IRequestChannel> 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<IRequestChannel>(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();
                }
            }
        }
    }

I am getting error at

OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestMessage;

NullReferenceException

Error

Community
  • 1
  • 1
Neu
  • 167
  • 3
  • 15

1 Answers1

0

If you directly create an instance of the service type and then you call a method on it, there is no OperationContext.The following example shows how to use the OperationContextScope to create a new context in a client application:

public class Client {
    public void run()
    {
        ServiceReference1.Service1Client service1Client = new Service1Client(new InstanceContext(this));
        using (OperationContextScope scope = new OperationContextScope(service1Client.InnerChannel)) {
            HttpRequestMessageProperty http = new HttpRequestMessageProperty();
            string apikey = "OAvHGAAatytiZno";
            http.Headers.Add("apikey", apikey);
            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = http;
            ServiceReference1.Result result = service1Client.GetUserData("Test");
        } 
    }
}
class Program

{
    static void Main(string[] args)
    {
        Client client = new Client();
        client.run();
    }
}

This is the reference link:

https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.operationcontext.-ctor?view=dotnet-plat-ext-3.1

Ding Peng
  • 3,702
  • 1
  • 5
  • 8
  • Thank you Ding Peng for your response. I am using your approach but at Using() I am getting this error "The communication object, System.ServiceModel.ChannelFactory`1[QuoteEngineService.IMarketingSoftware], cannot be used for communication because it is in the Faulted state.". – Neu May 18 '20 at 00:50
  • This is a problem with the server. you need to catch the exception information to see what happens. – Ding Peng May 18 '20 at 01:20
  • There is a demo in the link I give you. You can refer to it. – Ding Peng May 18 '20 at 01:21