namespace SimpleConsoleWCFHost
{
[ServiceContract]
public interface SimpleInterface
{
///
/// POST, bare body message - http://localhost:8080/SimpleService/SayHi/Suresh
/// Content-Type: text/html
///
///
[OperationContract]
[WebInvoke(Method="POST", BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate="SayHi/{name}")]
void SayHi(string name);
///
/// GET, http://localhost:8080/SimpleService/ReplyHi/Suresh
/// Content-Type: text/html
///
///
///
[OperationContract]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "ReplyHi/{name}")]
string ReplyHi(string name);
///
/// GET, http://localhost:8080/SimpleService/ReplyHii?name=Suresh
/// Content-Type: text/html
///
///
///
[OperationContract]
[WebGet]
string ReplyHii(string name);
///
/// GET, http://localhost:8080/SimpleService/ReplyByJson?name=Suresh
/// Content-Type: text/json
///
///
///
[OperationContract]
[WebGet(ResponseFormat=WebMessageFormat.Json)]
SimpleData ReplyByJson(string name);
///
/// GET, http://localhost:8080/SimpleService/ReplyByXml?name=Suresh
/// Content-Type: application/xml
///
///
///
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml)]
SimpleData ReplyByXml(string name);
///
/// POST, http://localhost:8080/SimpleService/ConverseUsingJson
/// Content-Type: text/json
/// {"Body":"Comment received at 03-01-2012 10:13:55","Header":"Welcome to JSON - Suresh"}
///
///
///
[OperationContract]
[WebInvoke(UriTemplate = "ConverseUsingJson", Method = "POST",
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
SimpleData ConverseUsingJson(SimpleData data);
///
/// POST, http://localhost:8080/SimpleService/ConverseUsingXml
/// Content-Type: application/xml
///
/// http://www.britishdeveloper.co.uk/2011/01/how-to-post-rest-fiddler.html
///
///
///
[OperationContract]
[WebInvoke(UriTemplate = "ConverseUsingXml", Method = "POST",
RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
SimpleData ConverseUsingXml(SimpleData data);
}
[DataContract(Namespace = "")]
public class SimpleData
{
[DataMember]
public string Header { get; set; }
[DataMember]
public string Body { get; set; }
}
public class SimpleService : SimpleInterface
{
public void SayHi(string name)
{
Console.WriteLine("Hi " + name);
}
public string ReplyHi(string name)
{
return "Hi " + name;
}
public string ReplyHii(string name)
{
return "Hii " + name;
}
public SimpleData ReplyByJson(string name)
{
return new SimpleData
{
Header = "Welcome to JSON - " + name,
Body = "Comment received at " + DateTime.UtcNow.ToString()
};
}
public SimpleData ReplyByXml(string name)
{
return new SimpleData
{
Header = "Welcome to XML - " + name,
Body = "Comment received at " + DateTime.UtcNow.ToString()
};
}
public SimpleData ConverseUsingJson(SimpleData value)
{
value.Header = "JSON Conversation";
return value;
}
public SimpleData ConverseUsingXml(SimpleData value)
{
value.Header = "XML Conversation";
return value;
}
}
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8080/");
string serviceName = "SimpleService";
using (WebServiceHost webHost = new WebServiceHost(typeof(SimpleService), baseAddress))
{
ServiceEndpoint sp = webHost.AddServiceEndpoint(typeof(SimpleInterface), new WebHttpBinding(), serviceName);
//ServiceDebugBehavior sdb = webHost.Description.Behaviors.Find
//sdb.HttpHelpPageEnabled = false;
webHost.Open();
Console.WriteLine("Host Opened");
Console.ReadLine();
webHost.Close();
Console.WriteLine("Host Closed");
}
}
}
}
Some fiddler shots below
No comments:
Post a Comment