Tag: rest

WCF Rest客户端发送不正确的内容类型

我正在尝试使用wcf客户端向使用json的ColdFusion 9服务发送请求。 但是,请求的内容类型是针对xml的。 这是服务合同。 正如您所看到的,我们使用了json的RequestFormat。 [ServiceContract(Name = “ServiceAgreementRequestService”, Namespace = NetworkNamespaces.ServiceNamespace)] public interface IServiceAgreementRequestService { [OperationContract] [FaultContract(typeof(RequestFault))] [WebInvoke(UriTemplate = “?method=CreateUser”, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] CreateUserResponse CreateUser(CreateUserRequest request); } 我也尝试在OutGoing请求上设置Request.ContentType,这也不起作用。 using (var context = this.GetServiceClient(clientId)) { WebOperationContext.Current.OutgoingRequest.ContentType = “application/json; charset=UTF-8”; var request = new CreateUserRequest(user.Id, user.Person.FirstName, user.Person.LastName); var response = context.Channel.CreateUser(request); } 这是发送的请求 POST […]

WCF服务应用程序:该操作必须具有单个参数,其类型为Stream

可能重复: WCF Rest Webservice with stream 我正在使用C#开发WCF .NET Framework 4.0。 我用这个Visual Studio模板创建了这个WCF: 我需要发送带有两个或三个参数的图像。 这是我所有的OperationContract (我要求最后一个): [ServiceContract] public interface IRestServiceImpl { [OperationContract] [WebInvoke(Method = “GET”, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = “orders/”)] OrderContract[] allOrders(); [OperationContract] [WebInvoke(Method = “POST”, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = “filteredOrders/”)] OrderContract[] GetOrders(IdsMessage msg); [OperationContract] [WebInvoke(Method = “POST”, […]

使用JsonProperty对JSON进行Model属性绑定

我受到我方和客户之间协议的约束,使用包含破折号的json参数。 由于无法在C#中的属性名称中使用它,我需要映射到所需的属性。 我现在应该做什么: 为方便起见,下面的代码被简化。 模型 public class MyRequest { [JsonProperty(“request-number”)] public string RequestNumber { get; set; } [JsonProperty(“name”)] public string Name { get; set; } } 调节器 [HttpGet] [Route(“api/load-stuff”)] public Stuff LoadStuff([FromUri]MyRequest request) { return BackEnd.LoadStuff(request); } 从客户端调用API 使用此uri定位上述控制器: http://localhost:12345/api/load-stuff?request-number=X123&name=requestName 我的问题 如果我在BackEnd.LoadStuff行放置断点,我可以看到调用到达,但请求未正确映射。 名称包含我所期望的: requestName ,但RequestNumber为null ,因此映射不起作用。 出了什么问题?

400错误请求将xml有效负载发送到WCF REST服务

我知道有几篇post询问400错误,我相信我已经阅读了所有这些错误,但我认为我面临的问题是不同的。 这是我的WCF服务合同 [WebInvoke(UriTemplate = “/cust_key/{key}/prod_id/{id}”, Method = “POST”, BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)] Stream GetData(string key, string id, string data); 这是我用来将请求发送到我的restsvc的代码 request.RequestUri = new Uri(“http://localhost:3138/v1/cust_key/company1/prod_id/testProductID”); request.ContentType = “application/xml”; request.HttpMethod = “POST”; string xml = @”dell 400400 dollars”; byte[] message = Encoding.ASCII.GetBytes(xml); string data = Convert.ToBase64String(message); response = request.MakeWebRequest(null, data); 这给了我400个错误请求错误。 我试图将xml字符串更改为以下两个,它们也会产生400错误 […]

在RESTful WCF中混合XML和JSON,无需单独的方法

我有一个RESTful WCF服务,可以返回XML,JSON或JSONP,具体取决于参数,例如/service.svc/stuff?format=xml或service.svc/stuff?format=json&callback=myCallback 。 为此,我创建了一个自定义的Behavior,MethodEncoder和MethodEncoderFactory,它处理包装JSONP回调并根据format参数选择writer。 在我的编码器的WriteMessage()方法中,我做了类似的事情 XmlWriter writer = IsXmlRequested() ? XmlDictionaryWriter.CreateTextWriter(stream) : JsonReaderWriterFactory.CreateJsonWriter(stream) message.WriteMessage(writer); 然后,我将我的服务方法定义为只返回JSON但使用我的自定义绑定元素: [OperationContract, JSONPBehavior, WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = “stuff”) public List GetStuff(){…} 它几乎可以工作。 当我要求XML或JSON时,我得到了正确格式的东西,但XML没有像我期望的那样被序列化。 这是XML的样子: 1 如果我只是将WebMessageFormat设置为XML,我会得到这样的结果: 1 我绝对想要后者。 我猜这种情况正在发生,因为在创建Message对象时,结果被序列化为字典; 我的自定义编码器只是决定如何将该字典写入响应流。 因此它获得了正确的编码,但不完全是格式,这已经由ResponseFormat决定。 首先,是吗? 如果是这样,我该如何解决这个问题呢? 例如,我可以编写自己的WebMessageFormat吗? 或者我是否只需要为/ json / *和/ xml / *提供不同的ResponseFormat属性的单独方法(和URI模板)? 更新:在.net 4中,您可以设置一个WebOperationContext.Current.OutgoingResponse.Format属性。 我想我的问题归结为:有没有办法在.net 3.5中实现这一目标?

创建一个wcf服务后,如何判断wsdl中的restful或soap?

我创建了一个服务,然后我看到一个页面说: 您已创建了一项服务。 要测试此服务,您需要创建一个客户端并使用它来调用该服务。 您可以使用命令行中的svcutil.exe工具执行此操作,语法如下: 但是如何判断它是SOAP还是REST服务呢? 我怎么从wsdl等告诉我? 服务配置: 更新: 嗨马克, 我的配置是: 所以基本上我浏览到.svc文件,我看到了一个wsdl的链接。 但是,我如何知道SOAP或REST端点是否存在。 我是否正确配置了它? 谢谢 更新:17:49(英国时间) marc_s更新:18:22(英国时间) Pete,试试这个 – 没有元数据发布,没有 – 只是webHttpBinding – 你不应该再看到任何WSDL了……

使用RestSharp构建XML请求

我正在尝试使用RestSharp和C#使用REST API。 我正在使用的API文档提供了一个示例XML请求: EX0000000 07700900654 07700900123 SMS Hello Mr Sands. 07700900124 SMS Hello Mr Mayo. 我正在努力了解如何以他们想要的格式构建请求(多个元素称为“消息”) 我已经为RestSharp创建了这些类来进行序列化: public class messages { public string accountreference { get; set; } public string from { get; set; } public message message { get; set; } } public class message { public string to { get; set; } public […]

RESTful Web服务返回XML而不是JSON

我有这个简单的Web服务,现在它只是查看部件号是否为A123456789然后它返回一个型号。 这将由将连接到数据库以检查partno的逻辑替换,然后返回实际的型号。 但此时我只需要它返回一些虚拟JSON数据。 然而,当我使用Fiddler并查看http:// localhost:PORT / Scan / Model / A123456789的web broswer中的调用时,它返回此 Model: CVS-1679 但是当我在同一个URI的fiddler中进行GET时,我得到了 “Model: CVS-1679” 仅在textview选项卡下。 当我将ResponseFormat设置为JSON时,为什么它以XML(在Fiddler中的浏览器和文本中)而不是JSON返回? 我的代码: [WebGet(UriTemplate = “Model/{partno}”, ResponseFormat = WebMessageFormat.Json)] public string Model(string partno) { if (partno == “A123456789”) { string modelno = “CVS-1679”; return “Model: ” + modelno; } else { string modelno = “CVS-1601”; return “Model: ” […]

通过Rest c#httpClient创建jira问题

我在atlassian上看了一个答案https://answers.atlassian.com/questions/79902/using-httpclient-c-to-create-a-jira-issue-via-rest-generates-bad-request-response用户通过以下代码创建了JIRA问题。 我调整了它,但通过使用ObjectContent的自构建类问题得到错误 Http.HttpContent content = new Http.ObjectContent(data, jsonFormatter); 编译器不会接受它。 有人知道为什么吗? public string CreateJiraIssue() { string data= @”{ “”fields””: { “”project””: { “”key””: “”HELP”” }, “”summary””: “”Test Ticket””, “”description””: “”Creating of an issue using project keys and issue type names using the REST API””, “”issuetype””: { “”name””: “”Ticket”” }, “”assignee””: { “”name””: “”user”” } } }”; […]

更新到vs2017.3后,不会触发断点

我们有一个运行在Vs2017.3上的asp.net核心2.0项目(从1.x迁移)(从2017年更新)。 更新后,断点不再被击中。 Vs报告“断点当前不会被命中。源代码与原始版本不同”。 在更新和迁移之前,情况正常。 更新到2017.3之后以及迁移到asp.net core 2.0之前,可以看到此问题。 我知道解决方法:右键单击并强制断点,即使源代码不同也是如此。 我需要一个解决方案。 清洁重建无效。 多台计算机上出现此问题。