Error Reporting in WCF Service?

天大地大妈咪最大 提交于 2019-12-11 07:31:51

问题


I have a self hosted Net Tcp WCF service and I have the following method

        Subscribtion GetSubscribtion(int subscribtionId)
        {
            Subscribtion s;
            if (_subscribtionTable.TryGetValue(subscribtionId, out s) == false)
            {
                Console.WriteLine("Not Found SessionID");//for debugging
                throw new ArgumentException("Invalid Subscription Id");
            }
            return s;
        }

Is throwing exception is a good practice for reporting erros in service programming?


回答1:


In WCF you should instead be defining and throwing a FaultException. The following articles provide examples and details:

  • Exception Handling in WCF Web Service
  • Developing a WCF Service - Fault Exceptions AND FAULT Contracts
  • Specifying and Handling Faults in Contracts and Services (MSDN)



回答2:


Since WCF inherently can be interoperable with non-.NET platforms, using Exceptions which is a strict .NET construct is a bad idea. It will also usually break your WCF connection, if you have one (e.g. in a session scenario).

The way to go is using interoperable SOAP faults that are transportable even to non-.NET clients.

Marc



来源:https://stackoverflow.com/questions/992883/error-reporting-in-wcf-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!