Tag: wcf rest

REST服务上的错误请求错误使用POST(json数据)调用方法?

嗨,我是RESTful WCF的新手,我正在尝试使用POST对webservice方法进行简单的调用,这是我的代码 Service Interface code [ServiceContract] public interface IJsonSave { [OperationContract] [WebInvoke(UriTemplate = “/SaveJason”, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = “POST”, BodyStyle = WebMessageBodyStyle.WrappedRequest)] string SaveJason(string value); } web.config文件代码 我尝试使用提琴手她是我的标题 POST http://localhost:50267/JsonSave.svc/Rest/SaveJason User-Agent: Fiddler Content-Type: application/json Data-Type: json Host: localhost:50267 申请机构: ({“value”:”asdfasd”}) 它给出了HTTP/1.1 400 Bad Request错误,当启用调试细节时,它会给出以下堆栈跟踪 The server encountered an error processing the […]

WCF Restful服务是否允许同样的方法公开为WebGet和WebInvoke?

WCF Restful服务是否允许同样的方法暴露为WebGet和WebInvoke,如方法重载? 这两种方法都可以从同一个URL访问。 对于Ex。 [ServiceContract] public interface IWeChatBOService { [WebGet(UriTemplate = “WeChatService/{username}”)] [OperationContract] string ProcessRequest(string MsgBody); [WebInvoke(Method = “POST”, UriTemplate = “WeChatService/{username}”)] [OperationContract] string ProcessRequest(string MsgBody); 是否可以使用WCF Restful Service?

如何在C#中使用WCF REST服务?

我的合同细节如下。 我正在使用Json响应和请求格式,也使用POST方法。 如何编写客户端以在c#中使用此服务。 [OperationContract()] [WebInvoke(UriTemplate = “/RESTJson_Sample1_Sample1Add”, Method = “POST”, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] int RESTJson_Sample1_Sample1Add(Int32 a, Int32 b, Int32 c);

Restful service returns – 检查System.Byte 类型的对象的start元素时出错。 遇到意外的角色’ÿ’

所以我试图将图像从客户端发送到服务。 服务定义看起来像这样(实现没什么特别的,它没有到达那里,所以排除该代码): [OperationContract] [WebInvoke(Method = “POST”, UriTemplate = “Image”)] Stream Image(byte [] Image); 调用客户端看起来像这样: public static byte[] ImageToByte(Image img) { ImageConverter converter = new ImageConverter(); return (byte[])converter.ConvertTo(img, typeof(byte[])); } string uri = “http://localhost:8000/RestfulService/Image”; Bitmap img = new Bitmap(“Random.jpg”); byte[] byteArray = ImageToByte(img); var request = WebRequest.Create(uri) as HttpWebRequest; if (request != null) { request.ContentType = […]

从WCT Restful服务中使用FileStream

我创建了一个WCF Restful服务,其代码如下: 接口: [ServiceContract] public interface ISIGService { [OperationContract] [WebInvoke(Method = “GET”, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = “GetMultimedia/{id}”)] Stream GetMultimedia(string id); } 实现此服务的代码: public Stream GetMultimedia(string id) { string filePath = MultimediaBLL.GetFilePath(int.Parse(id)); if (string.IsNullOrEmpty(filePath)) return null; try { FileStream multimediaFileStream = File.OpenRead(filePath); return multimediaFileStream; } catch (Exception ex) { Console.WriteLine(“Not able to […]

使用WCF REST在PUT和POST之间的区别

我试图实现一个REST WCF,以探索PUT和POST动词之间的区别。 我已使用该服务在某个位置上传了一个文件。 服务实现如下: [OperationContract] [WebInvoke(UriTemplate = “/UploadFile”, Method = “POST”)] void UploadFile(Stream fileContents); public void UploadFile(Stream fileContents) { byte[] buffer = new byte[32768]; MemoryStream ms = new MemoryStream(); int bytesRead, totalBytesRead = 0; do { bytesRead = fileContents.Read(buffer, 0, buffer.Length); totalBytesRead += bytesRead; ms.Write(buffer, 0, bytesRead); } while (bytesRead > 0); using (FileStream fs […]

如何向/与RESTful WCF服务传递和使用JSON参数?

我是RESTful服务的初学者。 我需要创建一个接口,客户端需要传递最多9个参数。 我宁愿将参数作为JSON对象传递。 例如,如果我的JSON是: ‘{ “age”:100, “name”:”foo”, “messages”:[“msg 1″,”msg 2″,”msg 3”], “favoriteColor” : “blue”, “petName” : “Godzilla”, “IQ” : “QuiteLow” }’ 如果我需要在下面执行下面的服务器端方法: public Person FindPerson(Peron lookUpPerson) { Person found = null; // Implementation that finds the Person and sets ‘found’ return found; } 问题(S): 我应该如何使用上面的JSON字符串从客户端进行调用? 我如何创建RESTful服务方法的签名和实现 接受这个JSON, 将它解析并反序列化为Person对象和 调用/将FindPerson方法的返回值返回给客户端?

ServiceHost仅支持类服务类型

我有一个名为WcfService2的服务(原件我知道),它有一个带有公共接口的IService.cs文件: namespace WcfService2 { [ServiceContract] public interface IService1 { [OperationContract] [WebGet(UriTemplate = “/{value}”)] string GetData(string value); } } 然后我有我的公共类Service1.svc.cs文件,它返回值的字符串,如下所示: namespace WcfService2 { public class Service1 : IService1 { public string GetData(string value) { return string.Format(“You entered: {0}”, value); } } } 我现在正尝试使用如下控制台应用程序来托管此服务: namespace Host { class Program { static void Main(string[] args) { WebHttpBinding binding […]

如何通过Id为Odata创建查询表达式

我已经创建了一个OData服务,现在我正在尝试在客户端使用此服务。 我想创建一个表达式,例如c#查询表达式中的下面的url- http://odata.org/Product-Service/Product(150) 上面的url在浏览器中工作正常,但我想在C#中为上面的url创建查询表达式。 任何帮助都会非常明显。

从WCF服务返回的JSON数据包含转义字符

我有一个工作的WCF – WPF应用程序工作,但我正在寻找一些优化。 下面是我的代码,其中WCF restful服务公开JSON数组,并且WPF UI正在接收它而没有任何问题。 WCF: public clsStatus[] GetAllStatus() { DataTable dt = new DataTable(); List lstGetAllStatus = new List(); try { dt = // My Data Table foreach (DataRow dr in dt.Rows) { dcStatus objGetAllStatus = new clsStatus(); objGetAllStatus.Id = Convert.ToInt32(dr[“Id”]); objGetAllStatus.Status = dr[“Status”].ToString(); lstGetAllStatus.Add(objGetAllStatus); } } return lstGetAllStatus.ToArray(); } 在WPF UI中: public […]