|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThe Server Side published a beta version of "Improving .NET Application Performance and Scalability" by Microsoft Pattern and Practice Group. In chapter 8 about remoting, Windows Service Host for remoting is labeled not optimized since it uses Workstation GC (Garbage Collection). Also according to Gregor Noriskin in this article: "The Server GC is optimized for throughput and multi-processor scalability" and "If you are building a server application that is going to run on multiprocessor machines then it is highly recommended that you use the Server GC". This article intends to implement a TCP Remoting Host with Server GC. The source code is included for developers in this community to comment and improve. I will try to explain step by step how this host is built, and hopefully generate some interest in writing a truly scalable TCP Remoting Host. This article primarily targets developers with C# and .NET Framework experience (Remoting). The usage of ATL is at beginner level and is for the sole purpose of creating CLR host. How to specify Server GCServer GC has to be specified before AppDomain gets loaded and user code gets executed. So we will have to use unmanaged code. The following is the API from MSDN documentation to do just that: HRESULT hr =
CorBindToRuntimeEx(pszVer, pszFlavor, Flags, CLSID_CorRuntimeHost,
IID_ICorRuntimeHost, (void **)&pHost);
According to this article, LPWSTR pszVer = L"v1.1.4322"; LPWSTR pszFlavor = L"svr"; ICorRuntimeHost *pHost = NULL; // this is changed to a global unmanaged variable in download code HRESULT hr = CorBindToRuntimeEx(pszVer, pszFlavor, STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN, CLSID_CorRuntimeHost, IID_ICorRuntimeHost, (void **)&pHost); Clearly, I have defined Server GC as "CLR Server Build with non-concurrent GC" and I believe Server GC has better scalability and can utilize multiple CPU better. How to get to Default AppDomain:After creating CLR Host as above, we need to access its default AppDomain to load our Remoting host code. In fact, all managed code must reside in an AppDomain, and Default AppDomain for the CLR Host is the easiest choice: pHost->Start(); mscorlib::_AppDomain *pDefaultDomain = NULL; IUnknown *pAppDomainPunk = NULL; hr = pHost->GetDefaultDomain(&pAppDomainPunk); hr = pAppDomainPunk->QueryInterface(__uuidof(mscorlib::_AppDomain), (void**) &pDefaultDomain); Notice that JQD::RemoteObjectLoader *pRemotingHost=NULL; mscorlib::_ObjectHandle *pObjHandle = NULL; hr = pDefaultDomain->CreateInstance(_bstr_t("RemoteObjectLoader"), _bstr_t("JQD.RemoteObjectLoader"), &pObjHandle); VARIANT v; VariantInit(&v); hr = pObjHandle->Unwrap(&v); hr =v.pdispVal->QueryInterface( __uuidof(RemoteObjectLoader::_RemoteObjectLoader), (void**) &pRemotingHost); pRemotingHost->Load(); Notice that pointer to default AppDomain creates an instance of a managed object with a unmanaged handle, which can be unwrapped and cast into a managed pointer public class RemoteObjectLoader { public void Load() { ChannelServices.RegisterChannel(new TCPChannel(10002)); RemotingConfiguration.RegisterWellKnownServiceType( typeof (JQD.MyClass), "MyClassURI", WellKnownObjectMode.SingleCall); } } This completes the starting of TCP Remoting using Server GC. As illustrated in the following diagram, this process utilizes both managed and unmanaged code to build the CLR host and eventually load Remoting Hosting code
Finally, I have hard coded Remote classes in How to run the sample code:After downloading the zip file, extract it to a directory such as c:\working\CLRMHost. You need to do the following few things:
Some important detailsThis .NET Solution consists mainly of two projects: a C++.NET Windows Service Project and a Remoting Loader .NET Class Library Project. All unmanaged C++ code resides in the Windows Service Project, which has included important header file mscoree.h for accessing ConclusionThis article provides a reasonably complete implementation for TCP Remoting host using Windows Service and Server GC. Anyone can just download it and add your own object for TCP remoting. Although it does not have proven scalability like IIS hosted HTTP Remoting, overtime developers in this community should be able to get a concrete and real understanding of TCP remoting host, and improving its scalability and throughput.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||