我目前有一个简单的用例。
1)使用Castle的AsWcfClient选项连接到WCF服务的客户端应用程序。
2)WCF服务" A"使用Castle托管并注入单个依赖项。此依赖关系是另一个WCF服务的客户端代理(Call it Service" B")。
3)服务" B"做了一些工作。
要想象:客户 - >服务" A"使用Castle注入代理 - >服务" B"
简单吧?工作没有问题IF,这是一个很大的if,服务" B"主机已启动并正在运行
我所看到的并且可以根据需要重现的行为是,如果服务" B"在没有任何暗示存在任何问题的情况下,调用链完成了。换句话说,Castle没有抛出任何解决方案异常,也没有任何WCF异常。我已将此与IOneWay =如何处理的操作隔离开来。
这是一个主要问题,因为您认为一切都已正确执行,但实际上您的代码都没有执行过!
这是预期的行为吗?有没有我可以在Castle中打开一些选项,以便在WCF客户端代理是已解析的依赖项时它将抛出异常?还有其他选择吗?
还有一点需要注意的是,你遇到的唯一问题就是/当你在客户端代理上执行Container.Release()时会发生异常。由于各种原因,这不能依赖于你。
感谢!
此外,下面是重新创建此问题的代码。运行它 1)在Visual Studio中创建一个新的单元测试项目 2)通过NuGet添加Castle Windsor WCF集成工具 3)将下面的代码粘贴到.cs文件中,所有内容都集于一身,以方便使用。 4)运行两个单元测试,SomeOperation_With3Containers_NoException()作为依赖服务(Service" B"来自上面)运行。 SomeOperation_With2Containers_NoException()失败是.Release 5)设置断点,您可以看到实现中没有命中代码。
**** UPDATE ****:需要处理的主要方法是使用IErrorHandler植入(正如Roman在下面的评论中所提到的)。可以在此处找到详细信息和示例:http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler(v=vs.110).aspx
使用此实现记录单向操作上的任何异常,并使用该数据采取适当的操作。
using Castle.Facilities.WcfIntegration;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace UnitTestProject1
{
[ServiceContract]
public interface IServiceContractA
{
[OperationContract(IsOneWay = true)]
void SomeOperation();
}
[ServiceContract]
public interface IServiceDependancy
{
[OperationContract]
void SomeOperation();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class ServiceContractAImplementation : IServiceContractA
{
private IServiceDependancy ServiceProxy;
public ServiceContractAImplementation() { }
public ServiceContractAImplementation(IServiceDependancy dep)
{
ServiceProxy = dep;
}
public void SomeOperation()
{
ServiceProxy.SomeOperation();
}
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class ServiceDependancyImplementation : IServiceDependancy
{
public void SomeOperation()
{
//do nothing, just want to see if we can create an instance and hit the operation.
//if we need to do something, do something you can see like: System.IO.File.Create(@"d:\temp\" + Guid.NewGuid().ToString());
}
}
public class ServiceCastleInstaller : IWindsorInstaller
{
public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{
container.AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero);
var returnFaults = new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true, HttpHelpPageEnabled = true };
container.Register(Component.For<IServiceBehavior>().Instance(returnFaults));
//local in-proc service hosting
var namedPipeBinding = new NetNamedPipeBinding();
//it works using Named Pipes
var serviceModelPipes = new DefaultServiceModel().AddEndpoints(
WcfEndpoint.BoundTo(namedPipeBinding).At("net.pipe://localhost/IServiceContractA")
).Discoverable();
container.Register(Component.For<IServiceContractA>()
.ImplementedBy<ServiceContractAImplementation>()
.LifeStyle.PerWcfOperation()
.AsWcfService(serviceModelPipes)
);
//our service (IServiceContractA) has a dependancy on another service so needs a client to access it.
container.Register(Castle.MicroKernel.Registration.Component.For<IServiceDependancy>()
.AsWcfClient(WcfEndpoint.BoundTo(namedPipeBinding)
.At(@"net.pipe://localhost/IServiceDependancy")).LifeStyle.Transient);
}
}
public class ServiceDependancyCastleInstaller : IWindsorInstaller
{
public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{
container.AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero);
var returnFaults = new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true, HttpHelpPageEnabled = true };
container.Register(Component.For<IServiceBehavior>().Instance(returnFaults));
//local in-proc service hosting
var namedPipeBinding = new NetNamedPipeBinding();
var serviceModel = new DefaultServiceModel().AddEndpoints(
WcfEndpoint.BoundTo(namedPipeBinding).At("net.pipe://localhost/IServiceDependancy")
).Discoverable();
container.Register(Component.For<IServiceDependancy>()
.ImplementedBy<ServiceDependancyImplementation>()
.LifeStyle.PerWcfOperation()
.AsWcfService(serviceModel)
);
}
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void SomeOperation_With3Containers_NoException()
{
//setup the container that is going to host the service dependancy
using (var dependancyContainer = new WindsorContainer().Install(new ServiceDependancyCastleInstaller()))
{
//container that host the service that the client will call.
using (var serviceContainer = new WindsorContainer().Install(new ServiceCastleInstaller()))
{
//client container, nice and simple so doing it in the test here.
using (var clientContainer = new WindsorContainer())
{
clientContainer.AddFacility<WcfFacility>();
var endpoint = WcfEndpoint.BoundTo(new NetNamedPipeBinding())
.At("net.pipe://localhost/IServiceContractA");
clientContainer.Register(Castle.MicroKernel.Registration.Component.For<IServiceContractA>()
.AsWcfClient(endpoint).LifeStyle.Transient);
var proxy = clientContainer.Resolve<IServiceContractA>();
proxy.SomeOperation();
clientContainer.Release(proxy);
}
}
}
}
[TestMethod]
public void SomeOperation_With2Containers_NoException()
{
//this one fails.
// this test omits the dependancy that the IServiceContractA has
//Note that all seems to work, the only hint you have that it doesnt
//is the .Release call throws and exception.
//container that host the service that the client will call.
using (var serviceContainer = new WindsorContainer().Install(new ServiceCastleInstaller()))
{
//client container, nice and simple so doing it in the test here.
using (var clientContainer = new WindsorContainer())
{
clientContainer.AddFacility<WcfFacility>();
var endpoint = WcfEndpoint.BoundTo(new NetNamedPipeBinding())
.At("net.pipe://localhost/IServiceContractA");
clientContainer.Register(Castle.MicroKernel.Registration.Component.For<IServiceContractA>()
.AsWcfClient(endpoint).LifeStyle.Transient);
var proxy = clientContainer.Resolve<IServiceContractA>();
//this call seems like it works but any break points
//in code don't get hit.
proxy.SomeOperation();
//this throws and exception
clientContainer.Release(proxy);
}
}
}
}
}
答案 0 :(得分:4)
出于“即发即弃”方案的目的,存在单向操作。您不会 关注 关于结果,是否成功。您不必 等待 以使服务器响应(如果是HTTP绑定,则只有初始TCP握手)。通过使用单向操作,客户端只能确保服务器 在线路上成功接收 消息,并且服务器无法保证它将成功处理信息。在HTTP协议中也是如此。在其他协议(如Microsoft MSMQ或IBM MQ)中,服务器甚至不需要与客户端同时联机。
在您的方案中,客户端不会收到异常,因为服务A已启动并正在运行。如果服务A关闭,你会看到一个错误(再次,假设HTTP,或在你的情况下.net管道)。服务B的条件无关紧要,因为服务B是服务A的实现细节,而您的客户端不关心服务A的返回值。如果您在服务B关闭时调试服务A(通过附加),您将看到第一次机会,甚至可能是未处理的异常(取决于服务A的实现)。
Castle无论如何都不应该抛出异常,因为它已经成功解决了服务A中服务B的代理。服务B关闭的事实不关心Castle或任何其他DI容器。