Tag: asp.net web api

如何在Web API中维护请求的状态或队列

我有情况,我必须在Web API方法中接收请求,排队这些请求,然后将批量发送到数据库(Solr实例)。 我不确定如何维护来自多个来源的一批请求。 现在我将每个请求数据以json格式写入磁盘上的文件,稍后我将有一个Windows服务,浏览文件夹读取所有文件,更新数据库并删除这些文件。 这是我在Web API中所做的 public void Post(LogEntry value) { value.EventID = Guid.NewGuid(); value.ServerTime = DateTime.UtcNow; string json = JsonConvert.SerializeObject(value); using(StreamWriter sw = new StreamWriter(value.EventID.ToString())) { sw.Write(json); } } (这里的EventID是GUID) 这个过程看起来不正确,必须有一种维护请求队列的方法,但我不确定如何在多个请求期间维护队列。 我这样做的原因是,在solr实例中批量插入比通过SolrNet插入单个记录更快。 我希望在Web API上每秒至少获得100个请求。 我想创建一批1000个请求并每10秒更新一次solr实例。 请不要认为我需要代码,只需知道我应采用什么策略来维护请求/状态队列。

validationASP.NET Web API

我已经创建了一个新的ASP.NET Web API,并且运行良好。 我现在正处于我想要保护API的地步。 我把[Authorize]属性放在我的基本控制器上面,如果我想在ASP.NET应用程序本身内进行API调用,它就能正常工作。 但是,我想知道,对于想要进行API调用并通过授权的外部客户端,最佳做法是什么? 另外,请记住我有自定义身份validation逻辑。 客户端应该如何发送凭据? 我在什么时候处理这​​些凭证?

使用webAPI承载令牌进行SignalR认证

+我使用这个解决方案使用ASP.NET Web API 2,Owin和Identity实现基于令牌的身份validation……效果非常好。 我使用了这个其他的解决方案 ,这通过将承载令牌传递给连接字符串来实现signalR集线器授权和认证,但看起来要么是承载令牌没有,要么某处出现其他错误,这就是为什么我在这里寻求帮助。 ..这些是我的代码… QueryStringBearerAuthorizeAttribute:这是负责validation的类 using ImpAuth.Entities; using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.Owin.Security; using Microsoft.Owin.Security.OAuth; using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using System.Web; namespace ImpAuth.Providers { using System.Security.Claims; using Microsoft.AspNet.SignalR; using Microsoft.AspNet.SignalR.Hubs; using Microsoft.AspNet.SignalR.Owin; public class QueryStringBearerAuthorizeAttribute : AuthorizeAttribute { public override bool AuthorizeHubConnection(HubDescriptor hubDescriptor, IRequest request) { var token […]

mvc和webapi之间的身份validation(单独的域/应用程序)

我正在为以下场景寻找好的想法/资源/实现 MVC网站http://mywebsite.com http://myapi.com上的 Webapi REST服务 重要信息 – 请注意单独的域/应用程序.. 用户登录网站,并通过JSONP / CORS从API获取数据 显然,我不希望用户使用基本身份validation在webapi上进行身份validation。 但API也暴露于Android / IOS应用程序,所以我需要基本的身份validation 我想过从MVC站点返回一个令牌,然后在webapi站点写一个DelegatingHandler来使用该令牌进行身份validation,但我想要一些输入,或者甚至更好的解决方案 我为这个场景做了一个漂亮的图表:

如何使用动作filter和HttpResponseMessage在Web API中使用ETag

我有一个ASP.Net Web API控制器,它只返回用户列表。 public sealed class UserController : ApiController { [EnableTag] public HttpResponseMessage Get() { var userList= this.RetrieveUserList(); // This will return list of users this.responseMessage = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ObjectContent<List>(userList, new JsonMediaTypeFormatter()) }; return this.responseMessage; } } 和一个动作filter属性类EnableTag ,负责管理ETag和缓存: public class EnableTag : System.Web.Http.Filters.ActionFilterAttribute { private static ConcurrentDictionary etags = new […]

如何在WebApi OwinHost启动中使用Ninject引导程序?

我正在从IIS WebAPI迁移到OwinHost。 利用nuget软件包的最新预发布版本,我在这里成功使用了说明: https://github.com/ninject/Ninject.Web.Common/wiki/Setting-up-a-OWIN-WebApi-application 这是我的代码的存根: public void Configuration(IAppBuilder app) { var config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); app.UseNinjectMiddleware(CreateKernel); app.UseNinjectWebApi(config); } private static StandardKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Load(Assembly.GetExecutingAssembly()); RegisterServices(kernel); return kernel; } private static void RegisterServices(IKernel kernel) { … } 但是在我的代码和文档示例中,Ninject内核直到启动后才会创建。 但是,我需要在Cors和OAuth中间件注册的启动注册过程中使用Ninject DI。 在迁移到OwinHost之前,我可以这样做: public void Configuration(IAppBuilder app) { _bootstrapper = new Bootstrapper(); […]

ASP.NET Web Api作为一个解决方案中的独立项目

如果我想使用WebAPI作为服务连接到不同服务器上的多个数据库并检索我的MVC应用程序将使用的数据,那么最好的方法是什么? 我不希望ApiController与我的MVC项目在同一个项目中,所以我需要添加一个新的WebApi项目(删除所有除了控制器和模板添加的东西都有一个干净的项目)我的MVC应用程序会参考吗? 这是我用来了解WebAPI的教程/博客文章列表: ASP.NET Web API – 具有可下载示例代码的Screencast系列 http://weblogs.asp.net/jgalloway/archive/2012/03/16/asp-net-web-api-screencast-series-with-downloadable-sample-码部分1.aspx 使用HttpClient使用ASP.NET Web API服务 http://debugmode.net/2012/03/03/creating-first-http-service-using-asp-net-web-api-part1-of-many/ http://debugmode.net/2012/03/ / 07 /使用-HttpClient的-第2部分-的-许多消费-ASP网的Web-API服务 使用ASP.NET Web API和MVC4的CRUD操作 http://www.dotnetglobe.com/2012/03/crud-operation-using-aspnet-web-api-in.html http://www.dotnetglobe.com/2012/03/crud-operation-using-aspnet -Web-API in_28.html 为ASP.Net Web API oData服务创建.Net可查询客户端 http://blog.petegoo.com/index.php/2012/03/11/creating-a-net-queryable-client-for-asp-net-网上API-的OData服务/ 使用HttpClient来使用ASP.NET Web API REST服务 http://www.johnnycode.com/blog/2012/02/23/consuming-your-own-asp-net-web-api-rest-service/ 使用ASP.NET Web API的客户端支持 https://msmvps.com/blogs/theproblemsolver/archive/2012/03/13/client-side-support-with-the-asp-net-web-api.aspx 创建和使用ASP.Net Web API REST服务 – MVC4 http://www.askamoeba.com/Opensource/Opensourcedetail/144/Create-and-Consume-ASP-Net-Web-API-REST-Services-MVC4 使用MediaTypeFormatter和OData支持使用ASP.NET Web API构建和使用REST服务 http://robbincremers.me/2012/02/16/building-and-consuming-rest-services-with-asp-net-web-api-and-odata-support/ 将JSON.NET与ASP.NET Web API结合使用 http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx 在ASP.NET Web […]

在Web API中生成超媒体链接

我很想知道其他人如何处理为他们的Web API生成超媒体链接的问题? 具体来说,我正在使用ASP.NET Web API,并且在操作返回与超媒体相关的类型或返回资源本身之间徘徊,并且在管道中稍后发生超媒体内容。 也就是说,人们倾向于做以下事情: public Resource GetOrder(int id) { return new Resource() { Content = new Order(), Links = new LinkCollection() { new AddOrderLink(), new UpdateOrderLink()} } 或者更喜欢的东西 public Order GetOrder(int id) { return new Order(); } 然后在HttpOperationHandler或自定义格式化程序或其他内容中添加超媒体链接? 如果方法更像#2,你怎么知道要生成什么链接? 只是为所有Order对象生成一些标准的链接集? 在OrdersController中装饰各种操作的属性?

在WebApi或MVC控制器中使用ConfigureAwait(false)有任何危险吗?

说我有两个场景: 1)WebApi控制器 [System.Web.Http.HttpPost] [System.Web.Http.AllowAnonymous] [Route(“api/registerMobile”)] public async Task RegisterMobile(RegisterModel model) { var registerResponse = await AuthUtilities.RegisterUserAsync(model, _userService, User); if (registerResponse.Success) { var response = await _userService.GetAuthViewModelAsync(model.Username, User); return Request.CreateResponse(HttpStatusCode.OK, new ApiResponseDto() { Success = true, Data = response }); } else { return Request.CreateResponse(HttpStatusCode.OK, registerResponse); } } 2)MVC控制器 [Route(“public”)] public async Task Public() { if […]

在web api控制器中使用async / await或task(.net core)

我有一个.net核心API,它有一个控制器,用于构建要返回的聚合对象。 它创建的对象由来自对服务类的3个方法调用的数据组成。 它们彼此独立,可以彼此隔离运行。 目前我正在使用任务来提高该控制器的性能。 当前版本看起来像这样…… [HttpGet] public IActionResult myControllerAction() { var data1 = new sometype1(); var data2 = new sometype2(); var data3 = new List(); var t1 = new Task(() => { data1 = service.getdata1(); }); t1.Start(); var t2 = new Task(() => { data2 = service.getdata2(); }); t2.Start(); var t3 = new Task(() => […]