none
I can't send byte array as parameter in REST service ? ( code attached )

    Question

  • I'm using WCF to create some REST service.
    One of the Rest Service method need to get byte array as parameter ( picture as byte array ) and return some object.
    I run this service using IIS.
    But this is not working.

    The code that i wrote :

        [ServiceContract]
        public interface IPicService
        {
            [OperationContract, WebInvoke(Method="POST", UriTemplate = "GetPicReport/{imageName}")]
            Report GetPicReport( string imageName, Stream image );

        }

        [ServiceBehavior( AddressFilterMode = AddressFilterMode.Any )]
        public class PicService: IPicService
        {
            public Report GetPicReport( string imageName, Stream image )
            {
                return new Report ();
            }
        }

    I checking this code using explorer - but i get an error about missing parameter ( the image stream ) 

    How can i test it ?
    I can't use the WCF Test Client - so i wrote simple application that create http call - and this method return error 404 ( server not found )

     

    Sunday, November 13, 2011 1:27 PM

Answers

  • Since this is a POST request, you cannot use the Internet Explorer to go to it (IE sends a GET request, the method expects a POST request). You can use a simple HTTP call, and the example below works fine - take a look to see if there is anything different from yours (in this case I'm defining the endpoint using the WebServiceHostFactory, but it could be defined using web.config as well - in this case web.config is not necessary).

    PicService.svc:

    <% @ServiceHost Service="MyNamespace.PicService" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

    PicService.svc.cs:

    namespace MyNamespace
    {
        [ServiceContract]
        public interface IPicService
        {
            [OperationContract, WebInvoke(Method="POST", UriTemplate = "GetPicReport/{imageName}")]
            Report GetPicReport( string imageName, Stream image );
        }
    
        public class PicService: IPicService
        {
            public Report GetPicReport( string imageName, Stream image )
            {
                return new Report ();
            }
        }
        [DataContract]
        public class Report
        {
            // members ommitted
        }
    }
    
    

    Client code:

            public static void Test()
            {
                string address = "http://service-name/vdir/PicService.svc/GetPicReport/myImageName";
                HttpWebRequest req = HttpWebRequest.Create(address) as HttpWebRequest;
                byte[] myImage = new byte[3000];
                new Random().NextBytes(myImage); // some image content
                req.Method = "POST";
                req.ContentType = "application/octet-stream";
                Stream reqStream = req.GetRequestStream();
                reqStream.Write(myImage, 0, myImage.Length);
                reqStream.Close();
                HttpWebResponse resp;
                try
                {
                    resp = req.GetResponse() as HttpWebResponse;
                }
                catch (WebException e)
                {
                    resp = e.Response as HttpWebResponse;
                }
    
                Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);
                foreach (var headerName in resp.Headers.AllKeys)
                {
                    Console.WriteLine("{0}: {1}", headerName, resp.Headers[headerName]);
                }
                Console.WriteLine();
                if (resp.ContentLength > 0)
                {
                    Console.WriteLine(new StreamReader(resp.GetResponseStream()).ReadToEnd());
                }
    
                resp.Close();
            }
    
    


    Carlos Figueira
    • Marked as answer by Yi-Lun Luo Friday, November 18, 2011 9:25 AM
    Sunday, November 13, 2011 4:15 PM
    Moderator