Antineutrino
Antineutrino

Reputation: 1093

TransactionCompleted and Castle Windsor

I use the Castle Windosr and its WcfFacility as IoC container and for creating WCF clients. The generated clients only seem to work when I configure them with singleton lifestyle. The code is called in a Transaction.TransactionCompleted event handler.

internal sealed class ViewsEventServicesGateway : IViewsEventServicesGateway
{
    private readonly Lazy<CreatedEventService.V3_1.ISystemStructureViewCreatedEventService> viewCreatedEventService;

    public ViewsEventServicesGateway(Lazy<CreatedEventService.V3_1.ISystemStructureViewCreatedEventService> viewCreatedEventService)
    {
        this.viewUpdatedEventService = viewUpdatedEventService;
    }

    public void RaiseCreated(IEnumerable<Guid> ids)
    {
        viewCreatedEventService.Value.Raised(new CreatedEventService.V3_1.RaisedNotification(null, CreatedEventService.V3_1.RaisedNotificationAssembler.ToContract(ids)));
    }
  }

Castle is configured as shown below:

  windsorContainer.Register(WcfClient.ForChannels(model).Configure(r => r.LifestyleTransient()));

This results in the following error when calling the client.

System.Net.Sockets.SocketException ist aufgetreten.
HResult=-2147467259
Message=Eine vorhandene Verbindung wurde vom Remotehost geschlossen
Source=System
ErrorCode=10054
NativeErrorCode=10054
StackTrace:
    bei System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size,   SocketFlags socketFlags)


System.dll!System.Net.Sockets.Socket.Receive(byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags) + 0x6d Bytes  
System.ServiceModel.dll!System.ServiceModel.Channels.SocketConnection.ReadCore(byte[] buffer, int offset, int size, System.TimeSpan timeout, bool closing) + 0xd0 Bytes 
System.ServiceModel.dll!System.ServiceModel.Channels.SocketConnection.Read(byte[] buffer, int offset, int size, System.TimeSpan timeout) + 0x84 Bytes   
System.ServiceModel.dll!System.ServiceModel.Channels.ConnectionUpgradeHelper.InitiateUpgrade(System.ServiceModel.Channels.StreamUpgradeInitiator upgradeInitiator, ref System.ServiceModel.Channels.IConnection connection, System.ServiceModel.Channels.ClientFramingDecoder decoder, System.ServiceModel.IDefaultCommunicationTimeouts defaultTimeouts, ref System.Runtime.TimeoutHelper timeoutHelper) + 0xe4 Bytes  
System.ServiceModel.dll!System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.SendPreamble(System.ServiceModel.Channels.IConnection connection = {System.ServiceModel.Channels.BufferedConnection}, System.ArraySegment<byte> preamble, ref System.Runtime.TimeoutHelper timeoutHelper) + 0x23d Bytes  
System.ServiceModel.dll!System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.DuplexConnectionPoolHelper.AcceptPooledConnection(System.ServiceModel.Channels.IConnection connection, ref System.Runtime.TimeoutHelper timeoutHelper) + 0x55 Bytes  
System.ServiceModel.dll!System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnection(System.TimeSpan timeout) + 0x21d Bytes    
System.ServiceModel.dll!System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(System.TimeSpan timeout) + 0x68 Bytes 
System.ServiceModel.dll!System.ServiceModel.Channels.CommunicationObject.Open(System.TimeSpan timeout) + 0x2e4 Bytes    
System.ServiceModel.dll!System.ServiceModel.Channels.CommunicationObject.Open(System.TimeSpan timeout) + 0x2e4 Bytes    
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.OnOpen(System.TimeSpan timeout) + 0xdd Bytes    
System.ServiceModel.dll!System.ServiceModel.Channels.CommunicationObject.Open(System.TimeSpan timeout) + 0x2e4 Bytes    
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannelProxy.ExecuteMessage(object target, System.Runtime.Remoting.Messaging.IMethodCallMessage methodCall = {System.Runtime.Remoting.Messaging.Message}) + 0xaa Bytes  
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannelProxy.InvokeChannel(System.Runtime.Remoting.Messaging.IMethodCallMessage methodCall) + 0x219 Bytes   
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannelProxy.Invoke(System.Runtime.Remoting.Messaging.IMessage message = {System.Runtime.Remoting.Messaging.Message}) + 0x1c0 Bytes 
mscorlib.dll!System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(ref System.Runtime.Remoting.Proxies.MessageData msgData, int type) + 0x1f4 Bytes   
Castle.Facilities.WcfIntegration.dll!Castle.Facilities.WcfIntegration.WcfClientActivator.GetChannelCreator.AnonymousMethod__1() + 0x4c Bytes    
Castle.Facilities.WcfIntegration.dll!Castle.Facilities.WcfIntegration.WcfChannelHolder.WcfChannelHolder(Castle.Facilities.WcfIntegration.ChannelCreator channelCreator, Castle.Facilities.WcfIntegration.IWcfBurden burden, System.TimeSpan? closeTimeout) + 0x90 Bytes
Castle.Facilities.WcfIntegration.dll!Castle.Facilities.WcfIntegration.WcfClientActivator.Instantiate(Castle.MicroKernel.Context.CreationContext context) + 0x137 Bytes  
Castle.Windsor.dll!Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(Castle.MicroKernel.Context.CreationContext context) Zeile 66 + 0x10 Bytes C#
Castle.Windsor.dll!Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(Castle.MicroKernel.Context.CreationContext context, Castle.MicroKernel.Burden burden) Zeile 80 + 0xe Bytes   C#
Castle.Windsor.dll!Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(Castle.MicroKernel.Context.CreationContext context, bool trackedExternally) Zeile 81    C#
Castle.Windsor.dll!Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve(Castle.MicroKernel.Context.CreationContext context, Castle.MicroKernel.IReleasePolicy releasePolicy) Zeile 70 + 0xf Bytes  C#
Castle.Windsor.dll!Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(Castle.MicroKernel.Context.CreationContext context, bool requiresDecommission, bool instanceRequired, out Castle.MicroKernel.Burden burden = null) Zeile 148 + 0x29 Bytes C#
Castle.Windsor.dll!Castle.MicroKernel.Handlers.DefaultHandler.Resolve(Castle.MicroKernel.Context.CreationContext context, bool instanceRequired) Zeile 93 + 0x16 Bytes  C#
Castle.Windsor.dll!Castle.MicroKernel.DefaultKernel.ResolveComponent(Castle.MicroKernel.IHandler handler, System.Type service, System.Collections.IDictionary additionalArguments, Castle.MicroKernel.IReleasePolicy policy) Zeile 742 + 0x16 Bytes C#
Castle.Windsor.dll!Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(System.Type service, System.Collections.IDictionary arguments, Castle.MicroKernel.IReleasePolicy policy) Zeile 234 + 0x16 Bytes  C#
Castle.Windsor.dll!Castle.MicroKernel.DefaultKernel.Resolve<Views.CreatedEventService.V3_1.ISystemStructureViewCreatedEventService>() Zeile 77 + 0x28 Bytes C#
mscorlib.dll!System.Lazy<Views.CreatedEventService.V3_1.ISystemStructureViewCreatedEventService>.CreateValue() + 0x1f1 Bytes    
mscorlib.dll!System.Lazy<Views.CreatedEventService.V3_1.ISystemStructureViewCreatedEventService>.LazyInitValue() + 0x20c Bytes  
...
System.Transactions.dll!System.Transactions.VolatileEnlistmentPreparing.EnterState(System.Transactions.InternalEnlistment enlistment = {System.Transactions.Phase1VolatileEnlistment}) + 0x15c Bytes    
System.Transactions.dll!System.Transactions.TransactionStateVolatilePhase1.EnterState(System.Transactions.InternalTransaction tx) + 0xf7 Bytes  
System.Transactions.dll!System.Transactions.PreparingEnlistment.Prepared() + 0x11a Bytes    
NHibernate.dll!NHibernate.Transaction.AdoNetWithDistributedTransactionFactory.DistributedTransactionContext.System.Transactions.IEnlistmentNotification.Prepare(System.Transactions.PreparingEnlistment preparingEnlistment = {System.Transactions.PreparingEnlistment}) Zeile 126  C#
System.Transactions.dll!System.Transactions.VolatileEnlistmentPreparing.EnterState(System.Transactions.InternalEnlistment enlistment = {System.Transactions.InternalEnlistment}) + 0x15c Bytes  
System.Transactions.dll!System.Transactions.TransactionStatePhase0.EnterState(System.Transactions.InternalTransaction tx) + 0x97 Bytes  
System.Transactions.dll!System.Transactions.CommittableTransaction.Commit() + 0x18e Bytes   
System.ServiceModel.dll!System.ServiceModel.Dispatcher.TransactionInstanceContextFacet.Complete(System.Transactions.Transaction transaction, System.Exception error) + 0x85 Bytes   
System.ServiceModel.dll!System.ServiceModel.Dispatcher.TransactionInstanceContextFacet.CompletePendingTransaction(System.Transactions.Transaction transaction, System.Exception error) + 0x72 Bytes 
System.ServiceModel.dll!System.ServiceModel.Dispatcher.TransactionRpcFacet.Complete(System.Exception error) + 0xb2 Bytes    
System.ServiceModel.dll!System.ServiceModel.Dispatcher.TransactionBehavior.ResolveOutcome(ref System.ServiceModel.Dispatcher.MessageRpc rpc) + 0x4ba Bytes  
System.ServiceModel.dll!System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ResolveTransactionOutcome(ref System.ServiceModel.Dispatcher.MessageRpc rpc = {System.ServiceModel.Dispatcher.MessageRpc}) + 0xc2 Bytes 
System.ServiceModel.dll!System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage9(ref System.ServiceModel.Dispatcher.MessageRpc rpc) + 0x50 Bytes 
System.ServiceModel.dll!System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage8(ref System.ServiceModel.Dispatcher.MessageRpc rpc) + 0x16a Bytes    
System.ServiceModel.dll!System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(ref System.ServiceModel.Dispatcher.MessageRpc rpc) + 0x146 Bytes    
System.ServiceModel.dll!System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(ref System.ServiceModel.Dispatcher.MessageRpc rpc) + 0x17b Bytes   
System.ServiceModel.dll!System.ServiceModel.Dispatcher.MessageRpc.Process(bool isOperationContextSet = false) + 0x17e Bytes 
System.ServiceModel.dll!System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump(System.ServiceModel.Channels.RequestContext request = {System.ServiceModel.Dispatcher.DuplexChannelBinder.DuplexRequestContext}, bool cleanThread, System.ServiceModel.OperationContext currentOperationContext) + 0x7ee Bytes 
System.ServiceModel.dll!System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(System.ServiceModel.Channels.RequestContext request, System.ServiceModel.OperationContext currentOperationContext) + 0x1e2 Bytes    
System.ServiceModel.dll!System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(System.IAsyncResult result) + 0xdb Bytes 
System.ServiceModel.Internals.dll!System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(System.IAsyncResult result) + 0x32 Bytes 
System.ServiceModel.Internals.dll!System.Runtime.AsyncResult.Complete(bool completedSynchronously) + 0x17a Bytes    
System.ServiceModel.dll!System.ServiceModel.Channels.TransportDuplexSessionChannel.TryReceiveAsyncResult.OnReceive(System.IAsyncResult result) + 0x11d Bytes    
System.ServiceModel.Internals.dll!System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(System.IAsyncResult result) + 0x32 Bytes 
System.ServiceModel.Internals.dll!System.Runtime.AsyncResult.Complete(bool completedSynchronously) + 0x17a Bytes    
System.ServiceModel.dll!System.ServiceModel.Channels.SynchronizedMessageSource.ReceiveAsyncResult.OnReceiveComplete(object state) + 0x82 Bytes  
System.ServiceModel.dll!System.ServiceModel.Channels.SessionConnectionReader.OnAsyncReadComplete(object state) + 0x174 Bytes    
System.ServiceModel.Internals.dll!System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(System.IAsyncResult result) + 0x32 Bytes 
System.dll!System.Net.LazyAsyncResult.Complete(System.IntPtr userToken) + 0xc5 Bytes    
System.dll!System.Net.Security.NegotiateStream.ProcessFrameBody(int readBytes, byte[] buffer, int offset, int count, System.Net.AsyncProtocolRequest asyncRequest) + 0x109 Bytes    
System.dll!System.Net.Security.NegotiateStream.ReadCallback(System.Net.AsyncProtocolRequest asyncRequest = {System.Net.AsyncProtocolRequest}) + 0xea Bytes  
System.dll!System.Net.FixedSizeReader.CheckCompletionBeforeNextRead(int bytes) + 0x32 Bytes 
System.dll!System.Net.FixedSizeReader.ReadCallback(System.IAsyncResult transportResult) + 0x9a Bytes    
System.ServiceModel.Internals.dll!System.Runtime.AsyncResult.Complete(bool completedSynchronously) + 0x17a Bytes    
System.ServiceModel.dll!System.ServiceModel.Channels.ConnectionStream.IOAsyncResult.OnAsyncIOComplete(object state) + 0x6a Bytes    
System.ServiceModel.dll!System.ServiceModel.Channels.SocketConnection.OnReceiveAsync(object sender, System.Net.Sockets.SocketAsyncEventArgs eventArgs) + 0xde Bytes 
System.dll!System.Net.Sockets.SocketAsyncEventArgs.FinishOperationSuccess(System.Net.Sockets.SocketError socketError, int bytesTransferred, System.Net.Sockets.SocketFlags flags) + 0xbc5 Bytes 
System.dll!System.Net.Sockets.SocketAsyncEventArgs.CompletionPortCallback(uint errorCode, uint numBytes, System.Threading.NativeOverlapped* nativeOverlapped) + 0x40 Bytes  
mscorlib.dll!System.Threading._IOCompletionCallback.PerformIOCompletionCallback(uint errorCode = 0, uint numBytes = 573, System.Threading.NativeOverlapped* pOVERLAP = 0x000000a6a0964860) + 0x96 Bytes 

How is Castle supposed to work with System.Transactions? If I use the singleton lifestyle the calls to the WCF client work correctly. I can't use singleton lifestyle because the client are not thread-safe.

Upvotes: 0

Views: 280

Answers (1)

Jony Adamit
Jony Adamit

Reputation: 3416

Make sure you register LazyOfTComponentLoader as described here.
Also, you may be making plenty of connections.
Make sure your max connections parameter is in order. Here is an example.

Good luck!

Upvotes: 0

Related Questions