[英]How to increase the timeout values for a WCF service in a dot net core 2.1 project
我發布此消息是因為我無法在 Stack Overflow 上找到任何通過連接服務添加服務引用來解決使用 WCF 的 .Net-Core 項目的問題的地方。
我的問題是由於長時間運行的操作請求,我面臨客戶端超時。
那么,如何增加 wcf 客戶端對象的超時值,因為 .Net-Core 不再使用 web 配置來存儲 WCF 服務引用的配置值? (請參閱我提供的答案)
在解決方案資源管理器中的連接服務下,添加 WCF 服務后,會為該服務生成一些文件。 您應該會看到一個文件夾,其名稱是您為 WCF 服務引用提供的名稱,在該文件夾下有一個Getting Started
、 ConnectedService.json
和一個Reference.cs
文件。
要增加任何客戶端服務對象的超時值,請打開Reference.cs
並找到方法: GetBindingForEndpoint
在此方法中,您應該看到如下內容:
if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_IYourService))
{
System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
result.MaxBufferSize = int.MaxValue;
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
result.MaxReceivedMessageSize = int.MaxValue;
result.AllowCookies = true;
//Here's where you can set the timeout values
result.SendTimeout = new System.TimeSpan(0, 5, 0);
result.ReceiveTimeout = new System.TimeSpan(0, 5, 0);
return result;
}
只用result.
以及您想要增加的超時時間,如SendTimeout
、 ReceiveTimeout
等,並將其設置為具有所需超時值的新 TimeSpan。
我希望這被證明是對某人有用的帖子。
Ryan Wilson 的回答將起作用,但僅在您嘗試更新服務之前。 Reference.cs 將被覆蓋。 在 .NET Core 3.1 中,您可以在語法上修改綁定超時:
public MemoqTMServiceClass(string api_key)
{
client = new TMServiceClient();
var eab = new EndpointAddressBuilder(client.Endpoint.Address);
eab.Headers.Add(
AddressHeader.CreateAddressHeader("ApiKey", // Header Name
string.Empty, // Namespace
api_key)); // Header Value
client.Endpoint.Address = eab.ToEndpointAddress();
client.Endpoint.Binding.CloseTimeout = new TimeSpan(2, 0, 0);
client.Endpoint.Binding.OpenTimeout = new TimeSpan(2, 0, 0);
client.Endpoint.Binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
client.Endpoint.Binding.SendTimeout = new TimeSpan(0, 10, 0);
}
只需在生成的代理類中實現以下部分方法即可配置服務端點。 將部分方法放在您自己的文件中以確保它不會被覆蓋。
static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.