Hello World ~~~

Fork me on GitHub
lucky528

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
统计
 

server:Windows server 2022 data center .

Sharepoint : SE.

使用VS2022 deploy之后,web页面访问SharePoint站点报缺失DLL的错误,这里边引入了NPOI库用来导入导出excel,但是导入的时候某些excel就会报错

System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

但是自己项目引入的是6.0的这个DLL,为什么会找5.0的DLL呢,我尝试在站点的web.config直接添加bindingRedirect,发现有效果。

经过查阅资料可以用过一个VS内置工具来查看DLL查找错误的日志。

assembly binding log viewer (FUSLOGVW) Fuslogvw.exe (Assembly Binding Log Viewer) - .NET Framework | Microsoft Learn

复制代码
The operation failed.
Bind result: hr = 0x80131040. No description available.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  c:\windows\system32\inetsrv\w3wp.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///C:/inetpub/wwwroot/wss/VirtualDirectories/9099/
LOG: Initial PrivatePath = C:\inetpub\wwwroot\wss\VirtualDirectories\9099\bin
LOG: Dynamic Base = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\ceaedf3a
LOG: Cache Base = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\ceaedf3a
LOG: AppName = bdf082ee
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\inetpub\wwwroot\wss\VirtualDirectories\9099\web.config
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: GAC Lookup was unsuccessful.
复制代码

这里可以很详细的看到查找DLL的顺序,但是依然没有办法再部署的时候自动修改这个配置,我又查阅了相关的资料

发现Sharepoint在feature激活的时候使用SPWebConfiModification来修改web.config.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
SPSecurity.RunWithElevatedPrivileges(() =>
{
    SPSite currentSite = properties.Feature.Parent as SPSite;
 
    var name = "*[local-name()='dependentAssembly'][*/@name='" + "System.Runtime.CompilerServices.Unsafe" + "'][*/@publicKeyToken='b03f5f7f11d50a3a'][*/@culture='neutral']";
    var value = "<dependentAssembly><assemblyIdentity name='" + "System.Runtime.CompilerServices.Unsafe" + "' publicKeyToken='b03f5f7f11d50a3a' culture='neutral' /><bindingRedirect oldVersion='0.0.0.0-6.0.0.0' newVersion='6.0.0.0' /></dependentAssembly>";
    SPWebConfigModification modification = new SPWebConfigModification()
    {
        Name = name,
        Path = "configuration/runtime/*[local-name()='assemblyBinding' and namespace-uri()='urn:schemas-microsoft-com:asm.v1']",
        Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
        Value = value,
        Sequence = 0,
        Owner = "SPAdmin"
    };
 
    currentSite.WebApplication.WebConfigModifications.Add(modification);
    currentSite.WebApplication.Update();
    currentSite.WebApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
});

 

 

posted on   gopher666  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
 
点击右上角即可分享
微信分享提示