【发布时间】: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