ASp.net vnext中的AssemblyNeutral属性 [英] AssemblyNeutral Attribute in ASp.net vnext

查看:52
本文介绍了ASp.net vnext中的AssemblyNeutral属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了ASP.net vnext的 AssemblyNeutral 属性.

I recently come across AssemblyNeutral attribute of ASP.net vnext.

如果需要具体说明,那么您将在记录"存储库中找到该属性的用法. https://github.com/aspnet/logging

If I have to be specific then you will find that attribute usage in Logging respository. https://github.com/aspnet/logging

还有许多其他存储库,但这很简单.

There are many other repository but this is simple one.

现在,根据许多博客,如果我们使用 AssemblyNeutral 属性,则该接口或类型不与程序集绑定,因此我们可以在其他框架中使用 AssemblyNeutral 属性定义相同的接口,想要相同的合同.

Now as per many blog if we use AssemblyNeutral attribute then that interface or type not tied with assembly so we can defined same interface with AssemblyNeutral attribute in some other framework that want same contract.

所以创建示例时,我已经在ASP.net vnext类库项目中创建了自己的小型记录器库,但是它给了我类型转换错误.

So create example I have created my own small logger library in ASP.net vnext class library project but It gives me type cast error.

 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)中使用.您会看到它给了我Cast错误.为什么这样 ?

Now I am trying to use in SampleApp ( Logging Repository SampleApp). You can see that it gives me Cast Error. Why so ?

推荐答案

我部分同意Visual Studio 2015预览版IDE不支持AssemlbyNeutral属性,但问题出在其他地方.

I am partially agree with Visual Studio 2015 Preview IDE does not support AssemlbyNeutral attribute but Problem is somewhere else.

实际上,当我们在自定义程序集中再次定义Interface或Type时,如果我们希望它支持AssemblyNeutral,则它必须具有与原始接口相同的名称空间.

Actually when we defining Interface or Type again in custom assembly and if we want it support AssemblyNeutral then it must have same namespace as original interface.

在我的示例中,我通过将接口放在New Namespace中来做错了.

In my sample I have done it wrong by putting interface in New Namespace.

我已更新代码,然后在Visual Studio 2015预览版中构建它可以编译甚至正确执行.

I have updated the code and then I build in Visual Studio 2015 Preview It compile and even execute correctly.

因此要记住的主要事情是放置程序集中性接口和类型,使其与原始程序集所驻留的名称空间相同."

So main thing to remember is "Put Assembly Neutral Interface and types in same namespace as it resides for original assembly".

以下是更新的代码.

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

这篇关于ASp.net vnext中的AssemblyNeutral属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆