【问题标题】:Configuring an ASP.NET hosted WCF 3.5 WebServiceHost programmatically? (i.e. No WebConfig)以编程方式配置 ASP.NET 托管的 WCF 3.5 WebServiceHost? (即没有 WebConfig)
【发布时间】:2013-12-05 21:31:10
【问题描述】:

让这一切运行起来有点麻烦。

这是我的 web.config:

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <bindings>
      <webHttpBinding>
        <binding name="requestSizeMax_1MB" maxReceivedMessageSize="1048576" transferMode="Streamed"></binding>
        <binding name="requestSizeMax_10MB" maxReceivedMessageSize="10485760" transferMode="Streamed"></binding>
        <binding name="requestSizeMax_100MB" maxReceivedMessageSize="104857600" transferMode="Streamed"></binding>
        <binding name="requestSizeMax_1000MB" maxReceivedMessageSize="1048576000" transferMode="Streamed"></binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="http">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="REST.IO.FileHandler">
        <endpoint binding="webHttpBinding" bindingConfiguration="requestSizeMax_1000MB" behaviorConfiguration="http" contract="REST.IO.IFileHandler"/>
      </service>
    </services>

这是我对程序化版本的尝试(在 Global.asax 中):

protected void Application_Start(object sender, EventArgs e) {
  //Dynamically Add Service EndPoint
    Uri baseAddress = new Uri("http://localhost:8000/");
    WebServiceHost serviceHost = new WebServiceHost(typeof(FileHandler), baseAddress);
    WebHttpBinding restBinding = new WebHttpBinding();
    restBinding.MaxReceivedMessageSize = 1048576000;
    restBinding.TransferMode = TransferMode.Streamed;
    ServiceEndpoint restService = serviceHost.AddServiceEndpoint(typeof(IFileHandler), restBinding, "Services/IO/FileHandler");
    restService.Behaviors.Add(new WebHttpBehavior());
    serviceHost.Open();
}

现在这将起作用,但前提是 WebServiceHost 在与网站不同的端口上打开。这与通过 web.config 设置不同。如何获得反映 web.config 功能的编程设置。尝试将其设置为与网站相同的端口会生成“进程无法访问文件,因为它正被另一个进程使用”错误。

我听说你可以覆盖 ServiceHostFactory,但我所有的尝试都失败了。我似乎无法让 WebServiceHost 使用它。 ServiceHost 似乎可以工作,但我使用的是以前开发的 REST 服务,我需要通过我们的网站以编程方式托管它,主要是因为它是计划托管的几个服务中的第一个,我需要做这个为我们需要执行的多个部署尽可能动态。

【问题讨论】:

    标签: asp.net wcf webservicehost


    【解决方案1】:

    我想知道您可能遇到了什么问题。通过命令式配置暴露 WCF 只是设置ServiceHostFactory,将*.svc 文件指向工厂并在工厂中创建绑定。

    这应该让你开始,我已经写了一篇关于如何为基本的 HTTP 绑定 WCF 配置 SSL 的博客,但总体思路是一样的,你只需更改绑定、端点和合同。

    http://www.wiktorzychla.com/2010/05/how-to-programmatically-configure-ssl.html

    【讨论】:

    • 感谢您的帮助。经过一些小的调整以将其转换为使用 WebServiceHostFactory / WebServiceHost 我已经启动并运行了。
    猜你喜欢
    • 2013-07-18
    • 2011-02-18
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    相关资源
    最近更新 更多