枚举 GAC 中的程序集

发表评论38次阅读2010.09.23 16:07 作者:Felicia 

GAC 的全称是 Global Assembly Cache (全局程序集缓存),作用是可以存放一些有很多程序都要用到的公共 Assembly,例如 System.Data、System.Windows.Forms 等等。这样,很多程序就可以从 GAC 里面取得 Assembly,而不需要再把所有要用到的 Assembly 都拷贝到应用程序的执行目录下面。

最近想通过程序发送另一个 .Net 程序到其他机器上执行。由于 .Net 程序可能会依赖其他程序集,这些程序集里面,有些是在目标机器 GAC 中的系统程序集,有些是不在目标机器 GAC 中的第三方的程序集。显然为了节省传输时间和带宽资源,我们只需要发送目标机器 GAC 中没有的程序集即可。以下的一段代码摘自 CSDN,演示了怎样利用 COM 调用枚举所有在 GAC 中的程序集。之所以采用这种方法,是因为 .Net Framework 根本没有提供托管 API 来进行同样的操作。

using System;
using System.Text;
using System.Runtime.InteropServices;
 
[ComImport]
[Guid("21b8916c-f28e-11d2-a473-00c04f8ef448")]
[InterfaceType (ComInterfaceType.InterfaceIsIUnknown)]
 
interface IAssemblyEnum
{
 
    
IntPtr GetNextAssembly(
        
/* [in] */ IntPtr pvReserved,
        
/* [out] */ out IAssemblyName ppName,
        
/* [in] */ uint dwFlags);
    
    
void Reset();
    
    
void Clone(
        
/* [out] */ out IAssemblyEnum ppEnum);
    
}
 
[ComImport]
[Guid("CD193BC0-B4BC-11d2-9833-00C04FC31D2E")]
[InterfaceType (ComInterfaceType.InterfaceIsIUnknown)]
 
interface IAssemblyName
{
    
void SetProperty(
        
/* [in] */ uint PropertyId,
        
/* [in] */ IntPtr pvProperty,
        
/* [in] */ uint cbProperty);
    
    
void GetProperty(
        
/* [in] */ uint PropertyId,
        
/* [out] */ IntPtr pvProperty,
        
/* [out][in] */ out uint pcbProperty);
    
    
void Finalize();
    
    
void GetDisplayName(
        
/* [out] */ StringBuilder szDisplayName,
        
/* [out][in] */ ref uint pccDisplayName,
        
/* [in] */ uint dwDisplayFlags);
    
    
void Reserved(
        
/* [in] */ ref Guid refIID,
        
/* [in] */ IntPtr pUnkReserved1,
        
/* [in] */ IntPtr pUnkReserved2,
        
/* [in] */ string szReserved,
        
/* [in] */ Int64 llReserved,
        
/* [in] */ IntPtr pvReserved,
        
/* [in] */ uint cbReserved,
        
/* [out] */ IntPtr ppReserved);
    
    
void GetName(
        
/* [out][in] */ ref uint lpcwBuffer,
        
/* [out] */ StringBuilder pwzName);
    
    
void GetVersion(
        
/* [out] */ out uint pdwVersionHi,
        
/* [out] */ out uint pdwVersionLow);
    
    
void IsEqual(
        
/* [in] */ IAssemblyName pName,
        
/* [in] */ uint dwCmpFlags);
    
    
void Clone(
        
/* [out] */out IAssemblyName pName);
    
}
class GacList
{
    
[DllImport ("fusion.dll")]
    
static extern IntPtr CreateAssemblyEnum (out IAssemblyEnum  pEnum,IntPtr pUnkReserved,IntPtr pName,uint dwFlags,IntPtr  pvReserved);
    
const uint ASM_CACHE_ZAP       = 0x01;
    
const uint ASM_CACHE_GAC       = 0x02;
    
const uint ASM_CACHE_DOWNLOAD  = 0x04;
    
const uint ASM_CACHE_ROOT      = 0x08;
 
    
static void Main ()
    
{
        
IAssemblyEnum gacEnum;
        
//创建GAC程序集的枚举器
        
CreateAssemblyEnum (out gacEnum,IntPtr.Zero,IntPtr.Zero,ASM_CACHE_GAC,IntPtr.Zero);
        
{
            
IAssemblyName asm;
            
gacEnum.GetNextAssembly (IntPtr.Zero,out asm,0);
            
            
while (asm != null)
            
{
                
StringBuilder sbuf = new StringBuilder(1024);
                
uint ccbuf = 1024;
                
//获取程序集显示名称
                
asm.GetName (ref ccbuf,sbuf);
                
//打印程序集名称
                
Console.WriteLine (sbuf.ToString());
                
//释放COM对象
                
Marshal.ReleaseComObject (asm);
                
//枚举下一个
                
gacEnum.GetNextAssembly (IntPtr.Zero,out asm,0);
            
}
        
}
        
Marshal.ReleaseComObject (gacEnum);
    
}
}
标签:, | 分类:心情日记, 精华

WCF Endpoint 配置

发表评论33次阅读2010.09.23 15:53 作者:Felicia 

以下配置列出了传送大量数据的 WCF Endpoint 配置,Timeout 和 Size 还能够再增加,Length 已经是最大值了。

<system.serviceModel>
 
<bindings>
    
<netTcpBinding>
      
<binding name="NetTcp_Reliable" receiveTimeout="10:00:00" openTimeout="10:00:00" closeTimeout="10:00:00" sendTimeout="10:00:00" maxReceivedMessageSize="16777216" maxBufferSize="16777216" maxBufferPoolSize="16777216">
        
<readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
      
</binding>
    
</netTcpBinding>
 
</bindings>
 
<client>
    
<endpoint address="net.tcp://localhost:10001/TaskDispatchService" binding="netTcpBinding" bindingConfiguration="NetTcp_Reliable" contract="Fdc.Contract.IFdcTaskDispatchService" name="defaultEndpoint"/>
 
</client>
</system.serviceModel>
标签:, , , | 分类:网络技术

Win7 或 Windows Server 2008 启用 ASP.NET 4

发表评论169次阅读2010.09.21 10:40 作者:Felicia 

在默认情况下,Win7 和 Windows Server 2008 是不支持 ASP.NET 4 的。安装了 VS2010 之后,IIS 也不会自动启用 ASP.NET 4。
我们需要做如下两个步骤来使 IIS 支持 ASP.NET 4:

  • 向 IIS 注册 .Net Framework
    1. 用管理员启动 cmd.exe
    2. cd “C:\Windows\Microsoft.NET\Framework\v4.0.30319″ (如果 .Net Framework 4 发布了新的版本,v4.0.30319 可能会是其他名称)
    3. aspnet_regiis.exe -i
  • 在“运行”中输入inetmgr,打开 IIS 管理器,在应用程序池 (Application pool) 中,会看到 “ASP.NET v4“ 选项,将 VS2010 发布的网站的应用程序池改成这个 “ASP.NET v4″
标签:, | 分类:网络技术

C# 线程终止(Abort)的原理

1个评论271次阅读2010.08.17 8:37 作者:Felicia 

假设有两个线程A和B,A调用了b.Abort,这句的作用就是,无论B运行到哪里了,在B中当前运行点上抛出ThreadAbortException,A中无法catch到这个Exception,因为它根本不在A中。B可以用catch探测到这个异常,但这个异常会在B的每层catch的末尾自动重新抛出,所以实际上无法被B的任何catch语句真的catch住,他会一直抛出到B的最顶层,使得B被中断。

综上所述,不是将要终结B线程时用ThreadAbortException通知B,而是B就是被这个ThreadAbortException本身终结的,这个异常就是终结线程的原理。通知只是其附加功能。不是因为终结是非正常或者什么什么其他原因所以才要抛这个异常,而是就通过此异常终结线程,没有这个异常就不能使线程终结。

标签: | 分类:C#

我现在用的服务器

1个评论30次阅读2010.07.12 17:16 作者:Felicia 

标签: | 分类:心情日记