来自 WCF 服务的 Silverlight 异常(故障)处理
我想从 wcf 方法获取异常代码,但总是收到 NotFound 错误。
客户端:
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???
}
}
服务器端:
[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; }
}
配置(includeExceptionDetailInFaults 设置为 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>
我需要在客户端获取 BaseFault 类型。怎么做呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Evgeny,你是如何创建你的客户端代理的?您的客户可以访问 BaseFault 类型吗?您收到什么类型的错误(未找到类型、未找到页面、未找到文件)?
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,
这里的问题是您收到错误 404。这是 WCF 服务之上的级别,由 IIS 处理和返回,因此您的请求永远不会到达您的 WCF 服务。您需要检查服务的端点 URL 和 .svc 文件/IIS 上的端点 URL 是否相同,并确保它们相同。实际上,我会尝试使用浏览器浏览端点 URL,看看会得到什么。
正如您的链接所解释的,您需要拥有能够转换为错误的代码,我假设您已经这样做了。
希望这有帮助。
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:
just add this line of code and it works without configuration.
在 Sliverlight 应用程序的 Application_Startup 事件处理程序中添加以下内容:
Add the following in the Application_Startup event handler of your Sliverlight app: