2

I use Unity to resolve my instance. Include WCF service.

But I found a trick problem. I deploy my WCF program to IIS (WAS), and it's ok for running my web service.

After IIS(WAS) recycle application pool, and I'm sure my register code has been executed. But now web service throw a exception, say

"Exception occurred while: while resolving."

Then I log information from IIS service, it's say I don't register this type.

LU RD
  • 34,438
  • 5
  • 88
  • 296
metavige
  • 51
  • 4
  • Now I check Unity container's registration, register type and mapping type are both exist in unity container registration collection – metavige Feb 22 '12 at 07:47
  • How do you integrate Unity with WCF? Via a custom `ServiceHostFactory` or something else? Are you checking the registrations on the same thread that threw the exception? WCF is inherently multi-threaded which might cause problems if your registration code is not run on the proper thread. – Sebastian Weber Feb 22 '12 at 10:07
  • Yes, I use ServiceHostFactory and implement a InstanceProvider. Inside the InstanceProvider, I use UnityContainer to create service instance. But I register interface and its implementation type, use assembly in bin directory, not assembly in Asp.Net template folder. – metavige Feb 23 '12 at 06:54

1 Answers1

3

I has resolved this problem by myself.

When application pool recycle and create start a new application pool, all DLL will copy to a new Asp.Net Template Folder.

When I force to load all "bin" DLLs to AppDomain, I register a type in bin's assembly. Not in Asp.net Temp Folder assembly. So Unity thought it's difference type.

Now I change the way to load assembly to current domain

string binPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin");
foreach (string dll in Directory.GetFiles(binPath, "*.dll", SearchOption.AllDirectories))
{
  var assemblyFromCurrentDomain = Assembly.Load(Assembly.LoadFile(dll).FullName);

  Debug.Print("Add Assembly : {0}, {1}", assemblyFromCurrentDomain.FullName, assemblyFromCurrentDomain.Location);
}

Then problem fixed.

metavige
  • 51
  • 4