Wcf服务可在.NET Core 3.1控制台应用程序中使用,但在ASP.NET Core 3.1 Web API中无法使用

问题描述 投票:0回答:1

遇到工作中的特殊问题。尝试连接到基于https的自定义客户端证书的已连接服务。我正在使用BasicHttpsBinding和ChannelFactory类创建该服务的客户端。

可以连接到服务并在3.1控制台应用程序上获取响应,但是当我将相同的代码放入3.1 Web API控制器中时,当我尝试使用错误“的客户端调用相同的操作时,它将失败。没有端点监听...。这可能是由于不正确的肥皂操作引起的。”内部异常显示“ winhttp异常。服务器名称或地址无法解析”

我尝试运行fiddler来查看有什么不同,对于控制台应用程序,我看到建立了到外部服务URL的隧道,但是对于Web API内的调用,我在Fiddler上看不到任何东西,这意味着它在Web API管道内的某个地方失败了本身。

在Google上搜索时指向检查代理设置,因为这是在公司网络上,但是netsh winhttp show proxy显示直接(无代理服务器),所以我不认为这是公司代理相关的问题。

3.1控制台应用程序和Web API在网络堆栈中究竟有什么不同,WCF服务调用可能在Web API中失败,但在控制台应用程序中起作用?

有问题的代码

var binding = new BasicHttpsBinding();
binding.Security.Mode = BasicHttpsSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
var endpoint = new EndpointAddress(new Uri("https://service url"));
var channelFactory = new ChannelFactory<ExternalServiceInterface>(binding, endpoint);
channelFactory.Credentials.ClientCertificate.Certificate = new X509Certificate2(certificatepath, password);

_client = channelFactory.CreateChannel();

var sbmtGtDtResp = _client.ExternalServiceOperation(data);

已删除敏感信息,例如服务URL,并替换了由ExternalServiceInterface将Connected Service添加到项目时生成的接口名称。。NET Core 3.1 Web API中的_client.ExternalServiceOperation()获得异常,但在.NET Core 3.1控制台应用程序中有效

异常转储

{System.ServiceModel.EndpointNotFoundException: There was no endpoint listening at "https://service url" that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
 ---> System.Net.Http.HttpRequestException: An error occurred while sending the request.
 ---> System.Net.Http.WinHttpException (80072EE7, 12007): The server name or address could not be resolved
   at System.Threading.Tasks.RendezvousAwaitable`1.GetResult()
   at System.Net.Http.WinHttpHandler.StartRequest(WinHttpRequestState state)
   --- End of inner exception stack trace ---
   at System.ServiceModel.Channels.ServiceModelHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpClientRequestChannel.HttpClientChannelAsyncRequest.SendRequestAsync(Message message, TimeoutHelper timeoutHelper)
   --- End of inner exception stack trace ---
   at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(HttpRequestException requestException, HttpRequestMessage request, HttpAbortReason abortReason)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpClientRequestChannel.HttpClientChannelAsyncRequest.SendRequestAsync(Message message, TimeoutHelper timeoutHelper)
   at System.ServiceModel.Channels.RequestChannel.RequestAsync(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.RequestAsyncInternal(Message message, TimeSpan timeout)
   at System.Runtime.TaskHelpers.WaitForCompletionNoSpin[TResult](Task`1 task)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(MethodCall methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(MethodInfo targetMethod, Object[] args)
--- End of stack trace from previous location where exception was thrown ---
   at System.Reflection.DispatchProxyGenerator.Invoke(Object[] args)
   at generatedProxy_1.submitGetDataRequest(submitGetDataRequestRequest )
   at WebAPIPOC.Controllers.ServiceController.Process() in "Code filepath.cs":line 50
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()}

遇到工作中的特殊问题。尝试连接到基于https的自定义客户端证书的已连接服务。我正在使用BasicHttpsBinding和ChannelFactory类为...

wcf asp.net-core-webapi asp.net-core-3.1 winhttp .net-core-3.1
1个回答
0
投票

所以,就像平常一样,我真的很愚蠢。但是万一其他人犯了和我一样的错误,我会列出来。

© www.soinside.com 2019 - 2024. All rights reserved.