异步调用WCF的方法需要小心的地方
直接使用下面的代码,由于client对象占用的资源没有被释放,会导致内存泄露
GetSimServiceReference.GetSimServiceClient client = new GetSimServiceReference.GetSimServiceClient()
client.computerSimAsync(DepartmentNo, FileID, F_intput.Length, ReadStringArrayFromStrings());
上面的问题,可以使用下面的方法来避免
using (GetSimServiceReference.GetSimServiceClient client = new GetSimServiceReference.GetSimServiceClient()) { client.computerSimA(DepartmentNo, FileID, F_intput.Length, ReadStringArrayFromStrings()); }
但是如果使用异步调用的方式,会导致调用不到远程服务,并且没有任何异常提示,很是让人摸不到头脑
using (GetSimServiceReference.GetSimServiceClient client = new GetSimServiceReference.GetSimServiceClient()) { client.computerSimAsync(DepartmentNo, FileID, F_intput.Length, ReadStringArrayFromStrings()); }
最后,采用了如下的代码:
try { GetSimServiceReference.GetSimServiceClient client = new GetSimServiceReference.GetSimServiceClient(); client.computerSim(DepartmentNo, FileID, F_intput.Length, ReadStringArrayFromStrings()); if (client.State != CommunicationState.Faulted) client.Close(); } catch (CommunicationObjectFaultedException cofe) { ShowException.SaveLogAsTXTInfoex("调用WCF服务失败,异常信息:" + cofe.Message); } catch (Exception ex) { ShowException.SaveLogAsTXTInfoex("调用WCF服务失败,异常信息:" + ex.Message+f_intput.FullName); }