gpt4 book ai didi

c# - 无法访问已处置的对象 - wcf 客户端

转载 作者:太空狗 更新时间:2023-10-30 00:55:18 24 4
gpt4 key购买 nike

我的 WCF 客户端有问题。
有时我会收到此异常:无法访问已处置的对象。这就是我打开连接的方式:

private static LeverateCrmServiceClient crm = null;

public static CrmServiceClient Get(string crmCertificateName)
{

if (crm != null)
{
crm.Close();
}
try
{
crm = new LeverateCrmServiceClient("CrmServiceEndpoint");
crm.ClientCredentials.ClientCertificate.SetCertificate(
StoreLocation.LocalMachine,
StoreName.My,
X509FindType.FindBySubjectName,
crmCertificateName);
}
catch (Exception e)
{
log.Error("Cannot access CRM ", e);
throw;
}
return crm;
}

如您所见,我每次都关闭并重新打开连接。您认为可能是什么问题?

堆栈:

System.ServiceModel.Security.MessageSecurityException: Message security verification failed. ---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.ServiceModel.Security.SymmetricSecurityProtocol'.
at System.ServiceModel.Channels.CommunicationObject.ThrowIfClosedOrNotOpen()
at System.ServiceModel.Security.MessageSecurityProtocol.VerifyIncomingMessage(Message& message, TimeSpan timeout, SecurityProtocolCorrelationState[] correlationStates)
--- End of inner exception stack trace ---

Server stack trace:
at System.ServiceModel.Security.MessageSecurityProtocol.VerifyIncomingMessage(Message& message, TimeSpan timeout, SecurityProtocolCorrelationState[] correlationStates)
at System.ServiceModel.Channels.SecurityChannelFactory`1.SecurityRequestChannel.ProcessReply(Message reply, SecurityProtocolCorrelationState correlationState, TimeSpan timeout)
at System.ServiceModel.Channels.SecurityChannelFactory`1.SecurityRequestChannel.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.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at Externals.CrmService.ICrmService.GetTradingPlatformAccountDetails(Guid ownerUserId, String organizationName, String businessUnitName, Guid tradingPlatformAccountId)
at MyAppName.Models.ActionsMetadata.Trader.BuildTrader(Guid tradingPlatformAccountId) in C:\Users\X\Documents\Visual Studio 2010\Projects\MyAppName\MyAppName\Models\ActionsMetadata\Trader.cs:line 120
at MyAppName.Models.ActionsMetadata.Trader.Login(String accountNumber, String password) in C:\Users\X\Documents\Visual Studio 2010\Projects\MyAppName\MyAppName\Models\ActionsMetadata\Trader.cs:line 48
at MyAppName.Models.ActionsMetadata.Handlers.LoginHandler.Handle(StepHandlerWrapper wrapper) in C:\Users\X\Documents\Visual Studio 2010\Projects\MyAppName\MyAppName\Models\ActionsMetadata\Handlers\LoginHandler.cs:line 23
at MyAppName.Models.ActionsMetadata.Handlers.HandlerInvoker.Invoke(IAction brokerAction, ActionStep actionStep, Dictionary`2 stepValues, HttpContext httpContext, BaseStepDataModel model) in C:\Users\X\Documents\Visual Studio 2010\Projects\MyAppName\MyAppName\Models\ActionsMetadata\Handlers\StepServerInoker.cs:line 42
at MyAppName.Controllers.LoginController.Login(String step) in C:\Users\X\Documents\Visual Studio 2010\Projects\MyAppName\MyAppName\Controllers\LoginController.cs:line 35
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

最佳答案

根据该堆栈跟踪,我假设您有一个 ASP.NET MVC 应用程序,其中包含一些调用 Get 方法以获取 CrmServiceClient 对象的代码,然后继续对该 CrmServiceClient 对象调用各种方法。例如,登录过程的一部分就是这样做的。

您的 Get 方法的工作方式是,每次调用它时,它都会先关闭它先前返回的 CrmServiceClient 对象(无论它是否被调用)仍在使用中)然后创建并返回一个新的。

假设两个用户几乎同时尝试登录您的应用程序 - 彼此相差几毫秒。处理第一个用户登录的线程调用 Get 并获取其 CrmServiceClient 对象,然后在一毫秒后处理第二个用户登录的线程调用 Get,这会导致第一个线程的 CrmServiceClient 对象关闭。但是,第一个线程仍在运行,现在当它尝试调用其 CrmServiceClient 对象上的方法时,它会收到 System.ObjectDisposedException:无法访问已处置的对象

"As you can see, I am closing and reopening the connection each time."

Get 方法中当前的代码不是实现此目的的好方法。相反,您应该让调用者负责关闭(或处置)CrmServiceClient 对象,并且您应该将 Get 方法重命名为 OpenCreate 来建议这个。调用者应使用 using 语句来确保对象已关闭/处置,无论是否发生任何异常:

using (CrmServiceClient client = CrmServiceFactory.Get("my-crm-certificate"))
{
client.Something();
client.GetTradingPlatformAccountDetails();
client.SomethingElse();
} // client is automatically closed at the end of the 'using' block

关于c# - 无法访问已处置的对象 - wcf 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10010662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com