dicej / spin-dotnet-sdk

Experimental Spin SDK for .NET
1 stars 4 forks source link

allow custom namespaces for applications #10

Open bacongobbler opened 3 weeks ago

bacongobbler commented 3 weeks ago

Currently, all of the sample projects must be declared under the SpinHttpWorld.wit.exports.wasi.http.v0_2_0 namespace:

using Spin.Http;
using SpinHttpWorld.wit.imports.wasi.http.v0_2_0;
using System.Text;

namespace SpinHttpWorld.wit.exports.wasi.http.v0_2_0;

public class IncomingHandlerImpl : IIncomingHandler
{
    /// <summary>Handle the specified incoming HTTP request and send a response
    /// via `responseOut`.</summary>
    public static void Handle(ITypes.IncomingRequest request, ITypes.ResponseOutparam responseOut)
    {
        RequestHandler.Run(
            new RequestHandler.Response(
                200,
                new Dictionary<string, byte[]>
                {
                    { "content-type", Encoding.UTF8.GetBytes("text/plain") }
                },
                Encoding.UTF8.GetBytes("hello, world!")
            ).SetAsync(responseOut)
        );
    }
}

It'd be nice if we could change the entrypoint's namespace to match our project name. e.g. if I have a project named MyProject:

using Spin.Http;

namespace MyProject;

class IncomingHandlerImpl : IIncomingHandler
{
    public static void Handle(ITypes.IncomingRequest request, ITypes.ResponseOutparam responseOut)
    {
        RequestHandler.Run(
            new RequestHandler.Response(
                200,
                new Dictionary<string, byte[]>
                {
                    { "content-type", Encoding.UTF8.GetBytes("text/plain") }
                },
                Encoding.UTF8.GetBytes("hello, world!")
            ).SetAsync(responseOut)
        );
    }
}

If you change the namespace, you'll see an error when building the project:

/home/bacongobbler/code/github.com/dicej/spin-dotnet-sdk/samples/http-hello/.packages/fermyon.spin.sdk/0.1.0-dev/SpinHttpWorld.wit.exports.wasi.http.v0_2_0.IncomingHandlerInterop.cs(23,13): error CS0103: The name 'IncomingHandlerImpl' does not exist in the current context

This is because wasmExportHandle expects to call a type referred to as IncomingHandlerImpl in the same namespace:

https://github.com/dicej/spin-dotnet-sdk/blob/e008b0885c9d1c3aea3123428496aa05df48b57e/src/SpinHttpWorld.wit.exports.wasi.http.v0_2_0.IncomingHandlerInterop.cs#L14-L28

bacongobbler commented 3 weeks ago

One way we may be able to fix this is through reflection. We can use System.Reflection.Assembly.GetExecutingAssembly() to find the first type that implements IIncomingHandler and call its Handle method.

Rough pseudo-code:

IIncomingHandler handler = System.Reflection.Assembly.GetExecutingAssembly().GetTypes().First(myType => myType.GetInterfaces().Contains(typeof(IIncomingHandler)));
handler.Handle((resource), (resource0));
dicej commented 3 weeks ago

This issue probably belongs on the wit-bindgen repo, since that's what generates the code.

dicej commented 3 weeks ago

Also, keep in mind reflection support is limited for Native AOT builds.