Paul Stanley
Paul Stanley

Reputation: 2161

Nancy fx how to use in Windows Forms

Here I have a simple WinForm app which has a NancyFx service all working fine: I use a Person object which implements the IPerson interface. The nancyModule has a ctor with a parameter of IPerson and in the post route of the nancyModule I use the this.Bind(); If I want to display the person on the form how do I do it?

using System;
using System.Windows.Forms;
using Microsoft.Owin.Hosting;
using Nancy;
using Nancy.ModelBinding;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private IDisposable dispose;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           string uri = "http://localhost:8080/";
           dispose = WebApp.Start<Startup1>(uri);
        }
    }

    public interface IPerson
    {
        String Name { get; set; }
    }
    public class Person : IPerson
    {
        public String Name { get; set; }
    }
    public class nancyModule : NancyModule
    {
        public nancyModule(IPerson person)
        {
            Post["/data"] = _ =>
            {
                person = this.Bind<Person>();
                //HOW DO I DISPLAY THE person  ON THE FORM UI
               return HttpStatusCode.OK;
           };
        }
   }
}

Upvotes: 0

Views: 1073

Answers (1)

Dr Schizo
Dr Schizo

Reputation: 4366

If you want to display the person data on the form then you need to call your REST API from your Win Forms application. Grab the response and output the results. Simply put, this is how you can achieve this.

I haven't used async and await keywords which ideally you would but for brevity I have omitted this.

Firstly, I removed the dependency of IPerson from your module as this isn't a dependency as such but an output from your POST. With that minor adjustment, it looks like this:

If you still feel strongly about IPerson being a dependency then simply leave it and the code will still work as expected.

public class PersonModule : NancyModule
{
    public PersonModule()
    {
        this.Post["/data"] = args => this.AddPerson();
    }

    private Negotiator AddPerson()
    {
        var person = this.Bind<Person>();

        return this.Negotiate
            .WithStatusCode(HttpStatusCode.Created)
            .WithContentType("application/json")
            .WithModel(person);
    }
}

Now from your Win Forms application simply call the API via the HttpClient, like this:

using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    var person = new Person { Name = "Foo Bar" };
    var serializer = new JavaScriptSerializer();
    var response = client.PostAsync(
        "http://localhost:8080/data",
        new StringContent(serializer.Serialize(person), Encoding.UTF8, "application/json")).Result;
    var result = new JavaScriptSerializer().Deserialize<Person>(response.Content.ReadAsStringAsync().Result);

    TextBox1.Text = result.Forename;
}

Purest's out there will mention 3rd party libraries such as Json.NET and Service Stack which allows for easier serialization and deserialization but again for simplicity in this example I am using out of the box features.

Upvotes: 2

Related Questions