The type initializer for 'ServiceStack.VirtualPath.FileSystemVirtualDirectory' threw an exception

StackOverflow https://stackoverflow.com/questions/23641076

  •  22-07-2023
  •  | 
  •  

سؤال

I've just moved a working ServiceStack project from version 3.9.69 to version 4.0.20 today and I'm getting an error when trying to run init on the app host which uses AppSelfHostBase. It says that it can't find a file or assembly for NLog, version=2.0.1.0 I updated everything through nuget and I believe it should be dependent on NLog 2.1.0 now, which I am referencing in my project. Not sure if I just have a bad reference hiding somewhere or if it's something else. Been looking at this for hours, any help would be great!

{"Could not load file or assembly 'NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c"}
[System.IO.FileLoadException]: {"Could not load file or assembly 'NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c"}
Data: {System.Collections.ListDictionaryInternal}
HelpLink: null
HResult: -2146234304
InnerException: null
Message: "Could not load file or assembly 'NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)"
Source: "ServiceStack.Logging.NLog"
StackTrace: "   at ServiceStack.Logging.NLogger.NLogLogger..ctor(Type type)\r\n   at ServiceStack.Logging.NLogger.NLogFactory.GetLogger(Type type)\r\n   at ServiceStack.Logging.LogManager.GetLogger(Type type)\r\n   at ServiceStack.VirtualPath.FileSystemVirtualDirectory..cctor()"
TargetSite: {Void .ctor(System.Type)}

Here is my apphost.cs

using Funq;
using McKissock.StudentApi.Security;
using ServiceStack.Logging.NLogger;
using NLog;
using ServiceStack.MiniProfiler;
using ServiceStack.OrmLite;
using ServiceStack;
using ServiceStack.IO;
using ServiceStack.Support;
using ServiceStack.Data;
using ServiceStack.Admin;
using System.Reflection;
using McKissock.StudentApi.Config;

namespace McKissock.StudentApi {
public class AppHost : AppSelfHostBase  {

    public AppHost() : base("McKissock.StudentApi", Assembly.GetExecutingAssembly()) { }

    public override void Configure(Container container) {
        //Logs
        ConfigLog();
        //Credentials

        this.GlobalRequestFilters.Add(CredentialFilter.FilterRequest);
        this.GlobalResponseFilters.Add(CredentialFilter.FilterResponse);

        //dbFactory
        container.Register<IDbConnectionFactory>(StudentApi.Config.AppConfig.Instance.OrmConnectionFactory);

        //Swagger
        Plugins.Add(new ServiceStack.Api.Swagger.SwaggerFeature());

        //Cors. TODO: is somebody using Cors ?
        Plugins.Add(new ServiceStack.CorsFeature());
    }

    private static Logger logger = LogManager.GetCurrentClassLogger();
    private static Logger loggerReq = LogManager.GetLogger("AppHost.Request");
    private static Logger loggerRes = LogManager.GetLogger("AppHost.Response");
    private static Logger loggerEx = LogManager.GetLogger("AppHost.Error");

    private void ConfigLog() {
        ServiceStack.Logging.LogManager.LogFactory = new NLogFactory();

        //Log Exceptions
        this.ServiceExceptionHandlers.Add((httpReq, request, ex) =>
        {
            loggerEx.Warn(ex);

            if (!ex.Message.Contains("Invalid Session - User: Null") && !ex.Message.Contains("Please specify the User") && !ex.Message.Contains("Invalid UserName or Password"))
            {
                string subject = "API Issue Logged - " + StudentApi.Config.AppConfig.Instance.Environment.ToString();
                string body = "Message: " + ex.Message + " Stack Trace: " + ex.StackTrace + " Inner Exception: " + ex.InnerException;
                NLogConfig.EmailError(subject, body);
            }

            return DtoUtils.CreateErrorResponse(request, ex);
        });

        //Log Requests
        this.GlobalRequestFilters.Add((httpReq, httpResp, requestDto) => loggerReq.Trace("{0}\t{1}\t{2}",httpReq.GetHttpMethodOverride(), httpReq.AbsoluteUri, httpReq.RemoteIp));

        //Log Responses
        this.GlobalResponseFilters.Add((req, res, dto) =>
        {

            if (dto == null)
                loggerRes.Debug("{0}\t{1}\t{2}", req.AbsoluteUri, res.StatusDescription, res.StatusCode);
            else
                loggerRes.Debug("{0}\t{1}\t{2}\t{3}", req.AbsoluteUri, res.StatusDescription, res.StatusCode, dto);

            dto = Profiler.ToJson();

        });

        //Log Exceptions
        this.ServiceExceptionHandlers.Add((httpReq, request, ex) =>
        {
            loggerEx.Warn(ex);

            if (!ex.Message.Contains("Invalid Session - User: Null") && !ex.Message.Contains("Please specify the User") && !ex.Message.Contains("Invalid UserName or Password"))
            {
                string subject = "API Issue Logged - " + StudentApi.Config.AppConfig.Instance.Environment.ToString();
                string body = "Message: " + ex.Message + " Stack Trace: " + ex.StackTrace + " Inner Exception: " + ex.InnerException;
                NLogConfig.EmailError(subject, body);
            }

            return DtoUtils.CreateErrorResponse(request, ex);
        });

        //Log Last requests except on production
        if (StudentApi.Config.AppConfig.Instance.Environment != StudentApi.Config.EnvironmentEnum.Prod)
            Plugins.Add(new RequestLogsFeature() {
                Capacity = 20,
                EnableErrorTracking = true,
                EnableRequestBodyTracking = true,
                EnableResponseTracking = false,
                EnableSessionTracking = true,
                RequiredRoles = null
            });

    }


}
}
هل كانت مفيدة؟

المحلول

The current v4.0.20 version of ServiceStack.Logging.Nlog on NuGet depends on NLog 2.0.0.2.

I've just updated the latest NuGet packages in ServiceStack v4.0.21 to reference the latest logging dependencies which are now available on MyGet which now has:

نصائح أخرى

I got the same issue with a newer version of ServiceStack (4.0.40). Turns out it was my fault, for not using the correct web.config. I forgot to include the following (SS includes it for you, I removed it on purpose):

 <runtime>
       <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                 <assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
                 <bindingRedirect oldVersion="0.0.0.0-3.2.1.0" newVersion="3.2.1.0" />
            </dependentAssembly>  
      </assemblyBinding>
    </runtime>

I don't know if this is a common mistake, but for me I the "production web.config" is always a little special, don't want to overwrite it, and then it is easy to miss out on these things.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top