2015-12-30 19 views
6

Ho un progetto web api 2. È configurato per essere auto-ospitato usando owin. Non ha alcun file global.asax. Ho bisogno di avere pagine di aiuto per l'API web e ho usato swaschbuckle per questo. Ma rooturl/swagger/docs non sta dando alcun output. Ho seguito le istruzioni qui 'https://github.com/domaindrivendev/Swashbuckle/issues/196', ma non funziona ancora. Di seguito è il mio codice di configurazioneSwagger Swashbuckle non funziona

 
public void Configuration(IAppBuilder app) 
     { 
      // Configure DI 
      container = BuildDI(); 

      // Create the configuration. OWIN Should create a new httpconfiguration. 
      // GlobalConfiguration can't be used. 
      HttpConfiguration = new HttpConfiguration(); 

      HttpConfiguration.Formatters.XmlFormatter.UseXmlSerializer = true; 
      HttpConfiguration.MapHttpAttributeRoutes(); 
      HttpConfiguration.DependencyResolver = new AutofacWebApiDependencyResolver(container); 
      // Set ServicePointManager properties. 
      ServicePointManager.ServerCertificateValidationCallback = ((sender, cert, chain, sslPolicyErrors) => true); 
      // 10 concurrent connections can be made on the service point. 
      ServicePointManager.DefaultConnectionLimit = 10; 
      // After the idle time expires, the ServicePoint object is eligible for 
      // garbage collection and cannot be used by the ServicePointManager object. 
      ServicePointManager.MaxServicePointIdleTime = 30000; // 30 Seconds. 

      app.UseSerilogRequestContext("RequestId"); 
      // Middleware is injected form DI. 
      app.UseAutofacMiddleware(container); 

      app.UseAutofacWebApi(HttpConfiguration); 

      //Enable swashbuckle 
      SwaggerConfig.Register(HttpConfiguration); 

      // Webapi middleware. Do it at the end. 
      app.UseWebApi(HttpConfiguration); 

      // Register callback to dispose container. 
      RegisterShutdownCallback(app, container); 
     } 

--------------------------------------------------------------- 
public class SwaggerConfig 
     { 
      public static void Register(HttpConfiguration config) 
      { 
       config.EnableSwagger(c => 
       { 
        c.RootUrl(rurl => ConfigurationManager.AppSettings["hostUrl"].ToString()); 
        c.IncludeXmlComments(GetXmlCommentsFileLocation()); 
        c.SingleApiVersion("v1", "Isone"); 
       }) 
       .EnableSwaggerUi(c => 
       { 
       }); 
      } 

      private static string GetXmlCommentsFileLocation() 
      { 
       var baseDirectory = AppDomain.CurrentDomain.BaseDirectory + "\\bin"; 
       var commentsFileName = Assembly.GetExecutingAssembly().GetName().Name + ".XML"; 
       var commentsFileLocation = Path.Combine(baseDirectory, commentsFileName); 
       return commentsFileLocation; 
      } 
     } 
-------------------------------------------------------------- 

Si prega di indicare l'errore se presente nel codice.

risposta

1

I passaggi devono essere come segue:

1) Add "Swashbuckle.Core" come dipendenza NuGet (solo il pacchetto di nucleo è richiesto con OWIN)

package.json

<packages>  
    <package id="Swashbuckle.Core" version="5.5.3" targetFramework="net462" /> 
    ... 
</packages> 

2) Dentro di te classe di avvio registro spavalderia

Startup.cs

public partial class Startup 
{ 
    /// <summary> 
    ///  Used to create an instance of the Web application 
    /// </summary> 
    /// <param name="app">Parameter supplied by OWIN implementation which our configuration is connected to</param> 
    public void Configuration(IAppBuilder app) 
    { 
     // Wire-in the authentication middleware 
     ConfigureAuth(app); 

     // In OWIN you create your own HttpConfiguration rather than re-using the GlobalConfiguration. 
     HttpConfiguration config = new HttpConfiguration(); 

     // Handle registration of Swagger API docs 
     SwaggerConfig.Register(config); 

     // Handles registration of the Web API's routes 
     WebApiConfig.Register(config); 

     // Register web api 
     app.UseWebApi(config); 
    } 

SwaggerConfig.cs

public class SwaggerConfig 
{ 
    public static void Register(HttpConfiguration config) 
    { 
     config 
      .EnableSwagger(c => 
      { 
       c.IncludeXmlComments(GetXmlCommentsPath()); 
      }) 
      .EnableSwaggerUi(c => 
      { 
       c.EnableApiKeySupport("Authorization", "header"); 
      }); 
    } 

    private static string GetXmlCommentsPath() 
    { 
     return [email protected]"{AppDomain.CurrentDomain.BaseDirectory}\bin\Example.XML"; 
    } 
} 

3) Uscita di abilitazione documentazione XML nelle proprietà di build

Build Properties

4) Swagger UI sarà servito a swagger/ui/index

enter image description here

Problemi correlati