V
V
valis2016-03-16 08:19:46
.NET
valis, 2016-03-16 08:19:46

System.NullReferenceException C# WebApi?

I am implementing a simple .net console application that should generate and return responses to web api requests.
When accessed, it generates the following error:
System.Web.Http.SelfHost.HttpSelfHostServer.ProcessRequestContext
(ChannelContext channelContext, RequestContext requestContext) in System.Web.Http.SelfHost.Main
code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.SelfHost;
using System.Threading;

namespace Stub
{
    class Program
    {
        public static meteo_ptog curr_obj = new meteo_ptog();
        static  void Main(string[] args)
        {
            //Конфигурируем запускаем сервак 
            var selfHostConfiguraiton = new HttpSelfHostConfiguration("http://127.0.0.1:5555");
            selfHostConfiguraiton.Routes.MapHttpRoute(name: "DefaultApiRoute", routeTemplate: "api/{controller}", defaults: null);
            using (var server = new HttpSelfHostServer(selfHostConfiguraiton))
            {
                server.OpenAsync().Wait();
            }
            //Периодически генерим показатели 
            while (true)
            {
                curr_obj.get_params();
                curr_obj.next_time();
                Console.WriteLine("Reading params OK Iteration" + curr_obj.iterat + "time " + curr_obj.time);
                Thread.Sleep(20000);
               }
    }
}

The indicator class Meteo_ptog will be only (if necessary, I will lay out additionally)
Interface to it:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ESP_Stub
{
   public class IParam
    {
        public int time { get; set; }
        public int iterat { get; set; }
        public string param_1 { get; set; }
        public string param_2 { get; set; }
        public string param_3 { get; set; }
        public string version { get; set; }
        public string cordinates { get; set; }
    }
}

And the controller itself:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ESP_Stub
{
    public class ProductController : ApiController
    {

        public IList<IParam> GetAllProducts()
        {
            return new List<IParam>(){
                    new IParam(){time=Program.curr_obj.time,
                        iterat =Program.curr_obj.iterat,
                        param_1=Program.curr_obj.param_1,
                        param_2=Program.curr_obj.param_2,
                        param_3=Program.curr_obj.param_3,
                        version=Program.curr_obj.version,
                        cordinates=Program.curr_obj.cordinates
                  }

        };
        }

       

        // POST api/<controller>
        public void Post(string value)
        {
        }

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

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

Please help me figure it out
Ps The application should constantly take indicators, and not wait for requests via WebAPI

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Filatov, 2016-03-16
@NYMEZIDE

1. public class IParam - by name it is an interface. Rename, otherwise you will be confused yourself and others.
2.

// POST api/<controller>
// PUT api/<controller>/5
// DELETE api/<controller>/5

where is it taught??
do it right:
[HttpGet, Route("~/api/Products")
public IList<IParam> GetAll()
{
}
[HttpGet, Route("~/api/Products/{id}")
public IParam Get(int id)
{
}
[HttpPost, Route("~/api/Products")]
public void Post(string value)
{
}
[HttpPut, Route("~/api/Products/{id}")]
public void Put(int id, string value)
{
}
[HttpDelete, Route("~/api/Products/{id}")]
public void Delete(int id)
{
}

3. "The application must constantly take indicators, and not wait for WebAPI calls"
and then why do you need to cross WebApi? let there be a console application or a slezhba that writes your indicators to the log / database.
and there will be a separate WebAPI site on ISS that can give this data from the log / database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question