2015-01-17 3 views
1

나는 최근에 ASP.net vnext의 AssemblyNeutral 속성을 발견했습니다.ASp.net의 AssemblyNeutral 속성 vnext

특정해야 할 경우 로깅 리포지토리에서 해당 특성 사용을 찾을 수 있습니다. 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 (로깅 저장소 SampleApp)에서 사용하려고합니다. 전송 오류가 발생하는 것을 볼 수 있습니다. 왜 그렇게?

enter image description here

답변

0

Visual Studio 2015 미리보기에서 부분적으로 동의합니다. IDE는 AssemlbyNeutral 특성을 지원하지 않지만 문제는 다른 곳에서 발생합니다.

실제로 사용자 정의 어셈블리에 인터페이스 또는 유형을 정의 할 때 AssemblyNeutral을 지원하려면 원래 인터페이스와 동일한 네임 스페이스가 있어야합니다.

필자는 새 네임 스페이스에 인터페이스를 추가하여 잘못 작성했습니다.

코드를 업데이트 한 다음 Visual Studio 2015 미리보기에서 컴파일하고 올바르게 실행합니다.

그래서 기억해야 할 중요한 것은 "어셈블리 중립 인터페이스와 유형을 원본 어셈블리와 동일한 네임 스페이스에 넣습니다"입니다.

다음은 업데이트 된 코드입니다.

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를 사용하지 않고 어떻게 예제를 컴파일 할 수 있습니까? 액션에서 그 속성을보고 싶습니다. – dotnetstep