是否以编程方式配置ASP.NET托管的WCF 3.5 WebServiceHost?(即无WebConfig)



我在运行这一切时遇到了一些麻烦。

这是我的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服务,我需要通过我们的网站以编程方式托管它,主要是因为它是计划托管的少数服务中的第一个,我需要使其尽可能动态,以便于我们需要执行的多次推出。

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

这应该让你开始,我已经在博客中介绍了如何为基本的HTTP绑定WCF配置SSL,但总体思路是一样的,你只需要更改绑定、端点和契约。

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

最新更新