Yes I looked at Fiddler, the specific WCF function is a simple GET, but the webrole service isn't supporting anything other than POST
[OperationContract]
[WebGet(UriTemplate = "/Users",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare)]
List<string> EnumUsers();
Let me try and get the traces above to see what are in them...
Thanks, Gavin
Hi,
So it turns out to be a pure WCF issue. This behavior is normal as you're using WebGet attribute for the OperationContract. This means the operation only accepts GET requests. For POST, etc. we need to use WebInvoke attribute. I have a sample
about RESTful WCF service that can help you quickly get started. Please check out below sample.
web.config
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="mywebHttpBinding"></binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="HttpEnableBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="WCF_REST.Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="WCF_REST.Service1Behavior" name="WCF_REST.Service1">
<endpoint behaviorConfiguration="HttpEnableBehavior" bindingConfiguration="mywebHttpBinding" address="" binding="webHttpBinding" contract="WCF_REST.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
IService1.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
namespace WCF_REST
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
[ServiceContract]
public interface IService1
{ //test
http://.../Service1.svc/GetData?q=aaa
[WebGet(UriTemplate="*")]
[OperationContract]
Message GetData();
//test
http://.../Service1.svc/Get/myparameter
[WebGet(UriTemplate="Get/{param}")]
[OperationContract]
string GetData2(string param);
[WebInvoke(Method="POST",UriTemplate="Post")]
[OperationContract]
string PostData();
}
}
Service1.svc.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
namespace WCF_REST
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config.
public class Service1 : IService1
{
public Message GetData()
{
WebOperationContext woc = WebOperationContext.Current;
woc.OutgoingResponse.Headers["Content-Type"] = "text/xml";
string querystring="";
if (woc.IncomingRequest.UriTemplateMatch.QueryParameters.Count > 0)
{
querystring = woc.IncomingRequest.UriTemplateMatch.QueryParameters[0];
}
Message response = Message.CreateMessage(MessageVersion.None, "*", "Simple response. Query String: " + querystring);
return response;
}
public string GetData2(string param)
{
string response = "Simple response. Param: " + param;
return response;
}
public string PostData()
{
string posteddata= OperationContext.Current.RequestContext.RequestMessage.ToString();
return "Posted at: " + DateTime.Now.ToString() + " Posted data: " + posteddata;
}
}
}
Client test code.
aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Threading;
namespace WCF_REST
{
public partial class _Default : System.Web.UI.Page
{
byte[] buffer;
protected void Page_Load(object sender, EventArgs e)
{//init buffer
//buffer contains the data send to REST service
string s = "<test>Hello World!</test>";
buffer= System.Text.Encoding.Default.GetBytes(s);
//note to change the url below to test
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create("http://localhost:1329/Service1.svc/Post");
hwr.Method = "POST";
//Important thing here. If it's not added we'll get 415
//The Content-Type header is essential when using RESTful
//services in general, but is especially important when
//making requests to RESTful services.
//If you don't have a Content-Type header in your HTTP
//request to a WCF service, you'll always get a
//"415 Missing Content Type" status code.
//You don't need the Content-Type header when making
//a GET request, since there isn't any entity body when
//making a GET request.
hwr.ContentType=@"text/xml";
//sync
using (Stream postStream = hwr.GetRequestStream())
{
postStream.Write(buffer, 0, buffer.Length);
postStream.Close();
HttpWebResponse response = (HttpWebResponse)hwr.GetResponse();
using (Stream respStream = response.GetResponseStream())
{
using (StreamReader streamRead = new StreamReader(respStream))
{
//render the result retrieved from REST service
Response.Write(streamRead.ReadToEnd());
}
}
response.Close();
}
////async
//bool flag=true;
//hwr.BeginGetRequestStream((a) =>
//{
// HttpWebRequest request = (HttpWebRequest)a.AsyncState;
// Stream postStream = request.EndGetRequestStream(a);
// postStream.Write(buffer, 0, buffer.Length);
// postStream.Close();
// request.BeginGetResponse((a1) =>
// {
// HttpWebResponse resp = (HttpWebResponse)request.EndGetResponse(a1);
// Stream streamResponse = resp.GetResponseStream();
// StreamReader streamRead = new StreamReader(streamResponse);
// string responseString = streamRead.ReadToEnd();
// //render the result retrieved from REST service
// Response.Write(responseString);
// // Close the stream object.
// streamResponse.Close();
// streamRead.Close();
// // Release the HttpWebResponse.
// resp.Close();
// flag=false;
// }, request);
//}, hwr);
////helper for asp.net application to force sync otherwise the resource may be GCed.
//while(flag){Thread.Sleep(1000);}
}
}
}
Allen Chen [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
