使用OWIN在Windows服务上托管的Web API Self,复杂对象始终为null

时间:2017-06-02 04:00:02

标签: c# asp.net api windows-services owin

我有Web API项目,它引用dll数据库实体,它包含API控制器,返回复杂对象

当我在Windows服务上托管API之前使用visual studio(浏览器)或Telerik Fiddler测试Web API时,它正常工作并按预期返回复杂对象的json。

在我为Windows服务创建另一个项目后,使用Owin

自我托管wep API

当我测试任何返回复杂对象的控制器时,它总是返回null,而如果我测试任何返回简单字符串的控制器,它工作正常,请帮助 我不知道为什么在主持Windows服务之后同样的事情不起作用

这里是我的Windows服务startup.cs文件

  public class Startup
    {
        public void Configuration(IAppBuilder appBuilder)
        {
            // Configure Web API for self-host. 
            HttpConfiguration config = new HttpConfiguration();

            var assembly = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string path = assembly.Substring(0, assembly.LastIndexOf("\\")) + "\\API.dll";
            config.Services.Replace(typeof(IAssembliesResolver), new SelfHostAssemblyResolver(path));

            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            appBuilder.UseWebApi(config);
        }
    }

这是从服务基类继承的Windows服务类

public partial class selfHosting : ServiceBase
    {
        public string baseAddress = "http://localhost:9000/";
        private IDisposable _server = null;


        public selfHosting()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            _server = WebApp.Start<Startup>(url: baseAddress);
        }

        protected override void OnStop()
        {
            if (_server != null)
            {
                _server.Dispose();
            }
            base.OnStop();
        }
    }

下面是另一个项目(API)下的WebApiConfig文件

 public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.Formatters.Remove(config.Formatters.XmlFormatter);
            config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
            config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
        }
    }

我检查localhost:9000 / api / employee

它返回此行

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<ArrayOfEmployees xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Application.Model" i:nil="true"/>

Web API项目中的控制器

  public class employeeController : ApiController
    {
        static readonly IRepository<employee> repository = new employeeRepository();

        // GET api/<controller>
        public IEnumerable<employee> Get()
        {
            return repository.GetAll();
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<controller>
        public void Post([FromBody]string value)
        {

        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }

如果get()方法返回简单的字符串,它工作正常 例如

// GET api/<controller>
            public string  Get()
            {
                return "Hello" ;
            }

0 个答案:

没有答案