Microsoft.Owin.Hosting 创建 RESTFul Web API 如何将对象发送到处理代码的控制器
Microsoft.Owin.Hosting Creating RESTFull webAPI how to send objects to the controllers that handle the code
我正在尝试为收集数据的服务创建 API。我希望这个 API 能够在 RESTful 种网络 API.
中公开这些数据
我有 API 工作,在我的主要代码中使用以下内容:
string baseUri = "http://*:8080"
try
{
webAppli = WebApp.Start<Startup>(baseUri);
}
catch (Exception ex) {
WriteLog.WriteErrorLog("THREAD REST API server : ", ex);
}
启动Class如下
public class Startup
{
// This method is required by Katana:
public void Configuration(IAppBuilder app)
{
var webApiConfiguration = ConfigureWebApi();
// Use the extension method provided by the WebApi.Owin library:
app.UseWebApi(webApiConfiguration);
}
private HttpConfiguration ConfigureWebApi()
{
var config = new HttpConfiguration();
config.Routes.MapHttpRoute(
"DefaultApi",
"api/{controller}/{id}",
new { id = RouteParameter.Optional });
return config;
}
}
我的控制器class如下:
public class TagsController : ApiController
{
// Simulate data
private static List<Tags> _Db = new List<Tags>
{
new Tags(),
new Tags(),
new Tags()
};
public IEnumerable<Tags> Get()
{
return _Db;
}
public Tags Get(int id)
{
var Tags = _Db.FirstOrDefault(c => c.TagId == id);
if (Tags == null)
{
throw new HttpResponseException(
System.Net.HttpStatusCode.NotFound);
}
return Tags;
}
}
我想不通的是将我的主代码中可用的对象传递给控制器。这些对象包含我需要回复 API 查询的所有数据的列表。这样我就不需要模拟数据了。
它可能很简单,但我尝试了很多事件命名管道,但似乎无法使其工作。你知道正确的方法吗
此致,
根据具体情况,您可以:
1.初始化一些class在控制器中检索数据(不推荐-难以测试):
public class TagsController : ApiController
{
private readonly IDataProvider dataProvider;
public TagsController()
{
this.dataProvider = new DataProvider();
}
public IEnumerable<Tags> Get()
{
return this.dataProvider.GetTags();
}
public Tags Get(int id)
{
var Tags = this.dataProvider.GetTags().FirstOrDefault(c => c.Id == id);
if (Tags == null)
{
throw new HttpResponseException(
System.Net.HttpStatusCode.NotFound);
}
return Tags;
}
}
- 或注入依赖项。您可以在此处找到关于如何实现 Unity 解析器的很好的教程:http://www.asp.net/web-api/overview/advanced/dependency-injection。修改后 TagsController class 看起来像
public class 标签控制器:ApiController
{
private readonly IDataProvider dataProvider;
public TagsController(IDataProvider dataProvider)
{
this.dataProvider = dataProvider;
}
public IEnumerable<Tags> Get()
{
return this.dataProvider.GetTags();
}
public Tags Get(int id)
{
var Tags = this.dataProvider.GetTags().FirstOrDefault(c => c.Id == id);
if (Tags == null)
{
throw new HttpResponseException(
System.Net.HttpStatusCode.NotFound);
}
return Tags;
}
}
并且 ConfigureWebApi
方法变为:
private HttpConfiguration ConfigureWebApi()
{
var config = new HttpConfiguration();
config.Routes.MapHttpRoute(
"DefaultApi",
"api/{controller}/{id}",
new { id = RouteParameter.Optional });
var container = new UnityContainer();
container.RegisterType<IDataProvider, DataProvider>(new HierarchicalLifetimeManager());
config.DependencyResolver = new UnityResolver(container);
return config;
}
这可能不是最佳答案,但这是对我有用的解决方案。
为了完成我需要的工作,我必须实施 Interpeocess 通信。我试图使用命名管道,但遇到了问题。
我使用的解决方案是使用 TCP 套接字。
我使用此处的信息创建代码:
http://csharp.net-informations.com/communications/csharp-multi-threaded-server-socket.htm
此致,
我正在尝试为收集数据的服务创建 API。我希望这个 API 能够在 RESTful 种网络 API.
中公开这些数据我有 API 工作,在我的主要代码中使用以下内容:
string baseUri = "http://*:8080"
try
{
webAppli = WebApp.Start<Startup>(baseUri);
}
catch (Exception ex) {
WriteLog.WriteErrorLog("THREAD REST API server : ", ex);
}
启动Class如下
public class Startup
{
// This method is required by Katana:
public void Configuration(IAppBuilder app)
{
var webApiConfiguration = ConfigureWebApi();
// Use the extension method provided by the WebApi.Owin library:
app.UseWebApi(webApiConfiguration);
}
private HttpConfiguration ConfigureWebApi()
{
var config = new HttpConfiguration();
config.Routes.MapHttpRoute(
"DefaultApi",
"api/{controller}/{id}",
new { id = RouteParameter.Optional });
return config;
}
}
我的控制器class如下:
public class TagsController : ApiController
{
// Simulate data
private static List<Tags> _Db = new List<Tags>
{
new Tags(),
new Tags(),
new Tags()
};
public IEnumerable<Tags> Get()
{
return _Db;
}
public Tags Get(int id)
{
var Tags = _Db.FirstOrDefault(c => c.TagId == id);
if (Tags == null)
{
throw new HttpResponseException(
System.Net.HttpStatusCode.NotFound);
}
return Tags;
}
}
我想不通的是将我的主代码中可用的对象传递给控制器。这些对象包含我需要回复 API 查询的所有数据的列表。这样我就不需要模拟数据了。
它可能很简单,但我尝试了很多事件命名管道,但似乎无法使其工作。你知道正确的方法吗
此致,
根据具体情况,您可以: 1.初始化一些class在控制器中检索数据(不推荐-难以测试):
public class TagsController : ApiController
{
private readonly IDataProvider dataProvider;
public TagsController()
{
this.dataProvider = new DataProvider();
}
public IEnumerable<Tags> Get()
{
return this.dataProvider.GetTags();
}
public Tags Get(int id)
{
var Tags = this.dataProvider.GetTags().FirstOrDefault(c => c.Id == id);
if (Tags == null)
{
throw new HttpResponseException(
System.Net.HttpStatusCode.NotFound);
}
return Tags;
}
}
- 或注入依赖项。您可以在此处找到关于如何实现 Unity 解析器的很好的教程:http://www.asp.net/web-api/overview/advanced/dependency-injection。修改后 TagsController class 看起来像
public class 标签控制器:ApiController {
private readonly IDataProvider dataProvider;
public TagsController(IDataProvider dataProvider)
{
this.dataProvider = dataProvider;
}
public IEnumerable<Tags> Get()
{
return this.dataProvider.GetTags();
}
public Tags Get(int id)
{
var Tags = this.dataProvider.GetTags().FirstOrDefault(c => c.Id == id);
if (Tags == null)
{
throw new HttpResponseException(
System.Net.HttpStatusCode.NotFound);
}
return Tags;
}
}
并且 ConfigureWebApi
方法变为:
private HttpConfiguration ConfigureWebApi()
{
var config = new HttpConfiguration();
config.Routes.MapHttpRoute(
"DefaultApi",
"api/{controller}/{id}",
new { id = RouteParameter.Optional });
var container = new UnityContainer();
container.RegisterType<IDataProvider, DataProvider>(new HierarchicalLifetimeManager());
config.DependencyResolver = new UnityResolver(container);
return config;
}
这可能不是最佳答案,但这是对我有用的解决方案。
为了完成我需要的工作,我必须实施 Interpeocess 通信。我试图使用命名管道,但遇到了问题。
我使用的解决方案是使用 TCP 套接字。 我使用此处的信息创建代码:
http://csharp.net-informations.com/communications/csharp-multi-threaded-server-socket.htm
此致,