2009-08-20 141 views
2

我想從ASP.NET應用程序中調用我的服務中的方法,如下所示。ASP.NET客戶端WCF故障排除 - 幫助!

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; 
} 

任何人有任何想法,我需要在這種情況下怎麼辦?

回答

5

WCF客戶端代理是.NET中的兩個地方之一,您不應該在實現IDisposable的類的實例周圍實現using塊。見Indisposable: WCF Gotcha #1

OBTW,擺脫客戶端呼叫的try/catch塊。除非你能以某種方式「處理」它,否則不要捕捉異常,而再次拋出它並不是「處理」它。

+1

上面的鏈接似乎已被破壞。這裏有一個替代方案:http://web.archive.org/web/20080803051143/www.iserviceoriented.com/blog/post/Indisposable+-+WCF+Gotcha+1.aspx – Kit 2010-09-27 15:11:10

1

那麼,利用您當前的客戶端設置,您希望對您的消息進行加密和簽名,並將您的Windows憑據作爲身份發送。

如果嘗試處理此請求的遠程服務器是而不是在同一個LAN上,或者無法訪問相同的Active Directory以驗證這些用戶憑據,您將收到此消息。

服務器和客戶端上的安全設置必須匹配 - 例如,如果你的服務器有類似的東西

<security mode="None"> 

那麼你必須在客戶端上也有相同的設置。如果您的服務器無法驗證Windows憑據,請不要使用它們。根本不使用任何安全性(除開發/測試之外,不建議使用)或允許匿名調用者(但仍然使用例如消息安全性),或者您必須定義調用者可以在其中對自身進行身份驗證的自定義機制。

安全在WCF是一個廣闊的領域:-)這裏有一些鏈接,可以幫助獲得關於這一主題的抓地力:

和所有的東西安全WCF相關的一個非常完整的,而且幾乎令人生畏概述安全,有在CodePlex上的WCF Security Guidance

希望這可以幫助你開始!

馬克

4

我敢肯定,你可能意識到了這一點,但如果沒有,WCF跟蹤可以證明在這些情況下很有幫助。

特別是在代碼已發貨,正在生產並且不可更新的情況下,但在服務器端引發了意外錯誤,例如,一個你沒有在合同中指定的。

也能夠查看消息的有效載荷可以是非常寶貴的。

要獲取跟蹤,請啓用app.config中的WCF跟蹤或等效功能。

這取決於您認爲最可能發生錯誤的位置(客戶端或服務器或兩者),以確定您可能想要打開跟蹤的位置。

然後使用Service Trace Viewer工具打開它生成的XML日誌文件。

在我的機器上是C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\SvcTraceViewer.exe,但是你的位置可能有點不同。

請參閱MSDN上的Service Trace Viewer頁面以獲取更多信息。

0

消息:「CommunicationObjectFaultedException」通常發生了,當你不適用處置與使用塊工作的渠道,所以你只需要添加一個聲明中止這樣的:

catch (Exception ex) 
{ 
    client.Abort(); 
} 

我希望這能幫你。