WebApi控制器使用类库

The*_*ave 11 c# windows-services asp.net-web-api

我正在尝试创建一个系统,允许我通过Web应用程序或通过Windows服务来托管"WebAPI"网站.为此,我希望我的所有商务逻辑都包含在一个类库中,以便我可以在我的Windows服务和我的"Web"(IIS)服务中引用它.

我目前的想法是使用HttpSelfHostServer中包含的自托管选项.对于Web端,我只想创建一个标准的webapi网站,并添加一些对我的类库的引用.

我发现如果我将控制器放在与HttpSelfHostServer相同的命名空间中,它可以正常工作,但只要控制器在外部类库中,服务器就无法再解析到我的控制/操作的路由.

我的代码:

Windows服务:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Reflection;
using System.IO;

using System.Web.Http.SelfHost;
using System.Web.Http;
using System.Web.Http.Dispatcher;

using WebApiClasses;
using WebApiClasses.Controllers;

namespace WebAPISelfHost
{
    public partial class Service1 : ServiceBase
    {
        private HttpSelfHostServer _server;
        private readonly HttpSelfHostConfiguration _config;
        public const string ServiceAddress = "http://localhost:8080";

        public Service1()
        {
            InitializeComponent();

            _config = new HttpSelfHostConfiguration(ServiceAddress);

            //AssembliesResolver assemblyResolver = new AssembliesResolver();
            //_config.Services.Replace(typeof(IAssembliesResolver), assemblyResolver);

            _config.Routes.MapHttpRoute("DefaultApi",
                "api/{controller}/{id}",
                new { id = RouteParameter.Optional });

        }

        protected override void OnStart(string[] args)
        {
            _server = new HttpSelfHostServer(_config);
            _server.OpenAsync();
        }




        protected override void OnStop()
        {
            _server.CloseAsync().Wait();
            _server.Dispose();
        }
    }

    //public class TestController : ApiController
    //{
    //    public string Get()
    //    {
    //        return "This is an internal test message.";
    //    }
    //}

    class AssembliesResolver : DefaultAssembliesResolver
    {
        public override ICollection<Assembly> GetAssemblies()
        {
            ICollection<Assembly> baseAssemblies = base.GetAssemblies();
            List<Assembly> assemblies = new List<Assembly>(baseAssemblies);

            // Add whatever additional assemblies you wish

            var controllersAssembly = Assembly.LoadFrom(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\WebApiClasses.dll");
            baseAssemblies.Add(controllersAssembly);
            return assemblies;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.Http;

namespace WebApiClasses.Controllers
{
    public class TestController : ApiController
    {
        public string Get()
        {
            return "hello from class library";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试导航到:"http:// localhost:8080/api /"时,我得到:

没有找到与请求URI"http:// localhost:8080/api /"匹配的HTTP资源.未找到与名为"Test"的控制器匹配的类型.

有什么建议?我想我应该能做到这一点.

And*_*dyD 11

看一下这篇文章http://www.strathweb.com/2012/06/using-controllers-from-an-external-assembly-in-asp-net-web-api/

在其中,他们描述了如何加载带有控制器的额外组件.