> This one was fun-- thanks for pushing me a bit!
Like to keep things interesting.
Almost there Scott, but the issue i have is that i am NOT using a WCF client - in a pure REST scenario any client should be able to easily call my service.
Now, using the ServiceKnownType stuff on the client clearly adds something to the request (but my Nokia mobile phone doesn't have WCF on the client). Can you get the equivalent to my code below working in your case? Note how rather than using WCF to build the Xml post request, you need to do it yourself. (I will check this out through a trace too, but if you are online it would be quicker from you
)
[TestMethod]
public void GetATest()
{
//set the data
UTF8Encoding enc = new UTF8Encoding();
string datatext = @"
<a xmlns=""http://myns.org/"">
<AValue>123</AValue>
<BValue>456</BValue>
</a>";
RestPost(new Uri("http://dev/services/api/grid.Test/default.svc"), "GetA", datatext);
}
public static string RestPost(Uri target, string method, string xmlBody)
{
return RestPost("http://myns.org/", target, method, xmlBody);
}
public static string RestPost(string uri, Uri target, string method, string xmlBody)
{
//set the data
UTF8Encoding enc = new UTF8Encoding();
string datatext = "<" + method + " xmlns=\"" + uri + "\"" + " xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" + xmlBody + "</" + method + ">";
byte[] data = enc.GetBytes(datatext);
//HTTP POST query
WebRequest request = HttpWebRequest.Create(target + "/" + method);
request.Method = "POST";
request.ContentType = "text/xml";
request.ContentLength = data.Length;
Stream datasteam = request.GetRequestStream();
datasteam.Write(data, 0, data.Length);
datasteam.Close();
WebResponse response = request.GetResponse();
StreamReader rdr = new StreamReader(response.GetResponseStream());
try
{
return rdr.ReadToEnd();
}
finally
{
rdr.Close();
}
}
My classes are defined as :
using System;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Runtime.Serialization;
namespace Grid.Web.ServiceInterfaces.REST
{
/// <summary>
/// This interface simply provides a mechanism to check the web services for the grid have
/// been correctly initialized.
/// </summary>
[ServiceContract(Namespace = "http://myns.org/")]
[ServiceKnownType(typeof(A))]
[ServiceKnownType(typeof(B))]
[ServiceKnownType(typeof(C))]
public interface ITest
{
//can only pass A (or B or C) and cast to B or C
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "GetA", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string GetA(A a);
}
[DataContract(Namespace = "http://myns.org/")]
public class C : A
{
[DataMember()]
public string CValue;
}
[DataContract(Namespace = "http://myns.org/")]
public class B : A
{
[DataMember()]
public string BValue;
}
[DataContract(Namespace = "http://myns.org/")]
[KnownType(typeof(B))]
[KnownType(typeof(C))]
public class A
{
[DataMember()]
public string AValue;
}
}