views:

36

answers:

3

I want get exeption code from wcf method but i always get NotFound error.

Client Side:

public MainPage()
    {
        InitializeComponent();
        client.TestCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(TestCompleted);
    }

    void TestCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        if(e.Error!=null)
        {
            //HOW to get here my class BaseFault???
        }
    }

Server side:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [FaultContract(typeof(BaseFault))]
    void Test(int id);
}

  public void Test(int id)
  {
            try
            {
                if (id == -1)
                    ThrowEx(new BaseFault() { ErrorCode = ProcessErrorsCode.InvalidArgument });
                else
                    throw new NullReferenceException("some server error with null value");
            }
            catch
            {
                ThrowEx(new BaseFault() { ErrorCode = ProcessErrorsCode.InternalServerError });
            }
   }


 public void ThrowEx(BaseFault fault)
 {
    throw new FaultException<BaseFault>(fault);
 }



    [DataContract]
    public class BaseFault
    {
        [DataMember]
        public ProcessErrorsCode ErrorCode { get; set; }
    }

Config (includeExceptionDetailInFaults set to True):

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>

    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">

                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="True" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
</configuration>

I need to get BaseFault type on my client side. How to do that?

+1  A: 

Evgeny, how have you created your client proxy? Does your client have access to BaseFault type? What kind of error do you get (type not found, page not found, file not found)?

Aliostad
Hi! I created client proxy automatically by visual studio. Yes i have access to BaseFault type. Exception: –CommunicationException
Evgeny
Thanks for the point (that's my first)! So the error is CommunicationException and not NotFound? Can you please print the e.Error.ToString() and send to me?
Aliostad
I mean in if(e.Error!=null) { Console.WriteLine(e.Error.ToString()); }
Aliostad
System.ServiceModel.CommunicationException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.And without throwing exception it always working fine (function return normal data)
Evgeny
Can you check it in test my test project?
Evgeny
Not really because this is bound to your configuration and I could be getting different error. It appears that buffer size has to do with it and needs to be setup. Have a look here: http://forums.silverlight.net/forums/t/40770.aspx
Aliostad
Apparantly "because Silverlight has absolutely no concept of a fault".
Aliostad
actually in my configuration buffer already set to max value... And if silverlight 4 has no concept of a fault how can we get error from server? And link: http://msdn.microsoft.com/en-us/library/ee844556(VS.95).aspx
Evgeny
+1  A: 

Evgeny,

The problem here is that you are getting error 404. This is at the level above WCF service and is handled and returned by the IIS so your request never hits your WCF service. You need to check the endpoint URL of your service and the same on your .svc file/IIS and make sure they are the same. I would actually try browsing to the endpoint URL using a browser and see what I get.

As your link explains, you need to have the code to be able to cast to fault and I assume you are already doing that.

Hope this helps.

Aliostad
Sorry, but end point is defined. I can debug server methods with my client and they working normally, but error handling is a big problem.
Evgeny
+1  A: 

Found easy solution for me:

        bool registerResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);

just add this line of code and it works without configuration.

Evgeny