Unity and WCF: Exception

Topics: Policy Injection Application Block
Jul 14, 2009 at 2:41 PM

Sometimes I get following exception:

----------------------------------------
Timestamp: 14.07.2009 12:12:13
Message: HandlingInstanceID: 57bc7b86-1b70-4ada-9b09-148430909c17
An exception of type 'System.ArgumentException' occurred and was caught.
------------------------------------------------------------------------
07/14/2009 16:12:13
Type : System.ArgumentException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Interface not found.
Source : mscorlib
Help link :  
ParamName :  
Data : System.Collections.ListDictionaryInternal
TargetSite : Int32 GetFirstSlotForInterface(IntPtr)
Stack Trace :    at System.RuntimeTypeHandle.GetFirstSlotForInterface(IntPtr interfaceHandle)
   at System.RuntimeType.GetInterfaceMap(Type ifaceType)
   at Microsoft.Practices.Unity.InterceptionExtension.InterceptingRealProxy.TranslateInterfaceMethod(MethodBase method)
   at Microsoft.Practices.Unity.InterceptionExtension.InterceptingRealProxy.Invoke(IMessage msg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at Services.Administration.ISecurityService.GetCryptoInfo(String username, String sessionId)
   at SyncInvokeGetCryptoInfo(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

Additional Info:

MachineName : S29
TimeStamp : 14.07.2009 12:12:13
FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
ThreadIdentity :
WindowsIdentity : NT AUTHORITY\NETWORK SERVICE

Category: Errors
Priority: 0
EventId: 100
Severity: Error
Title:Services Exception Handling
Process Id: 14088
Process Name: C:\WINDOWS\SysWOW64\inetsrv\w3wp.exe
Win32 Thread Id: 15948
Thread Name:  
Extended Properties: ErrorUniqueId - abfc167a-0fcc-40a5-8d50-c7751b02637e

 

May be, is there mistake in instance provider?

 

public class PolicyInjectionInstanceProvider : IInstanceProvider
{
	private Type serviceContractType { get; set; }		
	private readonly object _sync = new object();
	private static readonly IUnityContainer container;
	private static readonly TransparentProxyInterceptor injector = new TransparentProxyInterceptor();

	static PolicyInjectionInstanceProvider()
	{
		container = new UnityContainer()
			.AddNewExtension<Interception>();

		IConfigurationSource configSource = ConfigurationSourceFactory.Create();
		PolicyInjectionSettings settings = (PolicyInjectionSettings)configSource.GetSection(PolicyInjectionSettings.SectionName);
		if (settings != null)
		{
			settings.ConfigureContainer(container, configSource);
		}
	}

	public PolicyInjectionInstanceProvider(Type type)
	{
		if (type != null && !type.IsInterface)
		{
			throw new ArgumentException("Specified Type must be an interface");
		}

		serviceContractType = type;
	}

	#region IInstanceProvider Members

	public object GetInstance(System.ServiceModel.InstanceContext instanceContext, System.ServiceModel.Channels.Message message)
	{
		Type type = instanceContext.Host.Description.ServiceType;

		if (serviceContractType != null)
		{
			lock (_sync)
			{
				container.Configure<Interception>()
					.SetDefaultInterceptorFor(serviceContractType, injector);
				container.RegisterType(serviceContractType, type);
				return container.Resolve(serviceContractType);
			}
		}
		else
		{
			if (!type.IsMarshalByRef)
			{
				throw new ArgumentException("Type Must inherit MarshalByRefObject if no ServiceInterface is Specified");
			}
			lock (_sync)
			{
				container.Configure<Interception>()
					.SetDefaultInterceptorFor(type, injector);
				return container.Resolve(type);
			}
		}
	}

	public object GetInstance(System.ServiceModel.InstanceContext instanceContext)
	{
		return GetInstance(instanceContext, null);
	}

	public void ReleaseInstance(System.ServiceModel.InstanceContext instanceContext, object instance)
	{
		IDisposable disposable = instance as IDisposable;
		if (disposable != null)
		{
			disposable.Dispose();
		}
	}

	#endregion
}

 

Jul 15, 2009 at 7:50 AM

Hi,

At which point you've encoutered the exception? I'm not that familiar with the WCF PIAB integration, can you post sample snippet on how to use your class? Also, you stated that you sometimes get the exception, means some other times it dont occur. Do you know what specific scenario that you are getting that exception?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Jul 23, 2009 at 6:42 PM

I'm using the exact same code as FreddieM, and I am receiving the same exception intermittently.  The same service invocation of the same method with the same parameters will work once, and then fail, and then work again.

I have not been able determine exactly why this is happening, however the code above is also exhibiting the behavior described here:

http://blogs.msdn.com/tomholl/archive/2008/01/13/a-follow-up-on-piab-wcf-integration.aspx

My configuration is simpler than the example in that blog post however.  I have a WCF service hosted in IIS using the code above to create a service class via the PIAB.