主頁 > .NET開發 > C# Autofac學習筆記

C# Autofac學習筆記

2020-09-15 05:34:30 .NET開發

    一、為什么使用Autofac?

    Autofac是.NET領域最為流行的IoC框架之一,傳說是速度最快的一個,

    1.1、性能

    有人專門做了測驗:

    1.2、優點

    1)與C#語言聯系很緊密,C#里的很多編程方式都可以為Autofac使用,例如可以使用Lambda運算式注冊組件,

    2)較低的學習曲線,學習它非常的簡單,只要你理解了IoC和DI的概念以及在何時需要使用它們,

    3)支持JSON/XML配置,

    4)自動裝配,

    5)與Asp.Net MVC集成,

    6)微軟的Orchad開源程式使用的就是Autofac,可以看出它的方便和強大,

    1.3、資源

    官方網站:http://autofac.org/

    GitHub網址:https://github.com/autofac/Autofac

    學習資料:Autofac中文檔案

    二、資料準備

    2.1、新建專案

    IService下的介面類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinkTo.Test.Autofac.IService
{
    /// <summary>
    /// 動物吠聲介面類
    /// </summary>
    public interface IAnimalBark
    {
        /// <summary>
        /// 吠叫
        /// </summary>
        void Bark();
    }
}
IAnimalBark
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinkTo.Test.Autofac.IService
{
    /// <summary>
    /// 動物睡眠介面類
    /// </summary>
    public interface IAnimalSleep
    {
        /// <summary>
        /// 睡眠
        /// </summary>
        void Sleep();
    }
}
IAnimalSleep
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinkTo.Test.Autofac.IService
{
    /// <summary>
    /// 學校介面類
    /// </summary>
    public interface ISchool
    {
        /// <summary>
        /// 放學
        /// </summary>
        void LeaveSchool();
    }
}
ISchool
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinkTo.Test.Autofac.IService
{
    /// <summary>
    /// 學生介面類
    /// </summary>
    public interface IStudent
    {
        /// <summary>
        /// 增加學生
        /// </summary>
        /// <param name="studentID">學生ID</param>
        /// <param name="studentName">學生姓名</param>
        void Add(string studentID, string studentName);
    }
}
IStudent

    Service下的介面實作類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LinkTo.Test.Autofac.IService;

namespace LinkTo.Test.Autofac.Service
{
    /// <summary>
    /// 貓類
    /// </summary>
    public class Cat : IAnimalSleep
    {
        /// <summary>
        /// 睡眠
        /// </summary>
        public void Sleep()
        {
            Console.WriteLine("小貓咪睡著了zZ");
        }
    }
}
Cat
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LinkTo.Test.Autofac.IService;

namespace LinkTo.Test.Autofac.Service
{
    /// <summary>
    /// 狗類
    /// </summary>
    public class Dog : IAnimalBark, IAnimalSleep
    {
        /// <summary>
        /// 吠叫
        /// </summary>
        public void Bark()
        {
            Console.WriteLine("汪汪汪");
        }

        /// <summary>
        /// 睡眠
        /// </summary>
        public void Sleep()
        {
            Console.WriteLine("小狗狗睡著了zZ");
        }
    }
}
Dog
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LinkTo.Test.Autofac.IService;

namespace LinkTo.Test.Autofac.Service
{
    /// <summary>
    /// 學校類
    /// </summary>
    public class School : ISchool
    {
        /// <summary>
        /// IAnimalBark屬性
        /// </summary>
        public IAnimalBark AnimalBark { get; set; }

        /// <summary>
        /// 放學
        /// </summary>
        public void LeaveSchool()
        {
            AnimalBark.Bark();
            Console.WriteLine("你家的熊孩子放學了⊙o⊙");
        }
    }
}
School
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LinkTo.Test.Autofac.IService;

namespace LinkTo.Test.Autofac.Service
{
    /// <summary>
    /// 學生類
    /// </summary>
    public class Student : IStudent
    {
        /// <summary>
        /// 無參建構式
        /// </summary>
        public Student()
        { }

        /// <summary>
        /// 有參建構式
        /// </summary>
        /// <param name="studentID">學生ID</param>
        /// <param name="studentName">學生姓名</param>
        public Student(string studentID, string studentName)
        {
            Add(studentID, studentName);
        }

        /// <summary>
        /// 增加學生
        /// </summary>
        /// <param name="studentID">學生ID</param>
        /// <param name="studentName">學生姓名</param>
        public void Add(string studentID, string studentName)
        {
            Console.WriteLine($"新增的學生是:{studentName}");
        }
    }
}
Student
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using LinkTo.Test.Autofac.IService;

namespace LinkTo.Test.Autofac.Service
{
    /// <summary>
    /// 動物搖尾巴
    /// </summary>
    public class AnimalWagging
    {
        /// <summary>
        /// IAnimalBark屬性
        /// </summary>
        IAnimalBark animalBark;

        /// <summary>
        /// 有參建構式
        /// </summary>
        /// <param name="bark">IAnimalBark變數</param>
        public AnimalWagging(IAnimalBark bark)
        {
            animalBark = bark;
        }

        /// <summary>
        /// 搖尾巴
        /// </summary>
        public virtual void Wagging()
        {
            animalBark.Bark();
            Console.WriteLine("搖尾巴");
        }

        /// <summary>
        /// 計數
        /// </summary>
        /// <returns></returns>
        public static int Count()
        {
            return 6;
        }

        /// <summary>
        /// 任務
        /// </summary>
        /// <param name="name">動物名稱</param>
        /// <returns></returns>
        public virtual async Task<string> WaggingAsync(string name)
        {
            var result = await Task.Run(() => Count());
            return $"{name}搖了{result}下尾巴";
        }
    }
}
AnimalWagging

    2.2、Autofac安裝

    Client專案右鍵->管理 NuGet 程式包->Autofac,

    三、IoC-注冊

    3.1、型別注冊

    a)型別注冊:使用RegisterType進行注冊,

            //注冊Autofac組件
            ContainerBuilder builder = new ContainerBuilder();
            //注冊實作類Student,當我們請求IStudent介面的時候,回傳的是類Student的物件,
            builder.RegisterType<Student>().As<IStudent>();
            //上面這句也可改成下面這句,這樣請求Student實作了的任何介面的時候,都會回傳Student物件,
            //builder.RegisterType<Student>().AsImplementedInterfaces();
            IContainer container = builder.Build();
            //請求IStudent介面
            IStudent student = container.Resolve<IStudent>();
            student.Add("1001", "Hello");
View Code

    b)型別注冊(別名):假如一個介面有多個實作類,可以在注冊時起別名,

            ContainerBuilder builder = new ContainerBuilder();
            builder.RegisterType<Dog>().Named<IAnimalSleep>("Dog");
            builder.RegisterType<Cat>().Named<IAnimalSleep>("Cat");
            IContainer container = builder.Build();

            var dog = container.ResolveNamed<IAnimalSleep>("Dog");
            dog.Sleep();
            var cat = container.ResolveNamed<IAnimalSleep>("Cat");
            cat.Sleep();
View Code

    c)型別注冊(列舉):假如一個介面有多個實作類,也可以使用列舉的方式注冊,

        public enum AnimalType
        {
            Dog,
            Cat
        }
View Code
            ContainerBuilder builder = new ContainerBuilder();
            builder.RegisterType<Dog>().Keyed<IAnimalSleep>(AnimalType.Dog);
            builder.RegisterType<Cat>().Keyed<IAnimalSleep>(AnimalType.Cat);
            IContainer container = builder.Build();

            var dog = container.ResolveKeyed<IAnimalSleep>(AnimalType.Dog);
            dog.Sleep();
            var cat = container.ResolveKeyed<IAnimalSleep>(AnimalType.Cat);
            cat.Sleep();
View Code

    3.2、實體注冊

            ContainerBuilder builder = new ContainerBuilder();
            builder.RegisterInstance<IStudent>(new Student());
            IContainer container = builder.Build();

            IStudent student = container.Resolve<IStudent>();
            student.Add("1001", "Hello");
View Code

    3.3、Lambda注冊

    a)Lambda注冊

            ContainerBuilder builder = new ContainerBuilder();
            builder.Register(c => new Student()).As<IStudent>();
            IContainer container = builder.Build();

            IStudent student = container.Resolve<IStudent>();
            student.Add("1001", "Hello");
View Code

    b)Lambda注冊(NamedParameter)

            ContainerBuilder builder = new ContainerBuilder();
            builder.Register<IAnimalSleep>((c, p) =>
                {
                    var type = p.Named<string>("type");
                    if (type == "Dog")
                    {
                        return new Dog();
                    }
                    else
                    {
                        return new Cat();
                    }
                }).As<IAnimalSleep>();
            IContainer container = builder.Build();

            var dog = container.Resolve<IAnimalSleep>(new NamedParameter("type", "Dog"));
            dog.Sleep();
View Code

    3.4、程式集注冊

    如果有很多介面及實作類,假如覺得這種一一注冊很麻煩的話,可以一次性全部注冊,當然也可以加篩選條件,

            ContainerBuilder builder = new ContainerBuilder();
            Assembly assembly = Assembly.Load("LinkTo.Test.Autofac.Service");   //實作類所在的程式集名稱
            builder.RegisterAssemblyTypes(assembly).AsImplementedInterfaces();  //常用
            //builder.RegisterAssemblyTypes(assembly).Where(t=>t.Name.StartsWith("S")).AsImplementedInterfaces();  //帶篩選
            //builder.RegisterAssemblyTypes(assembly).Except<School>().AsImplementedInterfaces();  //帶篩選
            IContainer container = builder.Build();

            //單實作類的用法
            IStudent student = container.Resolve<IStudent>();
            student.Add("1001", "Hello");

            //多實作類的用法
            IEnumerable<IAnimalSleep> animals = container.Resolve<IEnumerable<IAnimalSleep>>();
            foreach (var item in animals)
            {
                item.Sleep();
            }
View Code

    3.5、泛型注冊

            ContainerBuilder builder = new ContainerBuilder();
            builder.RegisterGeneric(typeof(List<>)).As(typeof(IList<>));
            IContainer container = builder.Build();

            IList<string> list = container.Resolve<IList<string>>();
View Code

    3.6、默認注冊

            ContainerBuilder builder = new ContainerBuilder();
            //對于同一個介面,后面注冊的實作會覆寫之前的實作,
            //如果不想覆寫的話,可以用PreserveExistingDefaults,這樣會保留原來注冊的實作,
            builder.RegisterType<Dog>().As<IAnimalSleep>();
            builder.RegisterType<Cat>().As<IAnimalSleep>().PreserveExistingDefaults();  //指定為非默認值
            IContainer container = builder.Build();

            var dog = container.Resolve<IAnimalSleep>();
            dog.Sleep();
View Code

    四、IoC-注入

    4.1、建構式注入

            ContainerBuilder builder = new ContainerBuilder();
            builder.RegisterType<AnimalWagging>();
            builder.RegisterType<Dog>().As<IAnimalBark>();
            IContainer container = builder.Build();

            AnimalWagging animal = container.Resolve<AnimalWagging>();
            animal.Wagging();
View Code

    4.2、屬性注入

            ContainerBuilder builder = new ContainerBuilder();
            Assembly assembly = Assembly.Load("LinkTo.Test.Autofac.Service");                           //實作類所在的程式集名稱
            builder.RegisterAssemblyTypes(assembly).AsImplementedInterfaces().PropertiesAutowired();    //常用
            IContainer container = builder.Build();

            ISchool school = container.Resolve<ISchool>();
            school.LeaveSchool();
View Code

    五、IoC-事件

    Autofac在組件生命周期的不同階段,共對應了5個事件,執行順序如下所示:

    1.OnRegistered->2.OnPreparing->3.OnActivating->4.OnActivated->5.OnRelease

            ContainerBuilder builder = new ContainerBuilder();
            builder.RegisterType<Student>().As<IStudent>()
                .OnRegistered(e => Console.WriteLine("OnRegistered:在注冊的時候呼叫"))
                .OnPreparing(e => Console.WriteLine("OnPreparing:在準備創建的時候呼叫"))
                .OnActivating(e => Console.WriteLine("OnActivating:在創建之前呼叫"))
                //.OnActivating(e => e.ReplaceInstance(new Student("1000", "Test")))
                .OnActivated(e => Console.WriteLine("OnActivated:在創建之后調用"))
                .OnRelease(e => Console.WriteLine("OnRelease:在釋放占用的資源之前呼叫"));
            using (IContainer container = builder.Build())
            {
                IStudent student = container.Resolve<IStudent>();
                student.Add("1001", "Hello");
            }
View Code

    六、IoC-生命周期

    6.1、Per Dependency

    Per Dependency:為默認的生命周期,也被稱為"transient"或"factory",其實就是每次請求都創建一個新的物件,

            ContainerBuilder builder = new ContainerBuilder();
            Assembly assembly = Assembly.Load("LinkTo.Test.Autofac.Service");                                                   //實作類所在的程式集名稱
            builder.RegisterAssemblyTypes(assembly).AsImplementedInterfaces().PropertiesAutowired().InstancePerDependency();    //常用
            IContainer container = builder.Build();

            ISchool school1 = container.Resolve<ISchool>();
            ISchool school2 = container.Resolve<ISchool>();
            Console.WriteLine(school1.Equals(school2));
View Code

    6.2、Single Instance

    Single Instance:就是每次都用同一個物件,

            ContainerBuilder builder = new ContainerBuilder();
            Assembly assembly = Assembly.Load("LinkTo.Test.Autofac.Service");                                           //實作類所在的程式集名稱
            builder.RegisterAssemblyTypes(assembly).AsImplementedInterfaces().PropertiesAutowired().SingleInstance();   //常用
            IContainer container = builder.Build();

            ISchool school1 = container.Resolve<ISchool>();
            ISchool school2 = container.Resolve<ISchool>();
            Console.WriteLine(ReferenceEquals(school1, school2));
View Code

    6.3、Per Lifetime Scope

    Per Lifetime Scope:同一個Lifetime生成的物件是同一個實體,

            ContainerBuilder builder = new ContainerBuilder();
            builder.RegisterType<School>().As<ISchool>().InstancePerLifetimeScope();
            IContainer container = builder.Build();
            ISchool school1 = container.Resolve<ISchool>();
            ISchool school2 = container.Resolve<ISchool>();
            Console.WriteLine(school1.Equals(school2));
            using (ILifetimeScope lifetime = container.BeginLifetimeScope())
            {
                ISchool school3 = lifetime.Resolve<ISchool>();
                ISchool school4 = lifetime.Resolve<ISchool>();
                Console.WriteLine(school3.Equals(school4));
                Console.WriteLine(school2.Equals(school3));
            }
View Code

    七、IoC-通過組態檔使用Autofac

    7.1、組件安裝

    Client專案右鍵->管理 NuGet 程式包->Autofac.Configuration及Microsoft.Extensions.Configuration.Xml,

    7.2、組態檔

    新建一個AutofacConfigIoC.xml檔案,在其屬性的復制到輸出目錄項下選擇始終復制,

<?xml version="1.0" encoding="utf-8" ?>
<autofac defaultAssembly="LinkTo.Test.Autofac.IService">
  <!--無注入-->
  <components name="1001">
    <type>LinkTo.Test.Autofac.Service.Student, LinkTo.Test.Autofac.Service</type>
    <services name="0" type="LinkTo.Test.Autofac.IService.IStudent" />
    <injectProperties>true</injectProperties>
  </components>
  <components name="1002">
    <type>LinkTo.Test.Autofac.Service.Dog, LinkTo.Test.Autofac.Service</type>
    <services name="0" type="LinkTo.Test.Autofac.IService.IAnimalBark" />
    <injectProperties>true</injectProperties>
  </components>
  <!--建構式注入-->
  <components name="2001">
    <type>LinkTo.Test.Autofac.Service.AnimalWagging, LinkTo.Test.Autofac.Service</type>
    <services name="0" type="LinkTo.Test.Autofac.Service.AnimalWagging, LinkTo.Test.Autofac.Service" />
    <injectProperties>true</injectProperties>
  </components>
  <!--屬性注入-->
  <components name="3001">
    <type>LinkTo.Test.Autofac.Service.School, LinkTo.Test.Autofac.Service</type>
    <services name="0" type="LinkTo.Test.Autofac.IService.ISchool" />
    <injectProperties>true</injectProperties>
  </components>
</autofac>
View Code

    7.3、測驗代碼

            //加載配置
            ContainerBuilder builder = new ContainerBuilder();
            var config = new ConfigurationBuilder();
            config.AddXmlFile("AutofacConfigIoC.xml");
            var module = new ConfigurationModule(config.Build());
            builder.RegisterModule(module);
            IContainer container = builder.Build();
            //無注入測驗
            IStudent student = container.Resolve<IStudent>();
            student.Add("1002", "World");
            //建構式注入測驗
            AnimalWagging animal = container.Resolve<AnimalWagging>();
            animal.Wagging();
            //屬性注入測驗
            ISchool school = container.Resolve<ISchool>();
            school.LeaveSchool();
View Code

    八、AOP 

    8.1、組件安裝

    Client專案右鍵->管理 NuGet 程式包->Autofac.Extras.DynamicProxy,

    8.2、拉截器

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Castle.DynamicProxy;

namespace LinkTo.Test.Autofac.Client
{
    /// <summary>
    /// 攔截器:需實作IInterceptor介面,
    /// </summary>
    public class CallLogger : IInterceptor
    {
        private readonly TextWriter _output;

        public CallLogger(TextWriter output)
        {
            _output = output;
        }

        /// <summary>
        /// 攔截方法:列印被攔截的方法--執行前的名稱、引數以及執行后的回傳結果,
        /// </summary>
        /// <param name="invocation">被攔截方法的資訊</param>
        public void Intercept(IInvocation invocation)
        {
            //空白行
            _output.WriteLine();

            //在下一個攔截器或目標方法處理之前的處理
            _output.WriteLine($"呼叫方法:{invocation.Method.Name}");

            if (invocation.Arguments.Length > 0)
            {
                _output.WriteLine($"引數:{string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray())}");
            }

            //呼叫下一個攔截器(若存在),直到最終的目標方法(Target Method),
            invocation.Proceed();

            //獲取被代理方法的回傳型別
            var returnType = invocation.Method.ReturnType;

            //異步方法
            if (IsAsyncMethod(invocation.Method))
            {
                //Task:回傳值是固定型別
                if (returnType != null && returnType == typeof(Task))
                {
                    //定義一個異步方法來等待目標方法回傳的Task
                    async Task Continuation() => await (Task)invocation.ReturnValue;
                    //Continuation()中并沒有使用await,所以Continuation()就如同步方法一樣是阻塞的,
                    invocation.ReturnValue =https://www.cnblogs.com/atomy/p/ Continuation();
                }
                //Task<T>:回傳值是泛型型別
                else
                {
                    //獲取被代理方法的回傳型別
                    var returnTypeT = invocation.Method.ReflectedType;
                    if (returnTypeT != null)
                    {
                        //獲取泛型引數集合,集合中的第一個元素等價于typeof(Class),
                        var resultType = invocation.Method.ReturnType.GetGenericArguments()[0];
                        //利用反射獲得等待回傳值的異步方法
                        MethodInfo methodInfo = typeof(CallLogger).GetMethod("HandleAsync", BindingFlags.Public | BindingFlags.Instance);
                        //呼叫methodInfo類的MakeGenericMethod()方法,用獲得的型別T(<resultType>)來重新構造HandleAsync()方法,
                        var mi = methodInfo.MakeGenericMethod(resultType);
                        //Invoke:使用指定引數呼叫由當前實體表示的方法或建構式,
                        invocation.ReturnValue = https://www.cnblogs.com/atomy/p/mi.Invoke(this, new[] { invocation.ReturnValue });
                    }
                }

                var type = invocation.Method.ReturnType;
                var resultProperty = type.GetProperty("Result");

                if (resultProperty != null)
                    _output.WriteLine($"方法結果:{resultProperty.GetValue(invocation.ReturnValue)}");
            }
            //同步方法
            else
            {
                if (returnType != null && returnType != typeof(void))
                    _output.WriteLine($"方法結果:{invocation.ReturnValue}");
            }
        }

        /// <summary>
        /// 判斷是否異步方法
        /// </summary>
        public static bool IsAsyncMethod(MethodInfo method)
        {
            return 
                (
                    method.ReturnType == typeof(Task) || 
                    (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>))
                );
        }

        /// <summary>
        /// 構造等待回傳值的異步方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="task"></param>
        /// <returns></returns>
        public async Task<T> HandleAsync<T>(Task<T> task)
        {
            var t = await task;
            return t;
        }
    }
}
CallLogger
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Castle.DynamicProxy;

namespace LinkTo.Test.Autofac.Client
{
    public class CallTester: IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            Console.WriteLine("啥也不干");
            invocation.Proceed();
            Console.WriteLine("也不干啥");
        }
    }
}
CallTester

    8.3、測驗代碼

    注意:對于以類方式的注入,Autofac Interceptor要求類的方法必須為virtual方法,如AnimalWagging類的Wagging()、WaggingAsync(string name)都加了virtual修飾符,

            ContainerBuilder builder = new ContainerBuilder();

            //注冊攔截器
            builder.Register(c => new CallLogger(Console.Out));
            builder.Register(c => new CallTester());

            //動態注入攔截器

            //這里定義了兩個攔截器,注意它們的順序,
            builder.RegisterType<Student>().As<IStudent>().InterceptedBy(typeof(CallLogger), typeof(CallTester)).EnableInterfaceInterceptors();

            //這里定義了一個攔截器
            builder.RegisterType<AnimalWagging>().InterceptedBy(typeof(CallLogger)).EnableClassInterceptors();
            builder.RegisterType<Dog>().As<IAnimalBark>();

            IContainer container = builder.Build();
            IStudent student = container.Resolve<IStudent>();
            student.Add("1003", "Kobe");

            AnimalWagging animal = container.Resolve<AnimalWagging>();
            animal.Wagging();

            Task<string> task = animal.WaggingAsync("哈士奇");
            Console.WriteLine($"{task.Result}");
View Code

 

    IoC參考自:

    https://www.xin3721.com/ArticlecSharp/c14013.html

    https://www.cnblogs.com/GoogleGetZ/p/10218721.html

    http://niuyi.github.io/blog/2012/04/06/autofac-by-unit-test/

    https://www.cnblogs.com/kissdodog/p/3611799.html

 

    AOP參考自:

    https://www.cnblogs.com/stulzq/p/6880394.html

    https://blog.csdn.net/weixin_38211198/article/details/105925821

    https://blog.csdn.net/q932104843/article/details/97611912

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/42843.html

標籤:C#

上一篇:C# 基礎知識系列- 17 小工具優化

下一篇:教你配置windows上的windbg,linux上的lldb,打入clr內部這一篇就夠了

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • WebAPI簡介

    Web體系結構: 有三個核心:資源(resource),URL(統一資源識別符號)和表示 他們的關系是這樣的:一個資源由一個URL進行標識,HTTP客戶端使用URL定位資源,表示是從資源回傳資料,媒體型別是資源回傳的資料格式。 接下來我們說下HTTP. HTTP協議的系統是一種無狀態的方式,使用請求/ ......

    uj5u.com 2020-09-09 22:07:47 more
  • asp.net core 3.1 入口:Program.cs中的Main函式

    本文分析Program.cs 中Main()函式中代碼的運行順序分析asp.net core程式的啟動,重點不是剖析原始碼,而是理清程式開始時執行的順序。到呼叫了哪些實體,哪些法方。asp.net core 3.1 的程式入口在專案Program.cs檔案里,如下。ususing System; us ......

    uj5u.com 2020-09-09 22:07:49 more
  • asp.net網站作為websocket服務端的應用該如何寫

    最近被websocket的一個問題困擾了很久,有一個需求是在web網站中搭建websocket服務。客戶端通過網頁與服務器建立連接,然后服務器根據ip給客戶端網頁發送資訊。 其實,這個需求并不難,只是剛開始對websocket的內容不太了解。上網搜索了一下,有通過asp.net core 實作的、有 ......

    uj5u.com 2020-09-09 22:08:02 more
  • ASP.NET 開源匯入匯出庫Magicodes.IE Docker中使用

    Magicodes.IE在Docker中使用 更新歷史 2019.02.13 【Nuget】版本更新到2.0.2 【匯入】修復單列匯入的Bug,單元測驗“OneColumnImporter_Test”。問題見(https://github.com/dotnetcore/Magicodes.IE/is ......

    uj5u.com 2020-09-09 22:08:05 more
  • 在webform中使用ajax

    如果你用過Asp.net webform, 說明你也算是.NET 開發的老兵了。WEBform應該是2011 2013左右,當時還用visual studio 2005、 visual studio 2008。后來基本都用的是MVC。 如果是新開發的專案,估計沒人會用webform技術。但是有些舊版 ......

    uj5u.com 2020-09-09 22:08:50 more
  • iis添加asp.net網站,訪問提示:由于擴展配置問題而無法提供您請求的

    今天在iis服務器配置asp.net網站,遇到一個問題,記錄一下: 問題:由于擴展配置問題而無法提供您請求的頁面。如果該頁面是腳本,請添加處理程式。如果應下載檔案,請添加 MIME 映射。 WindowServer2012服務器,添加角色安裝完.netframework和iis之后,運行aspx頁面 ......

    uj5u.com 2020-09-09 22:10:00 more
  • WebAPI-處理架構

    帶著問題去思考,大家好! 問題1:HTTP請求和回傳相應的HTTP回應資訊之間發生了什么? 1:首先是最底層,托管層,位于WebAPI和底層HTTP堆疊之間 2:其次是 訊息處理程式管道層,這里比如日志和快取。OWIN的參考是將訊息處理程式管道的一些功能下移到堆疊下端的OWIN中間件了。 3:控制器處理 ......

    uj5u.com 2020-09-09 22:11:13 more
  • 微信門戶開發框架-使用指導說明書

    微信門戶應用管理系統,采用基于 MVC + Bootstrap + Ajax + Enterprise Library的技術路線,界面層采用Boostrap + Metronic組合的前端框架,資料訪問層支持Oracle、SQLServer、MySQL、PostgreSQL等資料庫。框架以MVC5,... ......

    uj5u.com 2020-09-09 22:15:18 more
  • WebAPI-HTTP編程模型

    帶著問題去思考,大家好!它是什么?它包含什么?它能干什么? 訊息 HTTP編程模型的核心就是訊息抽象,表示為:HttPRequestMessage,HttpResponseMessage.用于客戶端和服務端之間交換請求和回應訊息。 HttpMethod類包含了一組靜態屬性: private stat ......

    uj5u.com 2020-09-09 22:15:23 more
  • 部署WebApi隨筆

    一、跨域 NuGet參考Microsoft.AspNet.WebApi.Cors WebApiConfig.cs中配置: // Web API 配置和服務 config.EnableCors(new EnableCorsAttribute("*", "*", "*")); 二、清除默認回傳XML格式 ......

    uj5u.com 2020-09-09 22:15:48 more
最新发布
  • C#多執行緒學習(二) 如何操縱一個執行緒

    <a href="https://www.cnblogs.com/x-zhi/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2943582/20220801082530.png" alt="" /></...

    uj5u.com 2023-04-19 09:17:20 more
  • C#多執行緒學習(二) 如何操縱一個執行緒

    C#多執行緒學習(二) 如何操縱一個執行緒 執行緒學習第一篇:C#多執行緒學習(一) 多執行緒的相關概念 下面我們就動手來創建一個執行緒,使用Thread類創建執行緒時,只需提供執行緒入口即可。(執行緒入口使程式知道該讓這個執行緒干什么事) 在C#中,執行緒入口是通過ThreadStart代理(delegate)來提供的 ......

    uj5u.com 2023-04-19 09:16:49 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    <a href="https://www.cnblogs.com/huangxincheng/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/214741/20200614104537.png" alt="" /&g...

    uj5u.com 2023-04-18 08:39:04 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    一:背景 1. 講故事 前段時間協助訓練營里的一位朋友分析了一個程式卡死的問題,回過頭來看這個案例比較經典,這篇稍微整理一下供后來者少踩坑吧。 二:WinDbg 分析 1. 為什么會卡死 因為是表單程式,理所當然就是看主執行緒此時正在做什么? 可以用 ~0s ; k 看一下便知。 0:000> k # ......

    uj5u.com 2023-04-18 08:33:10 more
  • SignalR, No Connection with that ID,IIS

    <a href="https://www.cnblogs.com/smartstar/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/u36196.jpg" alt="" /></a>...

    uj5u.com 2023-03-30 17:21:52 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:15:33 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:13:31 more
  • C#遍歷指定檔案夾中所有檔案的3種方法

    <a href="https://www.cnblogs.com/xbhp/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/957602/20230310105611.png" alt="" /></a&...

    uj5u.com 2023-03-27 14:46:55 more
  • C#/VB.NET:如何將PDF轉為PDF/A

    <a href="https://www.cnblogs.com/Carina-baby/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2859233/20220427162558.png" alt="" />...

    uj5u.com 2023-03-27 14:46:35 more
  • 武裝你的WEBAPI-OData聚合查詢

    <a href="https://www.cnblogs.com/podolski/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/616093/20140323000327.png" alt="" /><...

    uj5u.com 2023-03-27 14:46:16 more