WCF Web API 配置文件到 IIS
我已经使用 WCF Web API 实现了一个 Restful 服务,我想将其发布到 IIS 中。 在开发过程中,我将该服务用作控制台应用程序,所有配置都是通过 API 进行的。 现在我尝试将服务发布为 ASP.NET 应用程序,我看到的唯一方法是以某种方式将所有配置移动到 Web 配置文件中。
这里是编码配置:
var cfg = HttpHostConfiguration.Create()
.AddMessageHandlers(typeof(AllowCrossDomainRequestHandler));
using (var host = new HttpConfigurableServiceHost(typeof(RESTfulService), cfg , new Uri("http://localhost:8081")))
{
var endpoint = ((HttpEndpoint)host.Description.Endpoints[0]); //Assuming one endpoint
endpoint.TransferMode = TransferMode.Streamed;
endpoint.MaxReceivedMessageSize = 1024 * 1024 * 10; // Allow files up to 10MB
host.Open();
Console.WriteLine("Host opened at {0} , press any key to end", host.Description.Endpoints[0].Address);
Console.ReadKey();
}
我的 web.config 应该如何反映此配置? 或者除了使用 ASP.NET 之外还有其他方法吗?
任何帮助表示赞赏。
I have implemented a restful service with WCF Web API and I want to publish it in IIS.
During developing process I was using the service as Console Application and all configuration was made through API.
Now I'm trying to publish the service as ASP.NET application and the only way I see is somehow to move all configuration into web config file.
Here the coded configuration:
var cfg = HttpHostConfiguration.Create()
.AddMessageHandlers(typeof(AllowCrossDomainRequestHandler));
using (var host = new HttpConfigurableServiceHost(typeof(RESTfulService), cfg , new Uri("http://localhost:8081")))
{
var endpoint = ((HttpEndpoint)host.Description.Endpoints[0]); //Assuming one endpoint
endpoint.TransferMode = TransferMode.Streamed;
endpoint.MaxReceivedMessageSize = 1024 * 1024 * 10; // Allow files up to 10MB
host.Open();
Console.WriteLine("Host opened at {0} , press any key to end", host.Description.Endpoints[0].Address);
Console.ReadKey();
}
How should my web.config look to reflect this configuration?
Or is there any other approach instead of using ASP.NET?
Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想保留现有配置,可以将所有配置设置内容放入一个方法中,并从
global.asax
Application_Start()
方法调用它。所有 Global.asax 方法都将在 WCF 中调用,就像在 ASP.NET 中一样。或者,您可以将服务连接到包含所有配置的自定义
ServiceHostFactory
和ServiceHost
(这是我在当前应用程序中使用的方法)。If you want to preserve your existing config, you can put all your config set up stuff into a method, and call it from
global.asax
Application_Start()
method. All the Global.asax methods will get called in WCF the same as they do for ASP.NET.Or, you can wire your services to a custom
ServiceHostFactory
andServiceHost
that has all the configuration in it (this is the approach I am using in my current app).