Answer the question
In order to leave comments, you need to log in
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);
}
}
}
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; }
}
}
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)
{
}
} }
Answer the question
In order to leave comments, you need to log in
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
[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)
{
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question