gpt4 book ai didi

c# - 从 ASP.NET 客户端排除 WCF 故障 -- 帮助!

转载 作者:行者123 更新时间:2023-11-30 19:22:23 25 4
gpt4 key购买 nike

我正在尝试从 ASP.NET 应用程序调用我的服务中的一个方法,如下所示。

public bool ValidateUser(string username, string password)
{
try
{
// String CurrentLoggedInWindowsUserName = WindowsIdentity.GetCurrent().Name;
// //primary identity of the call
// String CurrentServiceSecurityContextPrimaryIdentityName =
// ServiceSecurityContext.Current.PrimaryIdentity.Name;
//
}
catch (Exception ex)
{
FaultExceptionFactory fct = new FaultExceptionFactory();
throw new FaultException<CustomFaultException>(fct.CreateFaultException(ex));
}
return false;
}

我的服务客户端配置如下

<binding name="WSHttpBinding_IMembershipService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false"
hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
textEncoding="utf-8" useDefaultWebProxy="false" allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>

我一直遇到的问题是我调用它的时候;我收到以下异常消息。

Server Error in '/' Application. 

The communication object, System.ServiceModel.Channels.ServiceChannel,
cannot be used for communication because it is in the Faulted state.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.ServiceModel.CommunicationObjectFaultedException:
The communication object, System.ServiceModel.Channels.ServiceChannel,
cannot be used for communication because it is in the Faulted state.

Stack Trace:

[CommunicationObjectFaultedException: The communication object,
System.ServiceModel.Channels.ServiceChannel, cannot be used for
communication because it is in the Faulted state.]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,
IMessage retMsg) +7596735
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData,
Int32 type) +275
System.ServiceModel.ICommunicationObject.Close(TimeSpan timeout) +0

System.ServiceModel.ClientBase`1.System.ServiceModel.ICommunicationObject.
Close(TimeSpan timeout) +142
System.ServiceModel.ClientBase`1.Close() +38
System.ServiceModel.ClientBase`1.System.IDisposable.Dispose() +4
Controls.Membership.accountLogin.ValidateUserCredentials(String UserName,
String Password) in C:\ Petition.WebClient\Controls\
Membership\accountLogin.ascx.cs:49
Controls.Membership.accountLogin.Login1_Authenticate(Object sender,
AuthenticateEventArgs e) in C:\ WebClient\ Controls\Membership
\accountLogin.ascx.cs:55

我不完全确定为什么我一直收到这个。为了以防万一,这是我从客户端调用我的服务的方式

private bool ValidateUserCredentials(string UserName, string Password)
{
bool boolReturnValue = false;

using(Members.MembershipServiceClient client =
new Controls.Members.MembershipServiceClient())
{
try
{
boolReturnValue = client.ValidateUser(UserName, Password);
}
catch (FaultException<CustomFaultException> ex)
{
throw ex;
}
}

return boolReturnValue;
}

有人知道我在这种情况下需要做什么吗?

最佳答案

WCF 客户端代理是 .NET 中您不应该围绕实现 IDisposable 的类的实例实现 using block 的两个地方之一。参见 Indisposable: WCF Gotcha #1 .

顺便说一句,去掉客户端调用周围的 try/catch block 。不要捕获异常,除非你能以某种方式“处理”它,再次抛出它并不是“处理”它。

关于c# - 从 ASP.NET 客户端排除 WCF 故障 -- 帮助!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1307737/

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