简体   繁体   中英

Silverlight exception (faults) handling from wcf service

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?

在Sliverlight应用程序的Application_Startup事件处理程序中添加以下内容:

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

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

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.

Found easy solution for me:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM