我正在设置WCF服务,并且想实现SwaggerWCF进行文件存储。
我的配置方式类似于SwaggerWCF文档。但是,当我想以http://localhost/RegistrationService/api-docs的身份访问doc时,它会将我重定向到http://localhost/RegistrationService/api-docs/index.html?url=/RegistrationService/api-docs/swagger.json。并给出404.0错误。
我在模型和方法中添加了Swagger属性。在下面,我将共享我的配置。
我的app.config
<configSections>
<section name="swaggerwcf" type="SwaggerWcf.Configuration.SwaggerWcfSection, SwaggerWcf" />
</configSections>
<swaggerwcf>
<tags>
<tag name="LowPerformance" visible="false" />
</tags>
<settings>
<setting name="InfoDescription" value="Sample Service to test SwaggerWCF" />
<setting name="InfoVersion" value="0.0.1" />
<setting name="InfoTermsOfService" value="Terms of Service" />
<setting name="InfoTitle" value="SampleService" />
<setting name="InfoContactName" value="Abel Silva" />
<setting name="InfoContactUrl" value="http://github.com/abelsilva" />
<setting name="InfoContactEmail" value="no@e.mail" />
<setting name="InfoLicenseUrl" value="https://github.com/abelsilva/SwaggerWCF/blob/master/LICENSE" />
<setting name="InfoLicenseName" value="Apache License" />
</settings>
</swaggerwcf>
全局Asax
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("api-docs", new WebServiceHostFactory(), typeof(SwaggerWcfEndpoint)));
}
为什么会出现404错误?
答案 0 :(得分:0)
您对RouteTable.Routes的配置是api-docs,应将其设置为“ RegistrationService / api-docs”。
我的Application_Start
RouteTable.Routes.Add(new ServiceRoute("RegistrationService/api-docs", new WebServiceHostFactory(), typeof(SwaggerWcfEndpoint)));
我的web.config。
<directoryBrowse enabled="true"/>
<modules runAllManagedModulesForAllRequests="true"/>
<service name="SwaggerWcf.SwaggerWcfEndpoint">
<endpoint address="" binding="webHttpBinding" contract="SwaggerWcf.ISwaggerWcfEndpoint"></endpoint>
</service>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
我的合同
[ServiceContract]
public interface IStore
{
[SwaggerWcfPath("Get books", "Retrieve all books from the store")]
[WebGet(UriTemplate = "/books?filter={filterText}", BodyStyle = WebMessageBodyStyle.Bare)]
[OperationContract]
Book[] ReadBooks(string filterText = null);
}
我的服务。
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[SwaggerWcf("/v1/rest")]
public class BookStore : IStore
{
[SwaggerWcfTag("Books")]
[SwaggerWcfResponse(HttpStatusCode.OK, "Book found, value in the response body")]
[SwaggerWcfResponse(HttpStatusCode.NoContent, "No books", true)]
public Book[] ReadBooks(string filterText = null)
{
WebOperationContext woc = WebOperationContext.Current;
if (woc == null)
return null;
if (Store.Books.Any())
{
woc.OutgoingResponse.StatusCode = HttpStatusCode.OK;
return string.IsNullOrEmpty(filterText)
? Store.Books.ToArray()
: Store.Books.Where(b => b.Author.Name.Contains(filterText) || b.Title.Contains(filterText)).ToArray();
}
woc.OutgoingResponse.StatusCode = HttpStatusCode.OK;
return Store.Books.ToArray();
}
}
答案 1 :(得分:0)
我的意思是您有一个服务DummyService,该服务的端点配置在哪里?我只看到您的SwaggerWcf.ISwaggerWcfEndpoint端点。
并且您应该根据您的休息服务来修改配置。
下面是我修改的配置。
服务接口。请注意我的SwaggerWcfPath,它是与Webget uritemplate“” / books?filter = {filtername}“”
相对应的书籍。namespace ServiceInterface
{
[ServiceContract]
public interface IStore
{
[SwaggerWcfPath("/books", "Retrieve all books from the store")]
[WebGet(UriTemplate = "/books?filter={filtername}", BodyStyle = WebMessageBodyStyle.Bare)]
// [OperationContract]
Book[] ReadBooks(string filtername=null);
}
}
MyService SwaggerWcf的参数是您的svc的名称,我的是Store.svc
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[SwaggerWcf("Store.svc")]
public class BookStore : IStore
{
[SwaggerWcfTag("Books")]
[SwaggerWcfResponse(HttpStatusCode.OK, "Book found, value in the response body")]
[SwaggerWcfResponse(HttpStatusCode.NoContent, "No books", true)]
public Book[] ReadBooks(string filtername=null)
{
WebOperationContext woc = WebOperationContext.Current;
if (woc == null)
return null;
if (Store.Books.Any())
{
woc.OutgoingResponse.StatusCode = HttpStatusCode.OK;
return string.IsNullOrEmpty(filtername)
? Store.Books.ToArray()
: Store.Books.Where(b => b.Author.Name.Contains(filtername) || b.Title.Contains(filtername)).ToArray();
}
woc.OutgoingResponse.StatusCode = HttpStatusCode.OK;
return Store.Books.ToArray();
}
}
我的web.config。 Service.BookStore是通过Store.svc(SwaggerWcf的参数)激活的服务
<service name="SwaggerWcf.SwaggerWcfEndpoint">
<endpoint address="" binding="webHttpBinding" contract="SwaggerWcf.ISwaggerWcfEndpoint"></endpoint>
</service>
<service name="Service.BookStore">
<endpoint address="" binding="webHttpBinding" contract="ServiceInterface.IStore" behaviorConfiguration="web" ></endpoint>
<endpointBehaviors>
<behavior name="web">
<webHttp automaticFormatSelectionEnabled="true"/>
</behavior>
</endpointBehaviors>
答案 2 :(得分:0)
在服务中为呼叫路径添加装饰器
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[SwaggerWcf("/api")]
“ / api”替换正确的名称