consume RESTful WCF service from console app C#

Hi,

I'm trying to consume a RESTful WCF service and get the response in XML format.

I've been given an endpoint as below

[OperationContract]
[WebGet(UriTemplate = "[someuritemplate]")]
MethodXResponse MethodX(string var1, string var2);

Open in new window


Below is my code in a console application to invoke the service

        static void Main(string[] args)
        {
            WebRequest req;
            HttpWebResponse resp;

			string var1 = [value];
			string var2 = [value];
            string sesreq = "[serviceurl].svc/[const1]/"
                + var1 
                + "[const2]" 
                + "?parameter="
                + var2; 
            
            req = WebRequest.Create(@sesreq);
            req.Method = "GET"; //MethodX?
            req.ContentType = @"application/xml; charset=utf-8";
            try
            {
                resp = req.GetResponse() as HttpWebResponse;
                if (resp.StatusCode == HttpStatusCode.OK)
                {
                    XmlDocument myXMLDocument = new XmlDocument();
                    XmlReader myXMLReader = new XmlTextReader(resp.GetResponseStream());
                    myXMLDocument.Load(myXMLReader);
                }
            }
            catch(Exception e)
            {
                //print error
            }
		}

Open in new window


My questions are:
1) Is my code correct? Is this the right way to consume a WCF service from a console application?

2) Given the service contract should I say
      req.Method = "GET";
or
      req.Method = "MethodX";
      
If I use MethodX the server returns "(405) Method not allowed". So if no method name is defined in the service contract is it safe to assume it has to be "GET"? If I use "GET" I get a OK HttpStatusCode but I run into other problems, which I'll post in another question.

3) Is the line
      req.ContentType = @"application/xml; charset=utf-8";
necessary? Especially if I'm using encryption for var2 which is returned as a base64 string? Will it cause any problem down the line

I would appreciate a lucid answer than a link to a lengthy article :-). thanks!
LVL 1
ol muserTechnology GeneralistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
1) Is my code correct? Is this the right way to consume a WCF service from a console application?
I believe that will work. My only concerns are the placement of your var1 and var2 in your URL string, and the fact that you are calling var2's key "parameter" in your URL. Usually your keys are named the same as the variable on your class method. I would have expected to see:

string sesreq = "[serviceurl].svc/[const1]/"
                + var1 
                + "[const2]" 
                + "?var2="    // change here
                + var2;

Open in new window


2) Given the service contract should I say
      req.Method = "GET";
Yes. Method should be an HTTP verb, not a method name of your class. Which verb you use is dictated by the definition of the RESTful method, in this case a GET (i.e. WebGet).

3) Is the line
      req.ContentType = @"application/xml; charset=utf-8";
necessary?
Only if your service requires the content to be sent as XML. It looks as though you have defined two strings to be sent in. The content type is probably not necessary, but if you encounter errors--like your class method not being invoked--then you might try including the content type.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.