How to use WCF service using Autofac?

I know, using Autofac , you can host a WCF service. How about a way back? Can I use the WCF service using Autofac ? I mean the client side. If so, how can this be done?

+4
source share
3 answers

Take a look at http://code.google.com/p/autofac/wiki/WcfIntegration#Clients . You just need to register the binding configuration by registering ChannelFactory<IYourServiceContract> , and then register the channel creation. Remember to call UseWcfSafeRelease ().

+5
source

I recommend that you follow the instructions in the first section of the WCF integration wiki page .

The only note about this implementation is that UseWcfSafeRelease calls ICommunicationObject.Close() when a service instance is issued. From my point of view, this is bad because it blocks until the web call completely processes all the buffers, and in some cases blocks the user interface stream (in Silverlight). I'd rather call ICommunicationObject.Abort() , because if I release an instance of the component, it means that I no longer need its processes. However, I am using the following version of the RegistrationExtensions class:

 /// <summary> /// Extend the registration syntax with WCF-specific helpers. /// </summary> public static class RegistrationExtensions { /// <summary> /// Dispose the channel instance in such a way that exceptions /// </summary> /// <typeparam name="TLimit">Registration limit type.</typeparam> /// <typeparam name="TActivatorData">Activator data type.</typeparam> /// <typeparam name="TRegistrationStyle">Registration style.</typeparam> /// <param name="registration">Registration to set release action for.</param> /// <returns>Registration builder allowing the registration to be configured.</returns> /// <remarks>This will eat exceptions generated in the closing of the channel.</remarks> public static IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> UseWcfSafeRelease<TLimit, TActivatorData, TRegistrationStyle>( this IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> registration) { if (registration == null) throw new ArgumentNullException("registration"); return registration.OnRelease(CloseChannel); } static void CloseChannel<T>(T channel) { var disp = (IClientChannel) channel; disp.Abort(); } } 

Although, if you like it more, you can use Autofac's built-in integration code on the client side.

+2
source

@ Pavel Gatilov I extract through the reflector

 private static void CloseChannel<T>(T channel) { IClientChannel channel2 = (IClientChannel) channel; try { if (channel2.State == CommunicationState.Faulted) { channel2.Abort(); } else { channel2.Close(); } } catch (TimeoutException) { channel2.Abort(); } catch (CommunicationException) { channel2.Abort(); } catch (Exception) { channel2.Abort(); throw; } } 
0
source

Source: https://habr.com/ru/post/1387404/


All Articles