我是一个C#wcf服务,其中我的GetProducts()方法的代码如下所示。
using (var client = CreateHttpClient("/myURL", string.Empty))
{
HttpResponseMessage response = client.GetAsync(client.BaseAddress.AbsolutePath).Result;
if (response.IsSuccessStatusCode)
{
var products= response.Content.ReadAsAsync<List<Product>>().Result;
lstProducts.AddRange(products);
}
else
{
throw new Exception("Error fetching products...");
}
}
此服务中有另一个方法A()调用此方法,并抛出以下内存不足异常:
System.AggregateException: One or more errors occurred. ---> System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at System.Threading.Thread.StartInternal(IPrincipal principal, StackCrawlMark& stackMark)
at System.Threading.Thread.Start(StackCrawlMark& stackMark)
at System.Threading.Thread.Start()
at System.Net.TimerThread.Prod()
at System.Net.TimerThread.TimerQueue.CreateTimer(Callback callback, Object context)
at System.Net.ServicePoint..ctor(Uri address, Queue defaultIdlingQueue, Int32 defaultConnectionLimit, String lookupString, Boolean userChangedLimit, Boolean proxyServicePoint)
at System.Net.ServicePointManager.FindServicePointHelper(Uri address, Boolean isProxyServicePoint)
at System.Net.ServicePointManager.FindServicePoint(Uri address, IWebProxy proxy, ProxyChain& chain, HttpAbortDelegate& abortDelegate, Int32& abortState)
at System.Net.HttpWebRequest.FindServicePoint(Boolean forceFind)
at System.Net.HttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)
at System.Net.Http.HttpClientHandler.StartGettingResponse(RequestState state)
at System.Net.Http.HttpClientHandler.StartRequest(Object obj)
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
MyService.ProductsManager.GetProducts()
--> (Inner Exception #0) System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at System.Threading.Thread.StartInternal(IPrincipal principal, StackCrawlMark& stackMark)
at System.Threading.Thread.Start(StackCrawlMark& stackMark)
at System.Threading.Thread.Start()
at System.Net.TimerThread.Prod()
at System.Net.TimerThread.TimerQueue.CreateTimer(Callback callback, Object context)
at System.Net.ServicePoint..ctor(Uri address, Queue defaultIdlingQueue, Int32 defaultConnectionLimit, String lookupString, Boolean userChangedLimit, Boolean proxyServicePoint)
at System.Net.ServicePointManager.FindServicePointHelper(Uri address, Boolean isProxyServicePoint)
at System.Net.ServicePointManager.FindServicePoint(Uri address, IWebProxy proxy, ProxyChain& chain, HttpAbortDelegate& abortDelegate, Int32& abortState)
at System.Net.HttpWebRequest.FindServicePoint(Boolean forceFind)
at System.Net.HttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)
at System.Net.Http.HttpClientHandler.StartGettingResponse(RequestState state)
at System.Net.Http.HttpClientHandler.StartRequest(Object obj)<---
我已经使用&#34;使用&#34;在GetProducts()方法中阻止。所以只是想知道我还能在代码中做些什么来防止这种内存不足异常? 或者可能是&#34; myURL&#34;服务方?
或者会听取帮助吗? 1.将构建平台目标更改为x64。 2.在服务配置文件中添加以下内容?
<runtime>
<gcAllowVeryLargeObjects enabled="true" />
</runtime>
请告知。
感谢您的帮助。