如何获得扬鞭自插件托管servicestack内工作 [英] How to get Swagger Plugin working within self hosted servicestack

查看:501
本文介绍了如何获得扬鞭自插件托管servicestack内工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经重新要求与希望运行的代码本身提供在GitHub和一个下拉框下载链接的人的例子这个问题:的不工作的托管服务ServiceStack





自扬鞭

我有我的servicestack JSON服务我的网站解决方案中运行时,'/ API /路径下,但现在我想打出的服务栈部分,并运行它作为一个自托管窗口服务。我的问题是,我和其他开发人员可以找到扬鞭插件用于测试目的非常有用,但现在,它的自我托管它出现的HttpHandler的是也仅限于只处理服务航线,以及纯HTML无法正常工作。



我该如何解决这个问题。



网址:<?code>的http://本地主机:8081 / Swagger- UI / index.html中



响应:

 处理程序的请求未找到:

Request.HttpMethod:GET
Request.HttpMethod:GET
Request的PathInfo:/Swagger-UI/Index.html
的Request.QueryString:
Request.RawUrl:/Swagger-UI/Index.html

安装的NuGet包:

  ServiceStack 
ServiceStack.Razor

更新@marfarma



我的app.config文件有它里面没有相关... ServiceStack

 <?XML版本=1.0编码=UTF-8>?; 
<结构>
<&启动GT;
< supportedRuntime版本=V4.0SKU =.net框架,版本= V4.5/>
< /启动>
< system.serviceModel>
<&绑定GT;
<&basicHttpBinding的GT;
<绑定名称=BasicHttpBinding_service1_V2_SSL/>
<绑定名称=BasicHttpBinding_service2_V1_SSL/>
< / basicHttpBinding的>
< /绑定>
<客户端>
<! - 我连接过于定制服务 - >
< /客户>
< /system.serviceModel>
<&的appSettings GT;
<! - 定制应用程序设置 - >
< /的appSettings>
< /结构>



Program.cs的:

  AppHostHttpListenerBase APPHOST =新my.HttpApi.myApiServiceHostBase(我的服务,my.ApiService); 


串listeningURL =HTTP://本地主机:8081 /;

变种的appSettings =新ServiceStack.Configuration.AppSettings();
my.HttpApi.FakeProvider.ProductProvider.Init(的appSettings);
my.HttpApi.FakeProvider.UserProvider.Init(的appSettings);

#如果DEBUG


{
appHost.Init();
appHost.Start(listeningURL);

Console.WriteLine(新闻<&CTRL GT + C停止。);
Thread.sleep代码(Timeout.Infinite);
}
赶上(异常前)
{
Console.WriteLine(错误:{0}:{1},ex.GetType()名称,ex.Message );
抛出;
}
终于
{
appHost.Stop();
}

Console.WriteLine(WinServiceAppHost已完成);



配置方法:

 公共覆盖无效配置(Funq.Container容器)
{
Plugins.RemoveAll(X => x是CsvFormat);
Plugins.RemoveAll(X => x是HtmlFormat);

ServiceStack.Text.JsConfig.EmitCamelCaseNames = TRUE;

//注册任何依赖你的服务的使用,例如:
//container.Register<ICacheClient>(new MemoryCacheClient());

无功配置=新EndpointHostConfig {DefaultContentType = ContentType.Json,ServiceStackHandlerFactoryPath =API};


调用setConfig(配置);
Plugins.Add(新ServiceStack.Api.Swagger.SwaggerFeature());

&字典LT;类型,字符串[]> serviceRoutes =新词典<类型,字符串[]>();
serviceRoutes.Add(typeof运算(AuthService),新[] {/认证/用户});


AuthFeature authFeature =新AuthFeature(()=>新建AuthUserSession(),新IAuthProvider [] {新FakeCredentialsAuthProvider()});
authFeature.IncludeAssignRoleServices = FALSE;
authFeature.ServiceRoutes = serviceRoutes; //手动指定路线权威性

Plugins.Add(authFeature);

container.Register&所述; ICacheClient>(新MemoryCacheClient());
变种userRep =新InMemoryAuthRepository();
container.Register< IUserAuthRepository>(userRep);

Plugins.Add(新CorsFeature(allowedOrigins:*,
allowedMethods:GET,POST,OPTIONS,
// allowedHeaders:内容类型,
allowedHeaders:原产地,X-大气跟踪-ID,X-大气框架,X-缓存日期,内容类型,X-大气,交通运输,*,
allowCredentials:FALSE)) ;
}



更新2:



< 。p> 1)删除上面说的插件部分被删除HTML和CSV



2)冉在包管理器(的NuGet)以下内容:

 安装封装ServiceStack.Api.Swagger 

还没有找到处理程序



在这个问题的意见(的 ServiceStack:没有/swagger-ui/index.html )的marfarma指出我在暗示我可能不会有'招摇的用户界面HTML和JavaScript存在。



这似乎servicestack主办的自我只有'把手'指定的路线,当我问自助托管服务交付招摇HTML和JavaScript,我得到了处理程序对于申请未找到错误之上。



当我参观我的自我托管服务/资源的地址,它显示了预期的页面和数据(提示招摇插件做正确的事),可是当我加载从文件系统(而不是由我的服务服务)的招摇HTML和JavaScript,并为其提供该作品,我得到无法从服务器读取资源的URL。它可能没有适当的访问控制 - 出身设置。,这似乎是一个问题,CORS,但我对我的服务启用CORS(它似乎与扬鞭UI代码的问题),招摇的UI送我的自我托管服务发送的选项的请求(而不是GET或POST),以及选择请求失败:(


解决方案

Swagger不工作的自托管ServiceStack服务



自托管服务是从斌/调试服务文件 - >复制或总是在需要更新的副本


I've re-asked this question with examples provided on github and a drop box download link for anyone that want to run the code themselves : Swagger not working on a self hosted ServiceStack Service


I had my servicestack JSON service running inside my website solution, under the '/api/ path, but now I'd like to split out that service stack portion and have it running as a self hosted windows service. My problem is, myself and the other developers find the Swagger plugin very useful for testing purposes, but now that it's self hosted it appears the HTTPHandler is setup only for only handling the service routes, and plain HTML does not work.

How do I fix this?

URL : http://localhost:8081/Swagger-UI/Index.html

Response :

Handler for Request not found: 

Request.HttpMethod: GET
Request.HttpMethod: GET
Request.PathInfo: /Swagger-UI/Index.html
Request.QueryString: 
Request.RawUrl: /Swagger-UI/Index.html

Nuget packages installed :

ServiceStack
ServiceStack.Razor

Update for @marfarma

My app.config file has nothing ServiceStack related inside it...

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_service1_V2_SSL" />
        <binding name="BasicHttpBinding_service2_V1_SSL" />
      </basicHttpBinding>
    </bindings>
    <client>
      <!-- bespoke services I'm connecting too -->
    </client>
  </system.serviceModel>
  <appSettings>
     <!-- bespoke app settings -->
  </appSettings>  
</configuration>

Program.cs :

           AppHostHttpListenerBase appHost = new my.HttpApi.myApiServiceHostBase("My Service", "my.ApiService");   


           string listeningURL = "http://localhost:8081/";

            var appSettings = new ServiceStack.Configuration.AppSettings();
            my.HttpApi.FakeProvider.ProductProvider.Init(appSettings);
            my.HttpApi.FakeProvider.UserProvider.Init(appSettings);

#if DEBUG

            try
            {
                appHost.Init();
                appHost.Start(listeningURL);

                Console.WriteLine("Press <CTRL>+C to stop.");
                Thread.Sleep(Timeout.Infinite);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: {0}: {1}", ex.GetType().Name, ex.Message);
                throw;
            }
            finally
            {
                appHost.Stop();
            }

            Console.WriteLine("WinServiceAppHost has finished");

configure method :

public override void Configure(Funq.Container container)
        {
            Plugins.RemoveAll(x => x is CsvFormat);
            Plugins.RemoveAll(x => x is HtmlFormat);

            ServiceStack.Text.JsConfig.EmitCamelCaseNames = true;

            //register any dependencies your services use, e.g:
            //container.Register<ICacheClient>(new MemoryCacheClient());

            var config = new EndpointHostConfig { DefaultContentType = ContentType.Json, ServiceStackHandlerFactoryPath = "api" };


            SetConfig(config);
            Plugins.Add(new ServiceStack.Api.Swagger.SwaggerFeature());

            Dictionary<Type, string[]> serviceRoutes = new Dictionary<Type, string[]>();
            serviceRoutes.Add(typeof(AuthService), new[] { "/auth/user" });


            AuthFeature authFeature = new AuthFeature(() => new AuthUserSession(), new IAuthProvider[] { new FakeCredentialsAuthProvider() });
            authFeature.IncludeAssignRoleServices = false;
            authFeature.ServiceRoutes = serviceRoutes;                      //specify manual auth routes        

            Plugins.Add(authFeature);

            container.Register<ICacheClient>(new MemoryCacheClient());
            var userRep = new InMemoryAuthRepository();
            container.Register<IUserAuthRepository>(userRep);

            Plugins.Add(new CorsFeature(allowedOrigins: "*",
                        allowedMethods: "GET, POST, OPTIONS",
                        //allowedHeaders: "Content-Type",
                        allowedHeaders : "Origin, X-Atmosphere-tracking-id, X-Atmosphere-Framework, X-Cache-Date, Content-Type, X-Atmosphere-Transport,  *",
                        allowCredentials: false));
}

Update 2 :

1.) Removed the plugins sections above that is removing html and csv

2.) Ran the following in the package manager (nuget) :

Install-Package ServiceStack.Api.Swagger

still no handler found.

The advice in this question ( ServiceStack: No /swagger-ui/index.html ) that marfarma pointed me at suggests I may not have the 'swagger-ui' html and javascript present. I do.

It appears the self hosted servicestack only 'handles' the specified routes, and when I ask the self hosted service to deliver the swagger html and javascript, I get the "handler for request not found" error above.

When I visit the /resources address in my self hosted service, it shows the expected page and data (suggesting the swagger plugin is doing its thing properly), but when I load up the swagger html and javascript from the file system (not 'served' by my service), and provide it the resource url that works, I get "Can't read from server. It may not have the appropriate access-control-origin settings.", which appears to be a CORS issue, but I've enabled cors on my service (it appears an issue with the Swagger UI code), the swagger-ui send my self hosted service a request sent as 'OPTIONS' (instead of GET or POST), and the options request fails :(

解决方案

as answered in Swagger not working on a self hosted ServiceStack Service:

self hosted services are serving files from bin/debug -> copy always or copy if newer is needed.

这篇关于如何获得扬鞭自插件托管servicestack内工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆