Troubleshooting WCF from ASP.NET Client - Help!

I am trying to call a method on my service as shown below from an ASP.NET application.

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

      

The config for my client ends with below

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

      

The problem I keep when I call it; I am getting the following exception message.

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

      

I'm not really sure why I keep doing this. Just in case, this is how I call my service from the client

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

      

Does anyone know what I need to do in this case?

+2


source to share


4 answers


WCF Proxies are one of two places in .NET where you shouldn't implement a block using

around the instance of the class that implements IDisposable

. See Don't: WCF Gotcha # 1 .



OBTW, get rid of the try / catch block around your call in the client. Don't catch an exception if you can't "handle" it in some way, and throwing it again doesn't "handle" it.

+5


source


I'm sure you probably know about this, but if not, WCF tracing might be helpful in these situations.

Especially in scenarios where the code is posted is in production and not updated, but an unexpected error occurs on the server side, e.g. one that you did not specify in the contract.

Also, the ability to view the payload of a message can be invaluable.

To get tracing, enable WCF tracing in app.config or equivalent.



Depending on where you think the error most likely occurred (client or server or both), where you probably want to enable tracing.

Then, use the Service Tracer tool to open the XML log files it generates.

On my machine, it's in C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\SvcTraceViewer.exe

, but your location is probably slightly different.

For more information, see the Service Tracer page on MSDN.

+4


source


Okay, with your current client setup, you expect to encrypt and sign your messages and send your Windows credentials as an identity.

If the remote server that is trying to process this request is NOT on the same local network or does not have access to the same Active Directory to verify these user credentials, you will receive this message.

The security settings on the server and the client must match - for example, if your server has something like

<security mode="None">

      

then you should also have the same settings on the client. If your server cannot validate Windows credentials, do not use them. Either don't use security at all (not recommended except for development / testing), or allow anonymous callers (but still use message security, for example), or you need to define a custom mechanism where the caller can authenticate.

Security in WCF is a vast field :-) Here are some links that might help you grasp the topic:

and for a really complete but almost intimidating overview of all the security-related things of WCF, see the WCF Security Guide on CodePlex.

Hope this helps you get started!

Mark

+1


source


Message: "CommunicationObjectFaultedException" usually occurs when you do not Dispose on channels using a block, so you only need to add an Abort statement like this:

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

      

Hope this helps you.

0


source







All Articles