tomkuijsten / restup

Webserver for universal windows platform (UWP) apps
MIT License
114 stars 48 forks source link

Trying to build One Pager Sample but get build errors #113

Closed bbSkygazer closed 7 years ago

bbSkygazer commented 7 years ago

Hi,

I am new to REST and amy trying to use the One pager sample to get started. When I try and Build in VS2015Community I get build errors. Can someone point me in the right direction.

Error : Type 'myRestController.ParameterController.DataReceived' is a nested type.

Error : Method 'myRestController.ParameterController.GetWithSimpleParameters(System.Int32, System.String)' has a parameter of type 'Restup.Webserver.Models.Schemas.GetResponse' in its signature. Although this type is not a valid Windows Runtime type, it implements interfaces that are valid Windows Runtime types. Consider changing the method signature to use one of the following types instead: 'Restup.Webserver.Models.Contracts.IGetResponse, Restup.Webserver.Models.Contracts.IContentRestResponse, Restup.Webserver.Models.Contracts.IRestResponse'.

Error: A public type has a namespace ('test') that shares no common prefix with other namespaces ('myRestController'). All types within a Windows Metadata file must exist in a sub namespace of the namespace that is implied by the file name.

Code from my StartupTask.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using Windows.ApplicationModel.Background;

using Restup.Webserver.Attributes;
using Restup.Webserver.Models.Schemas;
using Restup.Webserver.Http;
using Restup.Webserver.Rest;
using myRestController;

// The Background Application template is documented at http://go.microsoft.com/fwlink/?LinkID=533884&clcid=0x409

namespace test
{
    public sealed class StartupTask : IBackgroundTask
    {

        private HttpServer _httpServer;
        private BackgroundTaskDeferral _deferral;

        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            // 
            // TODO: Insert code to perform background work
            //
            // If you start any asynchronous methods here, prevent the task
            // from closing prematurely by using BackgroundTaskDeferral as
            // described in http://aka.ms/backgroundtaskdeferral
            //
            _deferral = taskInstance.GetDeferral();
            var restRouteHandler = new RestRouteHandler();
            restRouteHandler.RegisterController<ParameterController>();

            var configuration = new HttpServerConfiguration()
              .ListenOnPort(8800)
              .RegisterRoute("api", restRouteHandler)
              .EnableCors();

            var httpServer = new HttpServer(configuration);
            _httpServer = httpServer;

            await httpServer.StartServerAsync();

        }

    }
}
namespace myRestController
{

    [RestController(InstanceCreationType.Singleton)]
    public class ParameterController
    {
        public class DataReceived
        {
            public int ID { get; set; }
            public string PropName { get; set; }
        }

        [UriFormat("/simpleparameter/{id}/property/{propName}")]
        public GetResponse GetWithSimpleParameters(int id, string propName)
        {
            return new GetResponse(
              GetResponse.ResponseStatus.OK,
              new DataReceived() { ID = id, PropName = propName });
        }
    }
}

StartupTask - Copy.cs.txt

Any help would be much appreciated.

Jark commented 7 years ago

Hey @bbSkygazer,

It looks like our "one pager" is not suitable for using it directly in a startup task / xaml project. Certain UWP project types have limitations about what kind of behaviour they support.

What you can do to make this work is to move the DataReceived class out of the ParameterController class and make the GetWithSimpleParameters return IGetResponse.

I've updated the one pager (https://github.com/tomkuijsten/restup/wiki/One-pager) so hopefully it should work now.

Let me know if it works for you or not.

Cheers,

Jark

Jark commented 7 years ago

Closing this as there has been no response for a month.

fjames commented 7 years ago

I know this has been closed but I'm having the same issue. I'm using the One-pager example you provided. Building shows the following errors.

StartupTask.DataReceived is a nested type. Nested types cannot be exported to the Windows Runtime. StartupTask.ParameterController is a nested type. Nested types cannot be exported to the Windows Runtime.

Am I missing something?

Thanks

namespace IoTWebServer
{
    public sealed class StartupTask : IBackgroundTask
    {
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            var restRouteHandler = new RestRouteHandler();
            restRouteHandler.RegisterController<ParameterController>();

            var configuration = new HttpServerConfiguration()
              .ListenOnPort(8800)
              .RegisterRoute("api", restRouteHandler)
              .EnableCors();

            var httpServer = new HttpServer(configuration);
            await httpServer.StartServerAsync();

            // now make sure the app won't stop after this (eg use a BackgroundTaskDeferral)
            var _Deferral = taskInstance.GetDeferral();
        }

        public class DataReceived
        {
            public int ID { get; set; }
            public string PropName { get; set; }
        }

        [RestController(InstanceCreationType.Singleton)]
        public class ParameterController
        {
            [UriFormat("/simpleparameter/{id}/property/{propName}")]
            public IGetResponse GetWithSimpleParameters(int id, string propName)
            {
                return new GetResponse(
                  GetResponse.ResponseStatus.OK,
                  new DataReceived() { ID = id, PropName = propName });
            }
        }
    }
}
Jark commented 7 years ago

Hi @fjames,

Please see my previous reply for the error.

Please have another look at https://github.com/tomkuijsten/restup/wiki/One-pager. Both the ParameterController and the DataReceived class will have to be placed outside the StartupTask class scope. You might also have to make them sealed to satisfy the uwp project requirements.

You will also need to turn the httpServer and _Deferral variable into a field in the StartupTask class to ensure the garbage collector doesn't clear up the reference and the server stops working.

Reopening this issue so that I get a reminder to update the wiki with a better example or, add a NuGet package that auto sets up the project once I have access to a computer again.

Hope this helps,

Jark

fjames commented 7 years ago

Thank you for getting back so quick. The recommended changes worked with one exception. I had to make the following change to the Uri format.

[UriFormat("/simpleparameter/{id}/property/{propName}")] [UriFormat("/{id}/{propName}")]

Response from server: {"ID":1,"PropName":"Test"} 👍 Thanks again.

ta-vroom commented 7 years ago

Having a little trouble getting DataReceived. What's the proper way to call it and print the data in debug? Sorry, still learning.

Also, posting here instead of the issue I opened because I thought others trying to work out of OnePager might need the help.

tomkuijsten commented 7 years ago

Could you explain what you are trying to achieve? The DataReceived in the sample is used to send a response object to the caller. This will be serialized and received as json (or xml) by the client.

ta-vroom commented 7 years ago

Mainly trying to understand how everything works and feeling a little stupid.

By caller you mean the client or browser right? So the client (browser) gets a response (DataReceived) from the server?

For some reason I was assuming GET meant in the opposite direction, but I am still trying to wrap my head around everything.

Update: So if I wanted to get data from the server, what should my curl command look like?

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://10.0.0.10:8800

I'm getting error 400, probably because I'm confused as to what the uri should look like.

Final edit: Looking at the sample helped.