获取 System.NullReferenceException: '未将对象引用设置为对象的实例.'调用服务时 [英] getting System.NullReferenceException: 'Object reference not set to an instance of an object.' when calling a service

查看:23
本文介绍了获取 System.NullReferenceException: '未将对象引用设置为对象的实例.'调用服务时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .net 核心并使用 WCF 服务.当我在标头中传递 Api 密钥并调用服务时,它会抛出 NullReferenceException 错误.

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();
                }
            }
        }
    }

我在

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

推荐答案

如果直接创建服务类型的实例,然后在其上调用方法,则没有 OperationContext.以下示例演示如何使用 OperationContextScope在客户端应用程序中创建新上下文:

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();
    }
}

这是参考链接:

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

这篇关于获取 System.NullReferenceException: '未将对象引用设置为对象的实例.'调用服务时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆