.net core 2.0學習筆記(六):Remoting核心類庫RealProxy遷移

來源:互聯網
上載者:User

標籤:border   tproxy   包含   order   string   學習   run   analyzer   方法   

    在學習.net core的過程中,我們已經明確被告知,Remoting將不會被支援。官方的解釋是,.net framework 類型包含了太多的Runtime的內容,是一個非常重量級的服務實現,已被確定為一項有問題的體繫結構。說白了就是遷移的難度很大,.net core中直接不提供了。微軟的建議是,如果是進程內或跨進程通訊,建議我們使用 Pipes或者記憶體對應檔(Memory Mapped Files)。如果是機器間的調用,建議我們採用網路通訊的方案,比如HTTP、WCF等。

    好吧,既然微軟官方不支援, 我們只能自己想辦法搞定了。

    OK,說遷移方案前,我們先看看.net Framework中,使用Remoting的代碼:

    通過Remoting封裝後的服務調用方法,我們可以直接得到一個服務介面的實現,通過調用次介面的本地代理實現執行遠程調用。

static void Main(string[] args)
{
             IUserService service = InvokeSerice.Proxy<IUserService>();
             string uName = service.GetCurrentUserName();

            Console.WriteLine($"目前使用者名:{uName}");
             Console.ReadLine();
}

    IUserService介面的聲明如下:

public interface IUserService
{
         string GetCurrentUserName();
}

    InvokeService方法的核心實現邏輯如下:

//遠程調用服務提供類

public class InvokeService
{

        //擷取一個服務的本地調用代理對象
         public static T Proxy<T>()
         {
             var proxy = new InvokeProxy<T>();
             return (T)proxy.GetTransparentProxy();
         }
}

 

//服務本地代理對象實作類別

public class InvokeProxy<T> : RealProxy
{
         private Type type = null;
         public InvokeProxy() : this(typeof(T))
         {
             type = typeof(T);
         }

        protected InvokeProxy(Type classToProxy) : base(classToProxy)
         {
         }

        //接收本地調用請求,然後轉寄遠端存取

        public override IMessage Invoke(IMessage msg)
         {
             Console.WriteLine("Invoke 遠程服務調用!");
             ReturnMessage message = new System.Runtime.Remoting.Messaging.ReturnMessage("Test",null,0,null,(IMethodCallMessage)msg);

            return (IMessage)message;
         }
}

    通過.NET Portability Analyzer分析這個工程,我們會得到系統不支援的結果。

    既然,.net core 已經不支援RealProxy,那麼就只能另起爐灶了。通過翻閱.net core的代碼,終於發現了一個程式集:System.Reflection.DispatchProxy。此程式中,有一個類型DispatchProxy。(靈感還是來自於WCF。WCF是.net 中重量級的API,微軟不可能不支援,通過翻閱其中的實現邏輯,終於到了RealProxy的替代方案。)

     好吧,通過DispatchProxy提供的功能,很容易可以替代RealProxyP,Remoting的問題終於比較完美的解決了。下面就貼一下替換後的服務實現代碼吧。

public class InvokeSerice
{
         public static T Proxy<T>()
         {
             return DispatchProxy.Create<T, InvokeProxy<T>>();
         }
}

public class InvokeProxy<T> : DispatchProxy
{
         private Type type = null;
         public InvokeProxy()
         {
             type = typeof(T);
         }

        protected override object Invoke(MethodInfo targetMethod, object[] args)
         {
             Console.WriteLine("Invoke 遠程服務調用!");

            return "Test";
         }
}

.net core 2.0學習筆記(六):Remoting核心類庫RealProxy遷移

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.