Friday, August 10, 2012

Simple Webserver Using WCF

Note: This post is still in work-in-progress, but if anyone interested to know it faster, please let me know.

I wanted to create a simple web server that I wanted to use in another architectural design (will be coming soon in another post). After some research, I found a good solution using WCF.

WCF can be hosted on many places including Windows forms, IIS, WPF...etc. In this solution, I host my simple WCF web server on a WPF window. Follwoing is the code behind of my WPF mian window that hosts the web server.


using System;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Text;
using System.Windows;
 
namespace WpfApplication1
{
    /// <summary>
    ///   Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private static readonly string baseAddress = "http://" + Environment.MachineName 
":8111/Service";
        private static readonly ServiceHost host = new ServiceHost(typeof (Service),
                                                new Uri(baseAddress));
 
        public MainWindow()
        {
            host.AddServiceEndpoint(typeof (IImageServer), new WebHttpBinding(), "")
                                         .Behaviors.Add(new WebHttpBehavior());
            host.Open();
            Console.WriteLine("Service is running");
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            InitializeComponent();
            brw.Navigate(new Uri("http://localhost:8111/Service/GetImage?width=50&height=40"));
        }
 
        private void Window_Closed(object sender, EventArgs e)
        {
            host.Close();
        }
    }
 
    [ServiceContract]
    public interface IImageServer
    {
        [WebGet]
        Stream GetImage(int width, int height);
 
        [WebInvoke]
        Stream GetPost(Stream contents);
    }
 
    // implement the service contract
    public class Service : IImageServer
    {
        public Stream GetImage(int width, int height)
        {
            // Although this method returns a jpeg, it can be
            // modified to return any data you want within the stream
 
            byte[] byteArray =
                Encoding.ASCII.GetBytes(
                    @"<html><body><form name='input' action='/Service/GetPost' method='post'>Username 
<input type='text' name='user' />
<input type='text' name='password' />
<input type='submit' value='Submit' />
</form></body></html>");
            var ms = new MemoryStream(byteArray);
 
            ms.Position = 0;
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
            return ms;
        }
 
        public Stream GetPost(Stream contents)
        {
            // Although this method returns a jpeg, it can be
            // modified to return any data you want within the stream
            string input = new StreamReader(contents).ReadToEnd();
            var x = WebOperationContext.Current.IncomingRequest;
            //var vl = x.[""];
 
            byte[] byteArray = Encoding.ASCII.GetBytes("<html><body><h1>Yeah</h1></body>
                                                          </html>");
            var ms = new MemoryStream(byteArray);
 
            ms.Position = 0;
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
            return ms;
        } 
    }
}

No comments:

Post a Comment