首页 新闻 会员 周边

ashx中如何获取mef的导出呢

0
悬赏园豆:5 [已关闭问题] 关闭于 2013-09-17 09:37

在controller里获取mef导出都没问题,但是ashx中就不行了,大家有什么办法

flyjonson的主页 flyjonson | 初学一级 | 园豆:103
提问于:2013-08-20 10:22
< >
分享
所有回答(1)
0
Launcher | 园豆:45045 (高人七级) | 2013-08-20 10:40

不会,因为unity也需要实现IHttpHandlerFactory;所以我换了MEF,没想到也是要实现,能否稍微写两句点拨下

支持(0) 反对(0) flyjonson | 园豆:103 (初学一级) | 2013-08-20 10:44

@flyjonson: 我给你的连接里面有个完整的实现。

支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2013-08-20 10:51
是这段吗,我拷贝下来报错
public class SimpleWebHandlerFactory : IHttpHandlerFactory                                                           
{                                                                                                                    
    public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path) 
    {                                                                                                                
        Type type = WebHandlerParser.GetCompiledType(context, virtualPath, path);                                    
        if (!(typeof(IHttpHandler).IsAssignableFrom(type)))                                                          
            throw new HttpException("Type does not implement IHttpHandler: " + type.FullName);                       
                                                                                                                     
        return Activator.CreateInstance(type) as IHttpHandler;                                                       
    }                                                                                                                
                                                                                                                     
    public virtual void ReleaseHandler(IHttpHandler handler)                                                         
    {                                                                                                                
    }                                                                                                                
}      
 
 
internal class WebHandlerParser : SimpleWebHandlerParser                                            
{                                                                                                   
    internal WebHandlerParser(HttpContext context, string virtualPath, string physicalPath)         
        : base(context, virtualPath, physicalPath)                                                  
    {                                                                                               
    }                                                                                               
                                                                                                    
    public static Type GetCompiledType(HttpContext context, string virtualPath, string physicalPath)
    {                                                                                               
        WebHandlerParser parser = new WebHandlerParser(context, virtualPath, physicalPath);         
        Type type = parser.GetCompiledTypeFromCache();                                              
        if (type != null)                                                                           
            return type;                                                                            
        else                                                                                        
            throw new HttpException(string.Format("File '{0}' is not a web handler.", virtualPath));
    }                                                                                               
                                                                                                    
    protected override string DefaultDirectiveName                                                  
    {                                                                                               
        get                                                                                         
        {                                                                                           
            return "webhandler";                                                                    
        }                                                                                           
    }                                                                                               
}           
 
public class ComposableWebHandlerFactory : SimpleWebHandlerFactory
{
    public override IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path)
    {
        IHttpHandler handler = base.GetHandler(context, requestType, virtualPath, path);
 
        if (handler != null)
        {
            CompositionBatch batch = new CompositionBatch();
            batch = BuildUp(batch, handler);
            ContextualCompositionHost.Container.Compose(batch);
        }
 
        return handler;
    }
 
    private CompositionBatch BuildUp(CompositionBatch batch, IHttpHandler handler)
    {
        ComposablePart part = AttributedModelServices.CreatePart(handler);
 
        // any imports?
        if (part.ImportDefinitions.Any())
        {
            if (part.ExportDefinitions.Any())
                // exports are not allowed
                throw new Exception(string.Format("'{0}': Handlers cannot be exportable", handler.GetType().FullName));
 
            batch.AddPart(part);
        }
 
        return batch;
    }
}                  
View Code

 

支持(0) 反对(0) flyjonson | 园豆:103 (初学一级) | 2013-08-20 11:10

@flyjonson: The source code can be downloaded here.

如果你不能读懂他的代码的话,我的建议是你把他的源码下载下来,运行几次,分析下程序的逻辑。

支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2013-08-20 11:13

@Launcher: 好的。我先试试,谢谢

支持(0) 反对(0) flyjonson | 园豆:103 (初学一级) | 2013-08-20 11:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册