2

I have created a web server:

public  void Run(IBackgroundTaskInstance taskInstance)
{
    taskInstance.GetDeferral();
    ServerWorkers.WebServer server = new ServerWorkers.WebServer();
    ThreadPool.RunAsync(async workItem =>
    {
        server.Start();
    });       
}

internal class WebServer
{
    private const uint BufferSize = 8192;

    public void Start()
    {      
        StreamSocketListener listener = new StreamSocketListener();
        listener.BindServiceNameAsync("80");
        listener.ConnectionReceived += async (sender, args) =>
        {
            StringBuilder request = new StringBuilder();
            using (IInputStream input = args.Socket.InputStream)
            {
                byte[] data = new byte[BufferSize];
                IBuffer buffer = data.AsBuffer();
                uint dataRead = BufferSize;
                while (dataRead == BufferSize)
                {
                    await input.ReadAsync(buffer, BufferSize, InputStreamOptions.Partial);
                    request.Append(Encoding.UTF8.GetString(data, 0, data.Length));
                    dataRead = buffer.Length;
                }
            }

            using (IOutputStream output = args.Socket.OutputStream)
            {
                using (Stream response = output.AsStreamForWrite())
                {
                    byte[] bodyArray = Encoding.UTF8.GetBytes("<html><body>Hello, World!</body></html>");
                    var bodyStream = new MemoryStream(bodyArray);

                    var header = "HTTP/1.1 200 OK\r\n" +
                                $"Content-Length: {bodyStream.Length}\r\n" +
                                    "Connection: close\r\n\r\n";

                    byte[] headerArray = Encoding.UTF8.GetBytes(header);
                    await response.WriteAsync(headerArray, 0, headerArray.Length);
                    await bodyStream.CopyToAsync(response);
                    await response.FlushAsync();
                }
            }
        };
    }
}[/code]

I can now type in the ip address of the device in many browser tabs and I will always get a 'Hello World'

Now if I amend the code to include:

public  void Run(IBackgroundTaskInstance taskInstance)
{
    PrintService.Get();           
    taskInstance.GetDeferral();
    ServerWorkers.WebServer server = new ServerWorkers.WebServer();
    ThreadPool.RunAsync(async workItem =>
    {
        server.Start();
    });       
}

internal  class PrintService
{
    public static event delError evError;
    private const string route = "/api/Print";

    static wsPrint.IPrint wsPrint = new wsPrint.PrintClient();

    public static void Get()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(Shared.URL);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Shared.HeaderType));
            using (var response =  client.GetAsync(route + "?" + GeneralTags.COMPANY_REF + "=" + ApplicationObject.CompanyRef).Result)
            {
                if (response.IsSuccessStatusCode)
                {
                    ApplicationObject.PrintData = JsonConvert.DeserializeObject<Model.Print>(response.Content.ReadAsStringAsync().Result);
                }
                else
                {
                    evError(new Exception(String.Format("{0}: {1}", (int)response.StatusCode, response.ReasonPhrase)), ErrorTags.PRINT_GET);
                }
            }
        }
    }       
}

Now when I run this and enter the ip into a browser tab the debugger exits.

Any ideas?

  • Use a try catch maybe and implement a logging interface, to write to disk or something. Just looking at code there is no way to tell. It may that Newtonsoft.JSON DLL is not copied over? Or referencing wrong version, or your JSON is not formed correctly, Null reference, Object is not set, stack overflow, or any thing in that line. – Piotr Kula Nov 4 '15 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.