2011-12-21 24 views
4

私はAutofacを使用して知っていますが、WCFサービスをホストすることは可能です。方法を逆転するのはどうですか? Autofacを使用してWCFサービスを利用できますか?私はクライアント側を意味します。はいの場合、どのように行うことができますか?Autofacを使用してWCFサービスを使用するには?

答えて

2

WCF integration wiki pageの最初のセクションの手順に従うことをお勧めします。

サービスインスタンスのリリース時にUseWcfSafeReleaseICommunicationObject.Close()を呼び出すという唯一の注意点があります。私の見解では、Web呼び出しがすべてのバッファを完全に処理し、場合によっては(Silverlightの)UIスレッドをブロックするまでブロックするので、これは悪いことです。私はICommunicationObject.Abort()に電話するほうがよいでしょう。なぜなら、コンポーネントインスタンスをリリースすれば、そのプロセスはもう必要ないからです。あなたはより多くのそれを好きならば、あなたはきっとAutofacの内蔵クライアント側の統合コードを使用することができますが

/// <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(); 
    } 
} 

:それは言った、私はRegistrationExtensions classの次のバージョンを使用します。

0

@Pavel Gatilov 私は反射

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 –

関連する問題