我正在尝试创建一些自定义FaultException.我做了一个DataContract叫做的课CreateFault.
[DataContract]
public class CreateFault
{
private string report;
public CreateFault(string message)
{
this.report = message;
}
[DataMember]
public string Message
{
get { return this.report; }
set { this.report = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将故障抛在服务方法中.
在 IService1.cs
[OperationContract]
[FaultContract(typeof(CreateFault))]
void TestFaultException();
Run Code Online (Sandbox Code Playgroud)
并在 Service1.cs
public void TestFaultException()
{
throw new FaultException<CreateFault>(new CreateFault("CreateFault message"), "Message abt exception");
}
Run Code Online (Sandbox Code Playgroud)
我抓住了FaultException我的客户.
private void btnTest_Click(object sender, RoutedEventArgs e)
{
try
{
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(); …Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个Windows Phone 7应用程序,它调用我也控制的WCF Web服务.该服务提供的操作在给定用户的登录名和密码时返回当前用户的帐户信息:
[ServiceContract]
public interface IWindowsPhoneService
{
[OperationContract]
[FaultContract(typeof(AuthenticationFault))]
WsAccountInfo GetAccountInfo(string iamLogin, string password);
}
Run Code Online (Sandbox Code Playgroud)
当然,总是存在身份验证失败的可能性,我想将这些信息传达给WP7应用程序.在这种情况下我可以简单地返回null,但我想传达身份验证失败的原因(即登录未知,密码错误,帐户被阻止,......).
这是我对上述操作的实现(出于测试目的,它只是抛出异常):
public WsAccountInfo GetAccountInfo(string iamLogin, string password)
{
AuthenticationFault fault = new AuthenticationFault();
throw new FaultException<AuthenticationFault>(fault);
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我在我的WP7应用程序中调用此操作,如下所示:
Global.Proxy.GetAccountInfoCompleted += new EventHandler<RemoteService.GetAccountInfoCompletedEventArgs>(Proxy_GetAccountInfoCompleted);
Global.Proxy.GetAccountInfoAsync(txbLogin.Text, txbPassword.Password);
void Proxy_GetAccountInfoCompleted(object sender, RemoteService.GetAccountInfoCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
return;
}
}
Run Code Online (Sandbox Code Playgroud)
调试器在Reference.cs中断,说FaultException'1未处理,这里:
public PhoneApp.RemoteService.WsAccountInfo EndGetAccountInfo(System.IAsyncResult result) {
object[] _args = new object[0];
PhoneApp.RemoteService.WsAccountInfo _result = ((PhoneApp.RemoteService.WsAccountInfo)(base.EndInvoke("GetAccountInfo", _args, result)));
return _result;
}
Run Code Online (Sandbox Code Playgroud)
开始更新1
按F5时,异常气泡到:
public …Run Code Online (Sandbox Code Playgroud) 我有一个关于如何发送自定义异常作为FaultException的问题.当我使用像ArgumentException这样的系统异常时,它可以工作,但是如果我将它更改为我的自定义异常"TestException",它就会失败.当我尝试添加它时,我无法获得服务引用的配置.
作品:
[OperationContract]
[FaultContract(typeof(ArgumentException))]
[TransportChannel TestMethod ();
public Void TestMethod()
{
throw new FaultException<ArgumentException>(new ArgumentException("test"), new FaultReason("test"));
}
Run Code Online (Sandbox Code Playgroud)
不起作用:
[OperationContract]
[FaultContract(typeof(TestException))]
[TransportChannel TestMethod ();
public Void TestMethod()
{
throw new FaultException<TestException>(new TestException("test"), new FaultReason("test"));
}
Run Code Online (Sandbox Code Playgroud)
我的"TestException"看起来像这样:
[Serializable()]
public class TestException: Exception
{
public TestException () : base() { }
public TestException (string message) : base(message) { }
public TestException (string message, Exception innerException) : base(message, innerException) { }
public TestException (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
} …Run Code Online (Sandbox Code Playgroud) wcf exception-handling exception custom-exceptions faultexception
单层应用程序可通过以下方式区分异常:
Exception ex;
if (ex is System.DirectoryServices.AccountManagement.PasswordException)
...
Run Code Online (Sandbox Code Playgroud)
ex只是一个普遍的例外.
当您转移到WCF以获得多层时,您将丢失所有这些并且需要使用FaultException机制.
问题是我找不到任何方法来做到这一点.
在我的客户端中,我想捕获FaultException类型,然后区分它们,例如:
catch (FaultException ex)
{
if FaultException is (PasswordExceptionFault)
...
etc
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
否则我必须有许多catch构造 - 每种类型的FaultException都有一个.
对于以下异常,异常窗口中未指定原因(->查看详细信息):
System.ServiceModel.FaultException 此错误的创建者未指定原因。我该如何更改它以显示原因?(我需要在那里显示 1234)
public class MysFaultException
{
public string Reason1
{
get;
set;
}
}
MyFaultException connEx = new MyFaultException();
connEx.Reason1 = "1234";
throw new FaultException<OrdersFaultException>(connEx);
Run Code Online (Sandbox Code Playgroud) 我有一个WCF服务,抛出一个异常,我试图在我的Silverlight客户端代码中捕获不成功.我使用Undeclared Faults进行调试,这是我的服务方法:
[OperationContract]
public ServiceResponse MyWCFServiceMethod()
{
ServiceResponse resp = new ServiceResponse ();
//code for setting resp...
//purposely throw error here.
throw new FaultException(new FaultReason(new FaultReasonText("My fault Reason!")),new FaultCode("my fault code here"));
return resp;
}
Run Code Online (Sandbox Code Playgroud)
现在在我的silverlight客户端视图模型中,在服务的回调方法中,我尝试像这样处理它:
private void MyServiceCallback(MyWCFServiceMethodCompletedEventArgs e)
{
if (e.Error == null)
{
//proceed normally
}
else if (e.Error is FaultException)
{
FaultException<ExceptionDetail> fault = e.Error as FaultException<ExceptionDetail>;
MessageBox.Show(fault.Detail.Message);
MessageBox.Show(fault.Reason.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
在这一行else if (e.Error is FaultException) 我仍然得到System.Net.WebException {远程服务器返回错误:NotFound.}
这些是配置条目
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" /> …Run Code Online (Sandbox Code Playgroud) 如何捕获任何Exception派生的TDetail的FaultException?
我尝试过,catch( FaultException<Exception> ) {}但似乎没有用.
编辑
目的是获取对Detail属性的访问权限.
一些跟踪显示正在抛出CommunicationException,因为我的异常T没有正确序列化的问题; 因为,两层深,我有一个匿名类型的对象,这是不可序列化的.删除它并冒泡更改似乎解决了它.在此之前还有其他小事我做过,但是我不记得我的生活是什么,只是在配置中没有完成.
我从我的踪迹中收到消息,例如:
Type 'RebuiltWCFService.Requests.HelloWorldRequest' with data contract name 'HelloWorldRequest:http://schemas.datacontract.org/2004/07/RebuiltWCFService.Requests' is not expected.
Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
Run Code Online (Sandbox Code Playgroud)
我今天遇到了一个看似奇怪的问题,我无法找到答案!
问题:当我抛出FaultException时,我的服务抛出了一个CommunicationException!如果我不抛出异常,它不会这样做.
在我的服务中,我正确地定义了错误合同:
[OperationContract]
[FaultContract(typeof(Faults.HelloWorldFault))]
Responses.HelloWorldResponse HelloWorld(Requests.HelloWorldRequest parameter);
Run Code Online (Sandbox Code Playgroud)
然后在错误条件下我抛出了正确类型的异常:
if (_errors.Count() > 0)
{
Faults.HelloWorldFault fault = new Faults.HelloWorldFault(_errors);
throw new FaultException<Faults.HelloWorldFault>(fault, new FaultReason("There are …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用带有.net 4.5的VS 2012中的Telerik OpenAccess的WCF Plain服务.
我尝试了telerik开发人员手册并创建了服务和客户端.
在我放的IEntitiesModel服务接口中
[OperationContract]
[FaultContract(typeof(string))]
PersistentDto ReadPersistentDto(string dtoKey);
Run Code Online (Sandbox Code Playgroud)
在方法的EntitiesModel.SVC中,我首先使用了一个简单的构造:
public PersistentDto ReadPersistentDto(string dtoKey)
{
throw new FaultException("test");
}
Run Code Online (Sandbox Code Playgroud)
在Consumer的用户代码中,我为异常设置了一个catch.
现在的问题是:
每次调用服务时,Visual Studio都会在"throw new FaultException"中停止并显示错误消息,即用户代码未处理FaultException.如果我继续使用F5,则异常也会被消费者的用户代码捕获.
为什么它会在服务中停止?
根据我的理解,FaultException应该传递给服务的使用者.
我该怎么做才能正确抛出FaultException?
我正在尝试在WCF服务中实现异常处理.我希望将此异常抛出到客户端,以便记录和处理它.
我不知道我写的代码是对还是错.这只会让我"错误的用户代码未解决"我应该如何解决,以便我可以在客户端处理我的异常?
WCF服务中的代码:
try
{
}
catch (FaultException fex)
{
throw fex;
}
catch (Exception ex)
{
throw ex;
}
Run Code Online (Sandbox Code Playgroud)
客户代码:
try
{
}
catch (FaultException fex)
{
Logger.AddExceptionToDb(fex);
}
catch (Exception ex)
{
Logger.AddExceptionToDb(ex);
}
Run Code Online (Sandbox Code Playgroud) 我需要抛出wcf异常而不向客户端显示堆栈跟踪,只需要消息...
抛出新的FaultException("ex1");
我平均有20个异常字符串.我如何实现它而不是每次使用字符串参数抛出FaultException,而是抛出异常对象
FaultException_i i = 1...20
Run Code Online (Sandbox Code Playgroud) faultexception ×11
wcf ×11
exception ×5
c# ×4
.net ×3
.net-4.0 ×1
asynchronous ×1
silverlight ×1
telerik ×1