2011-12-21 64 views

回答

2

我建议您按照WCF integration wiki page的第一部分说明。

关于落实唯一要注意的是,UseWcfSafeRelease电话ICommunicationObject.Close()在服务实例的释放。在我看来,这很糟糕,因为它会阻塞,直到Web调用完全处理所有缓冲区,并且在某些情况下会阻塞UI线程(在Silverlight中)。我最好打电话给ICommunicationObject.Abort(),因为如果我释放一个组件实例,这意味着我不再需要它的进程。这就是说,我用的是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(); 
    } 
} 

但如果你喜欢它更多的,你一定能使用Autofac内置的客户端集成代码。

0

@Pavel加季洛夫 我通过反射

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; 
    } 
} 
+1

你真的需要反思提取? ;-) https://github.com/autofac/Autofac.Wcf/blob/master/src/Autofac.Integration.Wcf/RegistrationExtensions.cs – 2015-04-18 11:33:09