C# 在自托管实例中嵌入静态内容

C# 在自托管实例中嵌入静态内容,c#,nancy,C#,Nancy,我有一个自托管Nancy实例,并将内容嵌入程序集中(一些sshtml视图和一些css文件) 这是我的DefaultNancyBootTrapper的内容 public class CustomBootstrapper : DefaultNancyBootstrapper { private byte[] favicon; protected override byte[] FavIcon { get { return this.favicon ?? (t

我有一个自托管Nancy实例,并将内容嵌入程序集中(一些sshtml视图和一些css文件)

这是我的DefaultNancyBootTrapper的内容

public class CustomBootstrapper : DefaultNancyBootstrapper
{
    private byte[] favicon;

    protected override byte[] FavIcon
    {
        get { return this.favicon ?? (this.favicon = LoadFavIcon()); }
    }

    private byte[] LoadFavIcon()
    {
        using (var resourceStream = GetType().Assembly.GetManifestResourceStream("FrontEnd.Webinterface.Views.Content.Images.Icons.FavIcon.ico"))
        {
            var memoryStream = new MemoryStream();
            resourceStream.CopyTo(memoryStream);
            return memoryStream.GetBuffer();
        }
    }

    protected override NancyInternalConfiguration InternalConfiguration
    {
        get
        {
            return NancyInternalConfiguration.WithOverrides(OnConfigurationBuilder);
        }
    }

    void OnConfigurationBuilder(NancyInternalConfiguration x)
    {
        x.ViewLocationProvider = typeof(ResourceViewLocationProvider);
    }

    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        Assembly assembly = Assembly.GetExecutingAssembly();
        string[] assemblyNames = assembly.GetManifestResourceNames();

        base.ConfigureApplicationContainer(container);
        ResourceViewLocationProvider.RootNamespaces.Add(
          Assembly.GetAssembly(typeof(MainModule)), "FrontEnd.Webinterface.Views");
    }

    protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
    {
        base.ApplicationStartup(container, pipelines);
        CookieBasedSessions.Enable(pipelines);
    }

    protected override IEnumerable<ModuleRegistration> Modules
    {
        get
        {
            return

                AppDomainAssemblyTypeScanner
                    .TypesOf<INancyModule>(ScanMode.All)
                    .NotOfType<DiagnosticModule>()
                    .Select(t => new ModuleRegistration(t))
                    .ToArray();
        }
    }
}

虽然你已经自己回答了你的问题,但我想补充几句关于这个解决方案的话,这个解决方案对我来说很好

首先,这里有一些与该主题讨论相关的链接,其中包含对事物如何工作的解释:

  • 谷歌群组中Nancy的嵌入式内容-
其次,我使用了基于Patrick Steele第二个链接中的源代码的实现,而不是您在问题中提到的
EmbeddedStaticContentConventionBuilder
,这更简单:

public static class StaticResourceConventionBuilder
{
    public static Func<NancyContext, string, Response> AddDirectory(string requestedPath, Assembly assembly, string namespacePrefix)
    {
        return (context, s) =>
        {
            var path = context.Request.Path;

            if (!path.StartsWith(requestedPath))
            {
                return null;
            }

            string resourcePath;
            string name;

            var adjustedPath = path.Substring(requestedPath.Length + 1);

            if (adjustedPath.IndexOf('/') >= 0)
            {
                name = Path.GetFileName(adjustedPath);
                resourcePath = namespacePrefix + "." + adjustedPath
                    .Substring(0, adjustedPath.Length - name.Length - 1)
                    .Replace('/', '.');
            }
            else
            {
                name = adjustedPath;
                resourcePath = namespacePrefix;
            }

            return new EmbeddedFileResponse(assembly, resourcePath, name);
        };
    }
}

并将内容嵌入程序集-请解释?这些静态文件通常不会绑定到程序集中。你也能详细说明一下你的解决方案结构吗。ThanksI有一个解决方案,其中包含一些Windows窗体和Nancy webinterface作为自托管实例。我想有一个解决方案,它的工作完全开箱即用,其中包含所有*,dll,Nancy视图,图像,CSS在一个.exe文件我将分发。是的,我也嵌入了这些静态文件,比如CSS和图像。你为什么要这样?为什么所有东西都是一个exe文件很重要?我只想要:)我的应用程序应该是可移植的。我不喜欢我的.exe文件周围有很多依赖项文件。我希望我的整个项目都能在这个单一的可执行文件中开箱即用。我可以想象这不是最好的做法,但我想有这个“功能”。好吧,这是非正统的,但我想是可能的,但不知道你要怎么做。我删除了我的答案,因为它在这方面没有帮助。
public static class StaticResourceConventionBuilder
{
    public static Func<NancyContext, string, Response> AddDirectory(string requestedPath, Assembly assembly, string namespacePrefix)
    {
        return (context, s) =>
        {
            var path = context.Request.Path;

            if (!path.StartsWith(requestedPath))
            {
                return null;
            }

            string resourcePath;
            string name;

            var adjustedPath = path.Substring(requestedPath.Length + 1);

            if (adjustedPath.IndexOf('/') >= 0)
            {
                name = Path.GetFileName(adjustedPath);
                resourcePath = namespacePrefix + "." + adjustedPath
                    .Substring(0, adjustedPath.Length - name.Length - 1)
                    .Replace('/', '.');
            }
            else
            {
                name = adjustedPath;
                resourcePath = namespacePrefix;
            }

            return new EmbeddedFileResponse(assembly, resourcePath, name);
        };
    }
}
public class Bootstrapper : DefaultNancyBootstrapper
{
    protected override void ConfigureConventions(NancyConventions nancyConventions)
    {
        base.ConfigureConventions(nancyConventions);

        nancyConventions.StaticContentsConventions
            .Add(StaticResourceConventionBuilder.AddDirectory("/Scripts", GetType().Assembly, "MyAssembly.Scripts"));
    }
}