Служба WCF не перехватывается с использованием перехвата Unity 2.0 - PullRequest
1 голос
/ 06 января 2012

В моем веб-приложении WCF я настроил контейнер Unity для перехвата. Следующее - моя конфигурация единства.

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> 
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>

    <assembly name="Infrastructure" />
    <assembly name="WCFServiceLib1"/>

    <namespace name="Infrastructure"/>
    <namespace name="WCFServiceLib1" />

    <container>
      <extension type="Interception" />
      <register type="IService1" mapTo="Service1">
        <interceptor type="InterfaceInterceptor"/>
        <interceptionBehavior type="LogPerformanceDataBehavior"/>
      </register>
    </container>
</unity>

Когда я пытаюсь вызвать метод службы с помощью инструмента wcftestclient, выдается следующее исключение.

ArgumentException - Тип WCFServiceLib1.Service1 не допускается.
Имя параметра: interceptedType

Я использовал инструмент svctraceviewer , чтобы получить вышеуказанные сведения об исключении.

Ниже приведена реализация класса LogPerformanceDataBehavior

public class LogPerformanceDataBehavior : IInterceptionBehavior
{
    public IEnumerable<Type> GetRequiredInterfaces()
    {
        return Type.EmptyTypes;
    }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
    {
        var watch = new Stopwatch();

        watch.Start();
        IMethodReturn methodReturn = getNext()(input, getNext);
        watch.Stop();

        string sb = string.Format("Method {0}.{1} executed in: ({2} ms, {3} ticks){4}",
                                  input.MethodBase.DeclaringType.Name, input.MethodBase.Name,
                                  watch.ElapsedMilliseconds, watch.ElapsedTicks, Environment.NewLine);

        using (StreamWriter outfile = new StreamWriter(@"c:\logs\Performance.txt"))
        {
            outfile.Write(sb);
        }

        return methodReturn;
    }

    public bool WillExecute
    {
        get { return true; }
    }
}


Что может быть не так?

1 Ответ

6 голосов
/ 09 января 2012

Проблема в том, что поставщик экземпляра WCF не разрешает интерфейс.Это разрешает тип сервиса.Вы используете интерфейсный перехватчик, который нельзя напрямую применить к классу.См. Сравнение методов перехвата .

Исправление:

  1. Изменить на VirtualMethodInterceptor.
  2. Пометить любые методы обслуживания, которые следует перехватить, как virtual.

Пример регистрации:

<register type="Service1" >         
    <interceptor type="VirtualMethodInterceptor"/> 
    <interceptionBehavior type="LogPerformanceDataBehavior"/>       
</register>
...