'ServiceStack.VirtualPath.FileSystemVirtualDirectory'的类型初始值设定项引发了异常

时间:2014-05-13 20:34:03

标签: servicestack nlog

我刚刚将一个正在运行的ServiceStack项目从版本3.9.69移动到版本4.0.20,当我尝试在使用AppSelfHostBase的app主机上运行init时出现错误。它说它找不到NLog的文件或程序集,版本= 2.0.1.0 我通过nuget更新了所有内容,我相信它现在应该依赖于NLog 2.1.0,我在项目中引用它。 不确定我是否只是隐藏了一个错误的引用,或者它是否是别的东西。 一直看着这几个小时,任何帮助都会很棒!

{"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)}

这是我的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
            });

    }


}
}

2 个答案:

答案 0 :(得分:2)

NuGet上当前v4.0.20版本的ServiceStack.Logging.Nlog取决于NLog 2.0.0.2

我刚刚更新了ServiceStack v4.0.21中的最新NuGet软件包,以引用现在available on MyGet的最新日志记录依赖项,现在它们具有:

答案 1 :(得分:2)

我在更新版本的ServiceStack(4.0.40)中遇到了同样的问题。事实证明这是我的错,因为没有使用正确的web.config。我忘了包括以下内容(SS包括它,我故意删除它):

 <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>

我不知道这是否是一个常见的错误,但对我来说,“生产web.config”总是有点特别,不想覆盖它,然后很容易错过这些的东西。