【问题标题】:getting System.NullReferenceException: 'Object reference not set to an instance of an object.' when calling a service获取 System.NullReferenceException:“对象引用未设置为对象的实例。”调用服务时
【发布时间】:2020-05-14 16:02:03
【问题描述】:

我正在使用 .net 核心并使用 WCF 服务。当我在 header 中传递 Api 键并调用服务时,它会引发 NullReferenceException 错误。

下面是我用来调用服务的函数

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;

【问题讨论】:

  • 在此处设置断点并检查其中的每个部分以确定其中哪一个是 null - 这应该会为您提供更多信息,以便您可以继续。

标签: c# web-services wcf .net-core


【解决方案1】:

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

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

【讨论】:

  • 感谢丁鹏的回复。我正在使用您的方法,但在 Using() 我收到此错误“通信对象 System.ServiceModel.ChannelFactory`1[QuoteEngineService.IMarketingSoftware],不能用于通信,因为它处于故障状态。”。跨度>
  • 这是服务器的问题。您需要捕获异常信息以查看发生了什么。
  • 我给你的链接里有demo。你可以参考一下。
猜你喜欢
  • 2013-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-21
  • 2021-04-09
  • 1970-01-01
相关资源
最近更新 更多