c# - WAS回收应用程序池后,使用Unity解析实例会失败

标签 c# .net iis unity-container was

我使用 Unity 来解析我的实例。包括WCF服务。

但是我发现了一个棘手的问题。 我将 WCF 程序部署到 IIS (WAS),并且可以正常运行我的 Web 服务。

IIS(WAS)回收应用程序池后,我确定我的注册代码已被执行。 但现在 Web 服务抛出异常,比如

"Exception occurred while: while resolving."

然后我从 IIS 服务记录信息,说明我没有注册此类型。

最佳答案

我自己解决了这个问题。

当应用程序池回收并创建启动新的应用程序池时,所有DLL都会复制到新的Asp.Net模板文件夹中。

当我强制将所有“bin”DLL 加载到 AppDomain 时,我在 bin 的程序集中注册了一个类型。不在 Asp.net 临时文件夹程序集中。所以Unity认为这是差异类型。

现在我更改将程序集加载到当前域的方式

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

然后问题就解决了。

关于c# - WAS回收应用程序池后,使用Unity解析实例会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9390356/

相关文章:

带有 taglib 的 C# mp3 ID 标签 - 专辑封面

c# - 如何用空格连接 session 值

c# - Umbraco 实例中的 MVC 路由

c# - Sitecore 动态占位符允许渲染

.net - 如何声明返回节点集的用户定义函数?

c# - 对字符串“3 + 4”中的值执行计算

c# - RestSharp 超时不起作用

c# - 从 ASP.net 运行 Windows 计划任务

asp.net - 带有 Hangfire 的 TeamCity MSDeploy .NET 应用程序失败

c# - session 变量由于某种原因丢失