Please confirm the accuracy of the entlb + unity configuration (Entlib 5.0 + Unity 2.0.414.0)

Aug 7, 2013 at 12:34 AM
I have a wcf service hosted in a web application on IIS express and
I have a problem with my Exception Fault Callhandler which should replace the original exception. (client is a console app)
But till now I got several different exceptions and end up with the following one:
Message=Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type LogWriter cannot be constructed. You must configure the container to supply this value.
I am using any logging in my test app, so I don't know why he requires the resolution of the logging library?!

The web.config:
<exceptionHandling>
<exceptionPolicies>
  <add name="DalExceptionPolicy">
    <exceptionTypes>
      <add name="All Exceptions" type="System.Exception, mscorlib"
        postHandlingAction="ThrowNewException">
        <exceptionHandlers>
          <add name="Wrap Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
            exceptionMessage="My DAL Wrap Exception" exceptionMessageResourceType=""
            wrapExceptionType="Customer.Framework.Exceptions.DAL.DalWrapperException, Customer.Framework" />
        </exceptionHandlers>
      </add>
    </exceptionTypes>
  </add>
  <add name="WCF Exception Shielding">
    <exceptionTypes>
      <add name="DalWrapperException" type="Customer.Framework.Exceptions.DAL.DalWrapperException, Customer.Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        postHandlingAction="ThrowNewException">
        <exceptionHandlers>
          <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            faultContractType="Customer.Framework.Exceptions.Faults.MyWcfServiceFault, Customer.Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
            name="Fault Contract Exception Handler">
            <mappings>
              <add source="Guid" name="FaultID" />
              <add source="Message" name="FaultMessage" />
            </mappings>
          </add>
        </exceptionHandlers>
      </add>
    </exceptionTypes>
  </add>
</exceptionPolicies>
</exceptionHandling>

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

  <interception>
    <policy name="WcfWrapPolicy">

      <matchingRule name="NamespaceMatchingRuleWcf" type="NamespaceMatchingRule">
        <constructor>
          <param name="matches" >
            <array type="Microsoft.Practices.Unity.InterceptionExtension.MatchingInfo, Microsoft.Practices.Unity.Interception">
              <dependency name="MatchingWcfNS"/>
              <!--<dependency name="MatchingInfo_NS2"/>-->
            </array>
          </param>
        </constructor>
      </matchingRule>

      <callHandler name="MyWcfExceptionFaultCallHandler" type="Customer.Framework.Unity.MyWcfExceptionFaultCallHandler, Customer.Framework">
        <lifetime type="singleton"/>
        <constructor>
          <param name="exceptionPolicy" dependencyName="WCF Exception Shielding"/>
        </constructor>
      </callHandler>

    </policy>

    <policy name="DalPolicy">

      <matchingRule name="Namespace Matching Rule" type="NamespaceMatchingRule">
        <constructor>
          <param name="matches" >
            <array type="Microsoft.Practices.Unity.InterceptionExtension.MatchingInfo, Microsoft.Practices.Unity.Interception">
              <dependency name="MatchingDalNamespace"/>
              <!--<dependency name="MatchingInfo_NS2"/>-->
            </array>
          </param>
        </constructor>
      </matchingRule>

      <callHandler name="MyCustomExceptionCallHandler" type="Customer.Framework.Unity.MyCustomExceptionCallHandler, Customer.Framework">
        <lifetime type="singleton"/>
        <constructor>
          <param name="exceptionPolicy" dependencyName="DalExceptionPolicy"/>
        </constructor>
      </callHandler>

    </policy>

  </interception>

  <register type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling">
  </register>
  <register type="Microsoft.Practices.Unity.InterceptionExtension.MatchingInfo, Microsoft.Practices.Unity.Interception" name="MatchingWcfNS">
    <constructor>
      <param name="nameToMatch" value="Customer.Application.Web.WCFService"/>
    </constructor>
  </register>
  <register type="Microsoft.Practices.Unity.InterceptionExtension.MatchingInfo, Microsoft.Practices.Unity.Interception" name="MatchingDalNamespace">
    <constructor>
      <param name="nameToMatch" value="Customer.DAL.Facade"/>
    </constructor>
  </register>

  <register  type="Customer.Application.Web.WCFService.IWcfServiceLayer, Customer.Application.Web.WCFService" mapTo="Customer.Application.Web.WCFService.WcfServiceLayer, Customer.Application.Web.WCFService">
    <lifetime type="transient"/>
    <interceptor type="Microsoft.Practices.Unity.InterceptionExtension.VirtualMethodInterceptor, Microsoft.Practices.Unity.Interception"/>
    <policyInjection/>
  </register>
  <register  type="Customer.Interfaces.DAL.IDal, Customer.Interfaces" mapTo="Customer.DAL.Facade.API, Customer.DAL">
    <lifetime type="transient"/>
    <interceptor type="Microsoft.Practices.Unity.InterceptionExtension.VirtualMethodInterceptor, Microsoft.Practices.Unity.Interception"/>
    <policyInjection/>
  </register>
  <register  type="Customer.Interfaces.Repository.IRepositoryOrder, Customer.Interfaces" mapTo="Customer.DAL.Northwind.RepositoryOrder, Customer.DAL.Northwind">
    <lifetime type="transient"/>
    <interceptor type="Microsoft.Practices.Unity.InterceptionExtension.VirtualMethodInterceptor, Microsoft.Practices.Unity.Interception"/>
    <policyInjection/>
  </register>
  <register  type="Customer.Interfaces.Repository.IRepositoryBlogs, Customer.Interfaces" mapTo="Customer.DAL.Blogger.RepositoryBlogs, Customer.DAL.Blogger">
    <lifetime type="transient"/>
    <interceptor type="Microsoft.Practices.Unity.InterceptionExtension.VirtualMethodInterceptor, Microsoft.Practices.Unity.Interception"/>
    <policyInjection/>
  </register>
</container>
</unity>

My Wcf Exception Fault Callhandler:
namespace Customer.Framework.Unity
{
    public class MyWcfExceptionFaultCallHandler : ICallHandler
    {
        // Fields
        private ExceptionPolicyImpl exceptionPolicy;
        private int order;

        public MyWcfExceptionFaultCallHandler()
        {

        }

        public MyWcfExceptionFaultCallHandler(string exceptionPolicyName, int order)
        {
            this.exceptionPolicy = DependencyFactory.Container.Resolve<ExceptionPolicyImpl>(exceptionPolicyName);
            this.order = order;
        }


        public MyWcfExceptionFaultCallHandler(ExceptionPolicyImpl exceptionPolicy)
        {
            this.exceptionPolicy = exceptionPolicy;
        }


        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {

            IMethodReturn return2 = getNext()(input, getNext);

            if (return2.Exception != null)
            {
                try
                {
                    this.exceptionPolicy.HandleException(return2.Exception);
                }
                catch (Exception exception)
                {
                    return2.Exception = exception;
                }
            }
            return return2;
        }


        public ExceptionPolicyImpl ExceptionPolicy
        {
            get
            {
                return this.exceptionPolicy;
            }
        }

        public int Order
        {
            get
            {
                return this.order;
            }
            set
            {
                this.order = value;
            }
        }
    }
}
The fault used:
namespace Customer.Framework.Exceptions.Faults
{
    [DataContract]
    public class MyWcfServiceFault
    {
        [DataMember]
        public Guid FaultID { get; set; }

        [DataMember]
        public string FaultMessage { get; set; }

        [DataMember]
        public IDictionary FaultData { get; set; }
    }
}
The Service:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    [ExceptionShielding]//
    public class WcfServiceLayer : IWcfServiceLayer
    {
The service interface:
[ServiceContract]
    public interface IWcfServiceLayer
    {
 [OperationContract]
        [FaultContract(typeof(Customer.Framework.Exceptions.Faults.MyWcfServiceFault))]
        Order GetFirstOrder();
So in the DAL Layer I manually throw a new system.exception which gets wrapped by a custom DAL Exception (throw new).
Then this exception pops up in the WCF layer and then I get the above mentioned error.

Do you see any issues in my web.config/code.
Aug 7, 2013 at 3:26 AM
The message indicates that somewhere a LogWriter is being requested but is not configured (usually in the configuration file). The stack trace should point to where the issue is. If you can't track down the issue, then I would recommend posting a sample solution that demonstrates the issue.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to