在我的 ASP.NET Web API 上,我有一种方法可以使用MsgReader从电子邮件中解析一些数据。它工作正常,但一段时间后它开始抛出以下异常:
System.IO.FileNotFoundException: Could not load file or assembly 'MsgReader.resources, Version=2.1.3.0, Culture=en-US, PublicKeyToken=47f99f3a9a2648df' or one of its dependencies. The system cannot find the file specified.
File name: 'MsgReader.resources, Version=2.1.3.0, Culture=en-US, PublicKeyToken=47f99f3a9a2648df' ---> System.IO.FileNotFoundException: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
at System.Reflection.RuntimeAssembly.nLoadFile(String path, Evidence evidence)
at System.Reflection.Assembly.LoadFile(String path)
at System.AppDomain.OnAssemblyResolveEvent(RuntimeAssembly assembly, String assemblyFullName)
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalGetSatelliteAssembly(String name, CultureInfo culture, Version version, Boolean throwOnFileNotFound, StackCrawlMark& stackMark)
at System.Resources.ManifestBasedResourceGroveler.GetSatelliteAssembly(CultureInfo lookForCulture, StackCrawlMark& stackMark)
at System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark)
at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark)
at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents)
at System.Resources.ResourceManager.GetString(String name, CultureInfo culture)
at MsgReader.Mime.MessagePart..ctor(Byte[] rawBody, MessageHeader headers) in C:\Users\Kees\Documents\GitHub\MsgReader\MsgReader\Mime\MessagePart.cs:line 242
at MsgReader.Mime.Message..ctor(Byte[] rawMessageContent, Boolean parseBody) in C:\Users\Kees\Documents\GitHub\MsgReader\MsgReader\Mime\Message.cs:line 139
at MsgReader.Mime.Message.Load(Stream messageStream) in C:\Users\Kees\Documents\GitHub\MsgReader\MsgReader\Mime\Message.cs:line 301
at MsgReader.Outlook.Storage.Message.LoadClearSignedMessage(IStorage storage) in C:\Users\Kees\Documents\GitHub\MsgReader\MsgReader\Outlook\Message.cs:line 1558
at MsgReader.Outlook.Storage.Message.LoadStorage(IStorage storage) in C:\Users\Kees\Documents\GitHub\MsgReader\MsgReader\Outlook\Message.cs:line 1384
at MsgReader.Outlook.Storage..ctor(Stream storageStream, FileAccess fileAccess) in C:\Users\Kees\Documents\GitHub\MsgReader\MsgReader\Outlook\Storage.cs:line 196
...
我回收应用程序池后,它再次工作(一段时间)然后在某个时候重复错误...
我尝试使用assemblyBinding
<dependentAssembly>
<assemblyIdentity name="MsgReader" publicKeyToken="47f99f3a9a2648df" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.1.3.0" newVersion="2.1.3.0" />
</dependentAssembly>
但没有成功......
我认为问题在于文化的问题?因为我错误地看到文化是en-US,但在 MsgReader包 中只有这些文化:
这是引发异常的代码部分:
var ext = Path.GetExtension(fileName)?.Replace(".", "").ToLower();
if (ext == "eml")
{
using (var connection = new SqlConnection(ConnectionString))
{
connection.Open();
var msg = new MsgReader.Mime.Message(datoteka.Bytes);
new HProperty(datoteka.ResourceId, "DateSent",
msg.Headers.DateSent.ToLocalTime().ToString("dd.MM.yyyy HH:mm:ss"))
.Save(connection);
new HProperty(datoteka.ResourceId, "From", msg.Headers.From.Raw)
.Save(connection);
new HProperty(datoteka.ResourceId, "To",
string.Join("; ", msg.Headers.To.Select(x => x.Raw)))
.Save(connection);
new HProperty(datoteka.ResourceId, "Cc",
string.Join("; ", msg.Headers.Cc.Select(x => x.Raw)))
.Save(connection);
new HProperty(datoteka.ResourceId, "Bcc",
string.Join("; ", msg.Headers.Bcc.Select(x => x.Raw)))
.Save(connection);
new HProperty(datoteka.ResourceId, "Subject", msg.Headers.Subject)
.Save(connection);
}
}
else if (ext == "msg")
{
using (var connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (var stream = new MemoryStream(datoteka.Bytes))
using (var msg = new Storage.Message(stream))
{
new HProperty(datoteka.ResourceId, "DateSent",
msg.Headers.DateSent.ToLocalTime().ToString("dd.MM.yyyy HH:mm:ss"))
.Save(connection);
new HProperty(datoteka.ResourceId, "From", msg.Headers.From.Raw)
.Save(connection);
new HProperty(datoteka.ResourceId, "To",
string.Join("; ", msg.Headers.To.Select(x => x.Raw)))
.Save(connection);
new HProperty(datoteka.ResourceId, "Cc",
string.Join("; ", msg.Headers.Cc.Select(x => x.Raw)))
.Save(connection);
new HProperty(datoteka.ResourceId, "Bcc",
string.Join("; ", msg.Headers.Bcc.Select(x => x.Raw)))
.Save(connection);
new HProperty(datoteka.ResourceId, "Subject", msg.Headers.Subject)
.Save(connection);
if (msg.SignatureIsValid != null)
new HProperty(datoteka.ResourceId, "SignatureIsValid",
msg.SignatureIsValid.Value
? "true"
: "false")
.Save(connection);
if (msg.SignedBy != null)
new HProperty(datoteka.ResourceId, "SignedBy", msg.SignedBy)
.Save(connection);
if (msg.SignedOn != null)
new HProperty(datoteka.ResourceId, "SignedOn",
msg.SignedOn.Value.ToLocalTime().ToString("dd.MM.yyyy HH:mm:ss"))
.Save(connection);
}
}
}