2015-01-17 28 views
1

我最近遇到0123.net的网站,属于ASP.net vnext。装配中性在ASp.net中属性vnext

如果我必须具体说明,那么您会在Logging respository中找到该属性的用法。 https://github.com/aspnet/logging

还有很多其他的存储库,但这很简单。

现在按照许多博客,如果我们使用AssemblyNeutral属性,那么该接口或类型不与装配绑定,因此我们可以在需要相同合同的其他一些框架中定义与AssemblyNeutral属性相同的接口。

因此,创建示例我已经在ASP.net vnext类库项目中创建了自己的小型记录器库,但它给我输入了类型转换错误。

namespace MyLogger 
{ 
    #if ASPNET50 || ASPNETCORE50 
    [Microsoft.Framework.Runtime.AssemblyNeutral] 
    #endif 
    public interface ILogger 
    {  
     void Write(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter); 
     bool IsEnabled(LogLevel logLevel); 
     IDisposable BeginScope(object state); 
    } 

    #if ASPNET50 || ASPNETCORE50 
    [Microsoft.Framework.Runtime.AssemblyNeutral] 
    #endif 
    public interface ILoggerFactory 
    {  
     ILogger Create(string name); 
     void AddProvider(ILoggerProvider provider); 
    } 
    #if ASPNET50 || ASPNETCORE50 
    [Microsoft.Framework.Runtime.AssemblyNeutral] 
    #endif 
    public interface ILoggerProvider 
    {   
     ILogger Create(string name); 
    } 

    #if ASPNET50 || ASPNETCORE50 
    [Microsoft.Framework.Runtime.AssemblyNeutral] 
    #endif 
    public enum LogLevel 
    { 
     Verbose = 1, 
     Information = 2, 
     Warning = 3, 
     Error = 4, 
     Critical = 5, 
    } 

    public class MyLogger : ILogger 
    { 
     public IDisposable BeginScope(object state) 
     { 
      return null; 
     } 

     public bool IsEnabled(LogLevel logLevel) 
     { 
      return true; 
     } 

     public void Write(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter) 
     { 
      System.IO.File.AppendAllText("C:\\Testlog.txt", "This is test log" + System.Environment.NewLine); 
     } 
    } 

    public class MyLoggerProvider : ILoggerProvider 
    { 
     private readonly Func<string, LogLevel, bool> _filter; 

     public MyLoggerProvider(Func<string, LogLevel, bool> filter) 
     { 
      _filter = filter; 
     } 

     public ILogger Create(string name) 
     { 
      return new MyLogger(); 
     }  
    } 

    public static class MyLoggerExtentions 
    { 
     public static ILoggerFactory AddMyLogger(this ILoggerFactory factory, Func<string, LogLevel, bool> filter) 
     { 
      factory.AddProvider(new MyLoggerProvider(filter)); 
      return factory; 
     } 
    } 
} 

现在我正在尝试在SampleApp(Logging Repository SampleApp)中使用。你可以看到它给我铸造错误。为什么这样 ?

enter image description here

回答

0

我部分同意Visual Studio 2015 Preview IDE不支持AssemlbyNeutral属性,但问题在其他地方。

实际上,当我们在自定义程序集中再次定义接口或类型,并且如果我们希望它支持AssemblyNeutral,那么它必须与原始接口具有相同的名称空间。

在我的示例中,我通过将接口放入新命名空间中做了错误处理。

我更新了代码,然后构建在Visual Studio 2015 Preview中它编译甚至正确执行。

所以要记住的主要事情是“将汇编中性接口和类型放在与原始汇编相同的命名空间中”。

以下为更新代码。

namespace Microsoft.Framework.Logging 
{ 
    #if ASPNET50 || ASPNETCORE50 
    [Microsoft.Framework.Runtime.AssemblyNeutral] 
    #endif 
    public interface ILogger 
    { 
     void Write(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter); 
     bool IsEnabled(LogLevel logLevel); 
     IDisposable BeginScope(object state); 
    } 

    #if ASPNET50 || ASPNETCORE50 
    [Microsoft.Framework.Runtime.AssemblyNeutral] 
    #endif 
    public interface ILoggerFactory 
    { 
     ILogger Create(string name); 
     void AddProvider(ILoggerProvider provider); 
    } 

    #if ASPNET50 || ASPNETCORE50 
    [Microsoft.Framework.Runtime.AssemblyNeutral] 
    #endif 
    public interface ILoggerProvider 
    { 
     ILogger Create(string name); 
    } 

    #if ASPNET50 || ASPNETCORE50 
    [Microsoft.Framework.Runtime.AssemblyNeutral] 
    #endif 
    public enum LogLevel 
    { 
     Verbose = 1, 
     Information = 2, 
     Warning = 3, 
     Error = 4, 
     Critical = 5, 
    } 
} 

namespace MyLogger 
{ 


    public class MyLogger : ILogger 
    { 
     public IDisposable BeginScope(object state) 
     { 
      return null; 
     } 

     public bool IsEnabled(LogLevel logLevel) 
     { 
      return true; 
     } 

     public void Write(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter) 
     { 
      System.IO.File.AppendAllText("C:\\Testlog.txt", "This is test log" + System.Environment.NewLine); 
     } 
    } 

    public class MyLoggerProvider : ILoggerProvider 
    { 
     private readonly Func<string, LogLevel, bool> _filter; 

     public MyLoggerProvider(Func<string, LogLevel, bool> filter) 
     { 
      _filter = filter; 
     } 

     public ILogger Create(string name) 
     { 
      return new MyLogger(); 
     } 
    } 

    public static class MyLoggerExtentions 
    { 
     public static ILoggerFactory AddMyLogger(this ILoggerFactory factory, Func<string, LogLevel, bool> filter) 
     { 
      factory.AddProvider(new MyLoggerProvider(filter)); 
      return factory; 
     } 
    } 
} 
0

IDE不会在处理源中定义适当尚未装配中性接口。这就是为什么你看到错误。

+0

是的。您是对的,可能我已经阅读过您的博客,但是如何在不使用IDE的情况下编译示例。我想在Action中看到这个属性。 – dotnetstep