我必须在.Net Core 2.0项目中使用SOAP服务。我添加了服务参考,如以下链接所述:( Missing link, 404 )
某些方法的服务正常。但是,有些方法需要很长时间(由于操作服务正在进行),在这种情况下程序抛出异常:“System.Net.Http.WinHttpException:操作超时”
我尝试设置超时值,但没有效果。
MyService.MyServiceSoapClient Pr =
new MyService.MyServiceSoapClient(
new MyService.MyServiceSoapClient.EndpointConfiguration());
Pr.InnerChannel.OperationTimeout = new TimeSpan(0, 10, 0);
我还查看了以下链接,其中包含.Net Core中有关wcf / soap服务的相关问题:GitHub: dotnet/wcf
但到目前为止还没有解决方法。知道如何解决这个问题吗?
答案 0 :(得分:1)
好的如果有人遇到同样的问题,我发布了我找到的解决方案。似乎其他人也遇到了这个问题并提出问题here
据我所知,这是.Net Core 2.0的一般错误,如果请求持续时间超过30秒,则会发生错误。
从上方链接可以看出,他们发布了一个解决方案here。它对我有用。解决方案需要在发出实际请求之前每次都提出虚假的服务请求,以便更改超时设置。为了避免这种情况,我可以建议你捕捉" CommunicationException",并在该捕获中应用解决方案。因此,只有在您真正需要的时候才能拨打额外的电话。我正在复制代码,以防链接死亡(所有代码为https://github.com/mconnew/)
static void Main(string[] args)
{
var client = new SimpleServiceClient();
client.OpenAsync().GetAwaiter().GetResult();
client.DelayedResponseAsync(2000).GetAwaiter().GetResult();
var channel = client.InnerChannel;
var httpChannelFactory = client.InnerChannel.GetProperty<IChannelFactory>();
var cacheField = httpChannelFactory.GetType().GetField("_httpClientCache", BindingFlags.NonPublic | BindingFlags.Instance);
var httpClientCache = cacheField.GetValue(httpChannelFactory);
var cacheDictionaryField = httpClientCache.GetType().GetField("_items", BindingFlags.NonPublic | BindingFlags.Instance);
IDictionary cacheDictionary = (IDictionary)cacheDictionaryField.GetValue(httpClientCache);
foreach(var cacheKey in cacheDictionary.Keys)
{
var cacheEntry = cacheDictionary[cacheKey];
var valueField = cacheEntry.GetType().GetField("value", BindingFlags.NonPublic | BindingFlags.Instance);
HttpClient httpClient = (HttpClient)valueField.GetValue(cacheEntry);
FixHttpClient(httpClient);
}
client.DelayedResponseAsync(50000).GetAwaiter().GetResult();
Console.WriteLine("Done");
Console.ReadLine();
}
private static void FixHttpClient(HttpClient httpClient)
{
var handlerField = typeof(HttpMessageInvoker).GetField("_handler", BindingFlags.NonPublic | BindingFlags.Instance);
DelegatingHandler delegatingHandler = (DelegatingHandler)handlerField.GetValue(httpClient); // Should be of type ServiceModelHttpMessageHandler
WinHttpHandler winHttpHandler = (WinHttpHandler)delegatingHandler.InnerHandler;
WinHttpHandler newHandler = new WinHttpHandler();
newHandler.ServerCredentials = winHttpHandler.ServerCredentials;
newHandler.CookieUsePolicy = winHttpHandler.CookieUsePolicy;
newHandler.ClientCertificates.AddRange(winHttpHandler.ClientCertificates);
newHandler.ServerCertificateValidationCallback = winHttpHandler.ServerCertificateValidationCallback;
newHandler.Proxy = winHttpHandler.Proxy;
newHandler.AutomaticDecompression = winHttpHandler.AutomaticDecompression;
newHandler.PreAuthenticate = winHttpHandler.PreAuthenticate;
newHandler.CookieContainer = winHttpHandler.CookieContainer;
// Fix the timeouts
newHandler.ReceiveHeadersTimeout = Timeout.InfiniteTimeSpan;
newHandler.ReceiveDataTimeout = Timeout.InfiniteTimeSpan;
newHandler.SendTimeout = Timeout.InfiniteTimeSpan;
var servicemodelHttpHandlerInnerHandlerField = delegatingHandler.GetType().GetField("_innerHandler", BindingFlags.NonPublic | BindingFlags.Instance);
servicemodelHttpHandlerInnerHandlerField.SetValue(delegatingHandler, newHandler);
var delegatingHandlerInnerHandlerField = typeof(DelegatingHandler).GetField("_innerHandler", BindingFlags.NonPublic | BindingFlags.Instance);
delegatingHandlerInnerHandlerField.SetValue(delegatingHandler, newHandler);}