WebApp.Start<&TSTARTUP GT;方法类型参数 [英] WebApp.Start<TStartup> Method Type Parameter

查看:1087
本文介绍了WebApp.Start<&TSTARTUP GT;方法类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用基于这篇文章Owin我的Windows服务应用程序中设置自主机:

In setting up my Windows Service application to self host using Owin based on this article:

http://www.asp.net/web-api/overview/hosting-aspnet-web -API /使用-owin到自托管的Web-API

我用WebApp.Start方法的这个过载:

I used this overload of the WebApp.Start method:

WebApp.Start方法(String )

下面是我的代码:

//(in startup method) 
_server = WebApp.Start<Startup>(BaseAddress);

public class Startup
{
    // This code configures Web API. The Startup class is specified as a type
    // parameter in the WebApp.Start method.
    public void Configuration(IAppBuilder appBuilder)
    {
        // Configure Web API for self-host. 
        var config = new HttpConfiguration();
        config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new
        {
            id = RouteParameter.Optional
        });

        appBuilder.UseWebApi(config);
    }
} 



它工作得很好,所以没有怨言。

It works fine, so no complaints.

但究竟什么是Start方法的类型参数的要求?它似乎没有任何限制,我一直没能找到我的选项/要求该参数什么的任何文件。它看起来该拿IAppBuilder作为参数的方法呢?如果我改变配置()方法以别的名字?如果我做的方法内部?是我可以用这个类配置有其他的选择?

But what exactly are the requirements of the type parameter of the Start method? It doesn't appear to have any constraints, and I haven't been able to find any documentation on what my options/requirements are on this parameter. Does it look for methods that take IAppBuilder as a parameter? What if I change the name of the Configuration() method to something else? What if I make the method internal? Are there other options I can configure with this class?

哪里是这一切的这个记录?我觉得自己就像一个没有上面链接的文章,我永远不会已经能够想出实现的东西。

Where is all of this this documented? I feel like without the article linked above, I never would have been able to figure out what to implement.

推荐答案

Web应用程序类使用反射来获得一个指向配置(IAppBuilder),那么方法调用它。如果您提供的泛型类型参数的类没有一个配置方法与预期的参数然后你在运行时出现错误。

The WebApp class uses reflection to get a pointer to the Configuration(IAppBuilder) method then calls it. If the class you provide as the generic type argument does not have a Configuration method with the expected arguments then you get an error at run time.

我同意,这是不是因为发现如我们所愿,我不知道为什么原来的开发人员实现这种方式,而不是添加其中T:IStartup 约束来代替。这不仅使没有证件更可发现,也将允许编译器在编译时进行检查。

I agree that this is not as discoverable as we would like and I am not sure why the original developers implemented it this way rather than adding a where T: IStartup constraint instead. Not only would this make it more discoverable without documentation, but would also have allowed the compiler to check this at compile time.

唯一的优势,这种方法是,OWIN开发者可以添加更多的方法,或者在未来不同签名的方法,而不会破坏现有的代码。

The only advantage to this approach is that the OWIN developers can add more methods or methods with different signatures in the future without breaking existing code.

这篇关于WebApp.Start&LT;&TSTARTUP GT;方法类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆