使用Castle WCF Facility托管服务时包含异常详细信息

时间:2010-08-20 14:23:21

标签: c# wcf mocking castle-windsor

在利用温莎城堡的Wcf设施时,我在测试一些代码时遇到了一些麻烦。当抛出Exception时,它似乎拒绝包含Exception Details,我只能看到空的FaultExceptions。这是我的测试设置:

首先,这是我要连接的服务的存根:

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public abstract class StubAilDataService : IAilDataService
{
    public virtual Method1()
    {
    }
    /* More methods */
}

请注意,我已指定IncludeExceptionDetailsInFaults并将其设为true

这是我托管存根服务的方式:

private ServiceHost _host;
private StubAilDataService _rhinoService;

[TestFixtureSetUp]
public void FixtureSetup()
{
    var sba = new ServiceDebugBehavior {IncludeExceptionDetailInFaults = true};
    _rhinoService = MockRepository.GeneratePartialMock<StubAilDataService>();

    _host = new ServiceHost(_rhinoService);
    _host.AddServiceEndpoint(typeof(IAilDataService), new WSHttpBinding("wsSecure"), "http://localhost:8080/Service");
    _host.Open();

    _container.AddFacility<WcfFacility>().Register(
        Component.For<IServiceBehavior>().Instance(sba),
        Component.For<IAilDataService>()
            .LifeStyle.PerWcfSession()
            .ActAs(new DefaultClientModel
                       {
                           Endpoint =
                               WcfEndpoint.BoundTo(new WSHttpBinding("wsSecure"))
                               .At("http://localhost:8080/Service")
                       }) // More stuff
        );
}

我尝试在模拟对象上保留Include ..属性时完成了PartialMock。

并且测试。请注意,我告诉我的模拟服务在这里抛出一个非常具体的异常。

[Test]
[ExpectedException(typeof(AggregateException))]
public void AnalyzeProductCreationJobs_Should_Throw_Aggregate_Exception_If_A_DataService_Call_Throws()
{
    //Arrange
    _rhinoService.Expect(
    s => s.CategoryIsInAgility(Arg<string>.Matches(str => str.Equals("000103")), Arg<Settings>.Is.Anything))
    .Throw(new FaultException<InvalidOperationException>(new InvalidOperationException("FAIL!")));

    var product = new Product { CategoryCode = "000103" };

    var analyzer = TypeResolver.Resolve<ProductAnalyzer>();

    //Act
    analyzer.AnalyzeProductCreationJobs(product);
}

最后,我正在测试的代码:

public class ProductAnalyzer
{
    private readonly IDataServiceClient _dataClient;

    public ProductAnalyzer(IDataServiceClient dataClient)
    {
        _dataClient = dataClient;
    }

    public IEnumerable<IAdsmlJob<CreateResponse>> AnalyzeProductCreationJobs(Product product)
    {
        IList<IAdsmlJob<CreateResponse>> creationJobs = new List<IAdsmlJob<CreateResponse>>();

        var task = Task.Factory.StartNew(() =>
        {
            // This is where the exception set up in my .Expect gets thrown.
            bool categoryIsInAgility = _dataClient.CategoryIsInAgility(product.CategoryCode);
            // Logic
        }); // Continued by more tasks

    try
    { task.Wait(); }
    catch (AggregateException ae)
    {
        ae.Flatten().Handle(ex => ex is TaskCanceledException);
    }
}

我希望服务崩溃并抛出我设置它的异常 - 但是Wcf Facility似乎剥离了引发的异常并用一个空的FaultException替换它。

我错过了什么吗?这里有很多组件在一起工作 - 而且我不能100%确定哪里出了问题。

1 个答案:

答案 0 :(得分:0)

您必须显式声明方法在接口上抛出的错误类型。 例如:

[ServiceContract(Namespace = "http://www.example.com")]
public interface IAilDataService
{
    [OperationContract]
    [FaultContract(typeof(OperationPermissionFault))]
    [FaultContract(typeof(InvalidOperationException))]
    void Method1();
}

请参阅http://msdn.microsoft.com/en-us/library/system.servicemodel.faultcontractattribute.aspx