Tag: rest

将int列表传递给HttpGet请求

我的结构与此类似: [HttpGet] public HttpResponseMessage GetValuesForList(List listOfIds) { /* create model */ foreach(var id in listOfIds) model.Add(GetValueForId(id) /* create response for model */ return response; } 但是,当我对该方法执行Get请求时: {{domain}}/Controller/GetValuesForList?listOfIds=1&listOfIds=2 调试时说明listOfIds为null时出错。 在我们的控制器中,我们有许多公共HttpGet方法可以正常工作,并且当将参数更改为单个int时,它可以工作。 我已经尝试将参数类型更改为int[]和IEnumerable ,但没有更改。 但是,当将调用更改为HttpPost并将列表作为x-www-form-urlencoded值传递时,该方法可行。 是否可以将列表传递给Get方法,还是必须使用Post? 因为它实际上不是post方法(因为它返回值的JSON模型,并且没有任何内容保存到服务器)。

在ASP.Net Web API中的日志记录DelegatingHandler中读取HttpRequestMessage.Content时丢失

在Controller中的Action中尝试对象时,偶尔似乎是null。 我发现它是由ReadAsStringAsync()的SendAsync()覆盖中的ReadAsStringAsync()引起的。 问题在于内容。 当我的客户端发送一个内容正文并在记录器中读取它时,它永远不会被Controller Action Invoker读取(或者可能在JsonFormatter某个地方)。 我怀疑后来对Content.ReadAsStringAsync()调用不会抛出exception但也不会返回预期的内容体(返回一些信息表明异步读取已完成)。 但是我的问题仍然存在,因为我想在一个动作中读取一个[FromBody]参数,当DelegatingHandler赢得Content.ReadStringAsync的RaceCondition时它为null。 当JsonFormatter赢得它时,我得到了对象,但这很少见(仅在服务启动时)。 这是我的DelegatingHandler代码: public class LogHandler : DelegatingHandler { protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var apiRequest = new WebApiUsageRequest(request); WriteLog(apiRequest); request.Content.ReadAsStringAsync().ContinueWith(t => { apiRequest.Content = t.Result; WriteLog(apiRequest); }); return base.SendAsync(request, cancellationToken).ContinueWith(task => { var apiResponse = new WebApiUsageResponse(task.Result); apiResponse.Content = task.Result.Content != null ? task.Result.Content.ReadAsStringAsync().Result […]

如何在代码中设置ServiceHostingEnvironment.AspNetCompatibilityEnabled = true(不在配置中).NET / C#

我需要从RESTful WCF服务中使用-h访问HttpContext.Current。 我知道我可以通过在config中添加以下内容来实现此目的: 并在我的服务上使用以下属性: [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 这是我的问题,我需要在代码中“旋转”一个服务实例进行unit testing,因此我不能使用配置文件来指定服务bebaviours等。目前我的代码看起来如下,但尽管在网上搜索我我一直无法弄清楚如何设置ServiceHostingEnvironment类并将AspNetCompatibilityEnabled属性设置为true而不使用config,任何人都可以帮忙吗? string serviceUrl = “http://localhost:8082/MyService.svc”; _host = new ServiceHost(typeof(MyService), new Uri[] { new Uri(serviceUrl) }); ServiceEndpoint serviceEndpoint = _host.AddServiceEndpoint(typeof(IMyService), new WebHttpBinding(), string.Empty); serviceEndpoint.Behaviors.Add(new WebHttpBehavior()); // Here’s where I’m stuck, i need something like… ServiceHostingEnvironmentSection shes = new ServiceHostingEnvironmentSection(); shes.AspNetCompatibilityEnabled = true; _host.Add(shes); _host.Open(); 任何帮助都非常感谢,并提前感谢。

某些方法的WCF REST基本身份validation

我在WCF 4.0中实现了很多RESTful(GET和POST)方法。 所有这些都通过SSL工作。 一些方法的示例: [OperationContract] [WebInvoke(UriTemplate = “Login?”, Method = “POST”, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] LoginResponse Login(LoginRequest request); [OperationContract] [WebInvoke(UriTemplate = “UpdateDetails?”, Method = “POST”, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] UpdateUserDetailResponse UpdateDetails(UpdateUserDetailRequest request); [OperationContract] [WebInvoke(UriTemplate = “GetDetails?”, Method = “POST”, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, […]

如何在WCF RESTful服务上启用HTTPS?

如何使wcf在https上工作。 我想在https上使用这个wcf我搜索了许多文章我没有得到答案请帮助我对wcf概念的新手。 我想从ajax,jquery中调用它

是否有WCF Rest C#客户端生成工具?

在冒险创建一个之前,我想知道是否有人知道一个实用程序,它将采用WCF Rest服务的REST帮助页面并为C#消费创建相关的客户端。 与svcutil.exe对WCF服务的作用类似,或者wsdl.exe对Web服务执行的操作与WCF REST服务的作用类似 亲切的问候, 安德鲁 编辑更多细节: 请看这个链接: http : //msdn.microsoft.com/en-us/library/dd203052.aspx 在使用WCF Rest Starter Kit Preview 2的restful服务中,它们提供将被序列化的类型。 但我的目的是能够在描述模式的帮助页面中创建客户端。 然后可以为C#,JavaScript,ActionScript等创建客户端。剪切地作为宁静服务的强类型版本,而不是要求或必要性。 这是一个程序或实用程序,我想知道存在这样做

WCF 4.0 – 使用REST服务模板返回JSON WebFaultException

我正在使用WCF REST服务模板40(CS)。 我正在抛出WebFaultExceptions: throw new WebFaultException(“Error Message”, HttpStatusCode.BadRequest); 但是,当我使用我的客户端测试时,所有内容都以Http状态代码500返回,响应为XML。 我可以在XML响应中看到错误消息。 当我正确拨打电话时,我得到200响应并且响应是JSON,这是正确的,因为我的配置和ServiceContract的设置方式。 我可以将错误请求的HTTP状态代码设置为400的唯一方法是: WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest; 我仍然无法获得以JSON身份返回的exception。 编辑添加签名以获取更多信息: [OperationContract] [WebInvoke(Method = “POST”, BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, UriTemplate = “myendpoint”)] 有一个简单的方法来实现这一目标吗?

WCF服务合同可以有一个可以为空的输入参数吗?

我的合同定义如下: [OperationContract] [WebGet(UriTemplate = “/GetX?myStr={myStr}&myX={myX}”, BodyStyle = WebMessageBodyStyle.Wrapped)] string GetX(string myStr, int? myX); 我得到一个例外:[InvalidOperationException:合同’IMyGet’中的’GetX’操作有一个名为’myX’的查询变量,类型为’System.Nullable 1[System.Int32]’, but type ‘System.Nullable 1 [System.Int32 ”不能通过’QueryStringConverter’转换。 UriTemplate查询值的变量必须具有可由“QueryStringConverter”转换的类型。 找不到任何有关此错误的信息,除了以下链接: http : //blog.rolpdog.com/2007/07/webget-and-webinvoke-rock.html这有点旧 ,而不是解决方案。 任何想法除了摆脱可以为空的参数做什么? 谢谢。

如何将SAML XML标记字符串转换为SecurityToken或ClaimsPrincipal实例?

我的背景: .Net RESTful Web服务 客户端(混合平台,技术,libfunction)已获得SAML令牌 尝试在REST服务中接受用于身份validation/授权的令牌 在HTTP授权/ X-Authorization标头中 作为查询参数 稍后还会支持SWT,但需要获得SAML令牌 细节: 我在字符串中有一个SAML令牌: ..etc… 在HttpModule中,我想将其转换为ClaimsPrincipal,以便我的服务可以将通常的Thread.CurrentPrincipal作为IClaimsPrincipal。 我找到了一些诱人的网页/博客/等……看起来很有帮助: Cibrax的想法是在HTTP Authorization标头中传递令牌 Dominick Baier在SWT上有类似的东西,提到SAML很容易做同样的事情 我实际上试图将SAML令牌转换为ClaimsPrincipal(通过SecurityToken中间步骤或直接……快乐)。 来自Cibrax的想法的示例代码使用以下内容进行关键validation和反序列化步骤: SecurityTokenSerializer securityTokenSerializer = new SecurityTokenSerializerAdapter( FederatedAuthentication.SecurityTokenHandlers, MessageSecurityVersion.Default.SecurityVersion, false, new SamlSerializer(), null, null); SecurityToken theToken = WSFederationAuthenticationModule.GetSecurityToken( theSamlTokenInStringForm, securityTokenSerializer); 我遇到的问题是,WIF的RTM版本没有公开GetSecurityToken的这个重载…它只暴露: WSFederationAuthenticationModule fam = new WSFederationAuthenticationModule(); SecurityToken theToken = fam.GetSecurityToken(HttpRequest theRequest); SecurityToken theToken = fam.GetSecurityToken(SignInResponseMessage message); […]

HttpClient – 处理聚合exception

嗨我正在使用类似这样的HttpClient: public static Task AsyncStringRequest(string url, string contentType) { try { var client = new HttpClient(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(contentType)); return client.GetStringAsync(url).ContinueWith(task => { return task.Result; }); } catch (AggregateException ex) { throw ex; } catch (WebException ex) { throw ex; } catch (Exception ex) { throw ex; } } 但我在处理exception时遇到困难。 我添加了额外的catch块只是为了尝试并逐步抛出,但没有任何断点被捕获到任何catch块中。 我意识到使用Task,exception可能发生在与调用者不同的线程上,因此exception包含在聚合容器中,但我不确定处理这些exception的最佳方法是什么。 例如,我向Web服务发出请求,并在请求中指定了特定的无效参数,并抛出exception。 我希望我能够捕获聚合exception,并查看innerexceptions以找出请求失败的原因并返回友好消息。 所以我的问题是,捕获这些聚合exception并处理它们的最佳方法是什么?