0

Here, when I am trying to pass both object and stream to wcf operation. I am getting "bad request 400" exception. If I pass only stream it is working fine with no issues and I am able to get output as stream. Any suggestions are greatly appreciated.

Client side code:

testclass tcls = new testclass();
tcls.name = "myclass";

 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(string.Format(@"http://localhost:225141/RestService.svc/getobject/tc/{0}",tcls));
 string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("bda11d91-7ade-4da1-855d-24adfe39d154"));
 req.Headers.Add("Authorization", "Basic " + svcCredentials);
 req.MaximumResponseHeadersLength = 2147483647;
 req.Method = "POST";
 req.ContentType = "application/octet-stream";

 FileStream fs = new FileStream("file1.txt", FileMode.Open, FileAccess.Read);
 MemoryStream ms = new MemoryStream();
 fs.CopyTo(ms);
 ms.Position = 0;
 byte[] dd = ms.ToArray();

 Stream strw = req.GetRequestStream();
 strw.Write(dd.ToArray(), 0, dd.Length);
 strw.Close();

 // here i am getting "bad request error"
 using (WebResponse svcResponse = (HttpWebResponse)req.GetResponse())
 {        
    MemoryStream msm = new MemoryStream();
    svcResponse.GetResponseStream().CopyTo(msm);
    msm.Position = 0;
    byte[] data = msm.ToArray();
 }
...

Service side code:

//IRestService.cs

[ServiceContract]    
public interface IRestService
{                

   [OperationContract]          
   [WebInvoke(UriTemplate="getobject/tc/{tc}",Method="POST",
    BodyStyle=WebMessageBodyStyle.Wrapped,ResponseFormat=WebMessageFormat.Json)]
    Stream getobjectl(testclass obj,Stream tc);
}

[DataContract]
[KnownType(typeof(testclass))]
public class testclass
{
    [DataMember]
    public string name { get; set; }        
}

//RestService.cs
public Stream getobject(testclass tc, Stream st)
{          
  //code;
}
sainath sagar
  • 499
  • 2
  • 10
  • 28
  • If you want to pass and object and a file stream to your service wrap both of them into another single class (simple POCO) and then pass it. I think there is a limitation that when stream is one parameter then the other parameter can only be a value from the URL and cannot be passed as part of the message body i.e. if you replace your object to be string and its value is from the URI then it would work – Rajesh Dec 17 '13 at 14:07
  • Even with single class type parameter also this function is not working fine.However it is working fine with only stream parameter. – sainath sagar Dec 18 '13 at 06:31
  • passing string and stream parameter is also working with no issues. I am worried about what if i want to send more parameters which may include class objects, strings and streams to service. Will rest wcf support this or not? – sainath sagar Dec 18 '13 at 07:12
  • That is quite strange that if you wrap the class object and stream into another class object and use this to pass to the service doesnt work. Can you post the code that you have tried. Make sure that you are passing a memory stream in the wrapped class. – Rajesh Dec 18 '13 at 10:56
  • Here is the my another question link, http://stackoverflow.com/questions/20651645/passing-complex-object-to-rest-wcf. This is also not working. Always giving me bad request error. – sainath sagar Dec 18 '13 at 11:19

0 Answers0