Hello,
This one is real simple, run this Silverlight4 example with the ContentType property commented out and you'll get back a response from from my service in xml. Now uncomment the property and run it and you'll get an exception similar to this one.
System.Net.ProtocolViolationException occurred
Message=A request with this method cannot have a request body.
StackTrace:
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at com.patten.silverlight.ViewModels.WebRequestLiteViewModel.<MakeCall>b__0(IAsyncResult cb)
InnerException:
What I am trying to accomplish is just pulling down some JSON formatted data from my wcf endpoint. Can this really be this hard, or is it another classic example of just overlooking something simple.
Edit: While perusing SO, I noticed similar posts, like this one Why am I getting ProtocolViolationException when trying to use HttpWebRequest?
Thank you,
Stephen
try
{
Address = "http://stephenpattenconsulting.com/Services/GetFoodDescriptionsLookup(2100)";
// Get the URI
Uri httpSite = new Uri(Address);
// Create the request object using the Browsers networking stack
// HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(httpSite);
// Create the request using the operating system's networking stack
HttpWebRequest wreq = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(httpSite);
// http://stackoverflow.com/questions/239725/c-webrequest-class-and-headers
// These headers have been set, so use the property that has been exposed to change them
// wreq.Headers[HttpRequestHeader.ContentType] = "application/json";
//wreq.ContentType = "application/json";
// Issue the async request.
// http://timheuer.com/blog/archive/2010/04/23/silverlight-authorization-header-access.aspx
wreq.BeginGetResponse((cb) =>
{
HttpWebRequest rq = cb.AsyncState as HttpWebRequest;
HttpWebResponse resp = rq.EndGetResponse(cb) as HttpWebResponse;
StreamReader rdr = new StreamReader(resp.GetResponseStream());
string result = rdr.ReadToEnd();
Jounce.Framework.JounceHelper.ExecuteOnUI(() => { Result = result; });
rdr.Close();
}, wreq);
}
catch (WebException ex)
{
Jounce.Framework.JounceHelper.ExecuteOnUI(() => { Error = ex.Message; });
}
catch (Exception ex)
{
Jounce.Framework.JounceHelper.ExecuteOnUI(() => { Error = ex.Message; });
}
EDIT: This is how the WCF 4 end point is configured, primarily 'adapted' from this link http://geekswithblogs.net/michelotti/archive/2010/08/21/restful-wcf-services-with-no-svc-file-and-no-config.aspx
[ServiceContract]
public interface IRDA
{
[OperationContract]
IList<FoodDescriptionLookup> GetFoodDescriptionsLookup(String id);
[OperationContract]
FOOD_DES GetFoodDescription(String id);
[OperationContract]
FOOD_DES InsertFoodDescription(FOOD_DES foodDescription);
[OperationContract]
FOOD_DES UpdateFoodDescription(String id, FOOD_DES foodDescription);
[OperationContract]
void DeleteFoodDescription(String id);
}
// RESTfull service
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class RDAService : IRDA
{
[WebGet(UriTemplate = "FoodDescription({id})")]
public FOOD_DES GetFoodDescription(String id)
{
...
}
[AspNetCacheProfile("GetFoodDescriptionsLookup")]
[WebGet(UriTemplate = "GetFoodDescriptionsLookup({id})")]
public IList<FoodDescriptionLookup> GetFoodDescriptionsLookup(String id)
{
return rda.GetFoodDescriptionsLookup(id); ;
}
[WebInvoke(UriTemplate = "FoodDescription", Method = "POST")]
public FOOD_DES InsertFoodDescription(FOOD_DES foodDescription)
{
...
}
[WebInvoke(UriTemplate = "FoodDescription({id})", Method = "PUT")]
public FOOD_DES UpdateFoodDescription(String id, FOOD_DES foodDescription)
{
...
}
[WebInvoke(UriTemplate = "FoodDescription({id})", Method = "DELETE")]
public void DeleteFoodDescription(String id)
{
...
}
}
And the portion of my web.config that pertains to WCF
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>