Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating WCF Restful service,I have followed this link.

The service is created without any error, but when I am calling it from C#, its giving me error method not allowed.

What I have tried:

I tried using
[WebGet(UriTemplate = "GetData/")]

and
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetData/")]


but same error in both cases
This is how I am using in C#

DataContractJsonSerializer objseria = new DataContractJsonSerializer(typeof(string));
           MemoryStream mem = new MemoryStream();
           //objseria.WriteObject(mem, stu);
           string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
           WebClient webClient = new WebClient();
           webClient.Headers["Content-type"] = "application/json";
           webClient.Encoding = Encoding.UTF8;
           webClient.UploadString("http://localhost:5025/MyService.svc/getdata/", "POST", data);


I tried changing the above code to this

WebClient proxy = new WebClient();
           byte[] abc = proxy.DownloadData((new Uri("http://localhost:5025/Accounts.svc/getdata/")));
           Stream strm = new MemoryStream(abc);
           DataContractSerializer obj = new DataContractSerializer(typeof(string));

           string result = obj.ReadObject(strm).ToString();

but I am getting below error in above code
"
There was an error deserializing the object of type System.String. The data at the root level is invalid. Line 1, position 1.
"

I am not getting any XML to check the error.
Posted
Updated 12-Jun-17 20:14pm

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900