basic fault contract not received correctly in my rest web service using https with ssl and basic authentication

I am trying to do a basic FaultException in my HTTPS using SSL and basic authentication.
The problem is when receiving the exception, the client receives the error as WebException... not the FaultException like intended.

I am also overriding a section in my service, not sure if it matters.  The exception I am trying to trap is in the MyServiceAuthorizationManager class.

      <serviceBehaviors>
        <behavior name="SecureRESTSvcTestBehavior">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceAuthorization serviceAuthorizationManagerType="EngineAppServiceV001.MyServiceAuthorizationManager, EngineAppServiceV001"/>

Open in new window


this is in my IService1.cs
    [DataContract]
    public class ServiceData
    {
        [DataMember]
        public bool Result { get; set; }
        [DataMember]
        public string ErrorMessage { get; set; }
        [DataMember]
        public string ErrorDetails { get; set; }
    }

Open in new window


this is the error section in my MyServiceAuthorizationManager which hits properly

                    myServiceData.Result = true;
                    myServiceData.ErrorMessage = "this is user error message";
                    myServiceData.ErrorDetails = "user error details";
                    throw new FaultException<ServiceData>(myServiceData, "user reason error");

Open in new window


any ideas why it ALWAYS gets received on client as web exception?

here is how I try and receive on client
                catch (FaultException<ServiceData> ex)
                {
                    var x1 = ex;
                }

Open in new window

Stephen ForeroAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
Have you defined a FaultContract on your server side functions?
[OperationContract]
[FaultContract(typeof(ServiceData))]
int MyFunction(int param1, int param2....);

Open in new window


Have a look at: http://www.codeproject.com/Articles/555415/A-Beginners-Tutorial-for-Understanding-Exception-H
0
Stephen ForeroAuthor Commented:
Mlanda, I followed the example you gave me and the exception on the client still goes right to WEBEXCEPTION...

in IService1.cs
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [FaultContract(typeof(MyExceptionContainer))]
        [WebGet(UriTemplate = "/Login",
          ResponseFormat = WebMessageFormat.Json,
          RequestFormat = WebMessageFormat.Json,
          BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        DataSet Login();

Open in new window


    [DataContract]
    public class MyExceptionContainer
    {
        [DataMember]
        public string Message { get; set; }

        [DataMember]
        public string Description { get; set; }
    }

Open in new window



then in client
                try
                {
                    var varResult = client.DownloadString(mainwindow.webServiceConnection_LOGIN);
                    var serializer = new DataContractJsonSerializer(typeof(DataSet));
                    using (var stream = new MemoryStream(Encoding.ASCII.GetBytes(varResult)))
}
                catch (FaultException<MyExceptionContainer> ex)
                {
                    var x1 = 1;
                }
                //catch (FaultException<ServiceData> ex)
                //{
                //    var x1 = ex;
                //}
                catch (FaultException ex)
                {
                    var x2 = 1;
                }
                catch (CommunicationException ex)
                {
                    var x3 = 3;
                }
                catch (WebException ex)
                {
                    var x4 = 1;
                }

Open in new window

0
Stephen ForeroAuthor Commented:
could it be a setting in my web.config file?
0
MlandaTCommented:
I wonder what it could be. This is what I have:

Client consuming the service (code from a button client event):
            try
            {
                var client = new ServiceReference1.Service1Client();
                client.ErrorMethod();
            }
            catch (FaultException<ServiceReference1.MyExceptionContainer> ex)
            {
                MessageBox.Show(ex.Detail.Message, ex.Detail.Description);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Standard Exception");
            }

Open in new window

My client's app.config
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:59004/Service1.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
                name="BasicHttpBinding_IService1" />
        </client>
    </system.serviceModel>

Open in new window

And my WCF Service Contract
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
        [OperationContract]
        [FaultContract(typeof(MyExceptionContainer))]
        string ErrorMethod();

    }

    [DataContract]
    public class MyExceptionContainer
    {
        [DataMember]
        public string Message { get; set; }

        [DataMember]
        public string Description { get; set; }
    }
    
    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

Open in new window

And the actual service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
        public string ErrorMethod()
        {
            throw new FaultException<MyExceptionContainer>(
                new MyExceptionContainer { Description = "the error description", Message = "the message of the error" },
                new FaultReason("This method was implemented to always throw an error") /*FaultReason is required*/
            );
        }

        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}

Open in new window

And the web.config
<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

Open in new window

You will notice how I am creating the
             throw new FaultException<MyExceptionContainer>(
                new MyExceptionContainer { Description = "the error description", Message = "the message of the error" },
                new FaultReason("This method was implemented to always throw an error") /*FaultReason is required*/
            );

Open in new window

And that is exactly what I am getting on the client sideResult... I get my faultException
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Stephen ForeroAuthor Commented:
thank you, I am going to start from scratch using your example to see where the error could possible be
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.