【问题标题】:Multiple HTTPS and HTTP WCF endpoints error多个 HTTPS 和 HTTP WCF 端点错误
【发布时间】:2015-12-23 21:50:03
【问题描述】:

我正在尝试将 WCF 项目配置为同时使用 HTTPS 和 HTTP。

我已经设法让它在我的服务器上运行(HTTPS 和 HTTP)——当我远程调试时(使用 Chrome 的扩展程序“Advanced Rest Client”)可以完美运行,但我不断收到“错误 500 System.ServiceModel.ServiceActivationException”在我的 localhost 环境中。

我附上了我的代码,请注意我只尝试更改 "Behavior_Users"(只是想弄清楚),但我的所有接口都需要这个 HTTP + HTTPS 功能。

这是我的 Web.config

    <bindings>
      <webHttpBinding>
        <binding name="basicWebBinding">
          <security mode="None">
            <transport clientCredentialType="None" />
          </security>
        </binding>
        <binding name="secureWebBinding">
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" httpsHelpPageEnabled="true"/>
        </behavior>
      </serviceBehaviors>

      <endpointBehaviors>
        <behavior name="Behavior_Users">
          <webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Bare"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <protocolMapping>
      <add binding="basicHttpBinding" scheme="http"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

    <services>
      <service name="TakesApp.MainWCF.Users">
        <endpoint behaviorConfiguration="Behavior_Users" binding="webHttpBinding" bindingConfiguration="basicWebBinding" contract="TakesApp.MainWCF.IUsers">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint behaviorConfiguration="Behavior_Users" binding="webHttpBinding" bindingConfiguration="secureWebBinding" contract="TakesApp.MainWCF.IUsers">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
      </service>
    </services>
  </system.serviceModel>
</configuration>

这是我的方法:

  [OperationContract]
  [WebInvoke(Method = "*", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
  System.ServiceModel.Channels.Message Register(Stream body);

希望你们能帮助我,我尝试了 2 天的正确答案,但没有运气。

非常感谢!!!

更新:仍然没有。考虑简单地给它一个不同的配置来调试和发布。我尝试了每种配置组合。当托管在服务器上时,它可以同时使用 HTTPS 和 HTTP,但它会在我的 localhost 环境中故障转移 POST。 WCF 很烂。

【问题讨论】:

  • 你能把你的代码只减少到相关部分吗?
  • 完成,希望我没有删除任何相关部分。谢谢
  • 我也遇到了类似的问题

标签: .net wcf web-config wcf-binding


【解决方案1】:

好的,所以我发现这是一个已知的 WCF 错误,特别是在我的调试环境中 - 使用 IIS express。

我尝试使用 web.config 转换(将特定的 HTTPS 端点添加到 web.release.config)来解决它,但它也有一个严重的错误,它会阻止您更改除第一个设置之外的任何内容。

对于 2015 年标准中的简单网络外观而言,WCF 过于复杂。

无论如何,我所做的是根据环境(生产与否)以编程方式在运行时设置一个 HTTPS 端点。

我在 global.asax 中添加了一个新的服务器主机工厂:

public class ExtendedServiceHostFactory : WebServiceHostFactory
        {
            protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
            {
                //Add host
                WebServiceHost host = (WebServiceHost)base.CreateServiceHost(serviceType, baseAddresses);
                host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });

                //Add HTTP endpoint
                ServiceEndpoint httpEndpoint = host.AddServiceEndpoint(serviceType.GetInterfaces()[0], new WebHttpBinding(WebHttpSecurityMode.None), string.Empty);
                httpEndpoint.Behaviors.Add(new WebHttpBehavior());

                //Add HTTPS endpoint
                if (ToolBox.IsProduction()) //My proprietary indication for environment type, replace with yours
                {
                    ServiceEndpoint httpsEndpoint = host.AddServiceEndpoint(serviceType.GetInterfaces()[0], new WebHttpBinding(WebHttpSecurityMode.Transport), string.Empty);
                    httpsEndpoint.Behaviors.Add(new WebHttpBehavior());
                }

                return host;
            }
        }

然后我用这个扩展注册了一个服务(在 Application_Start 中):

//Routing
            RouteTable.Routes.Add(
               new ServiceRoute(
                   "Users", new ExtendedServiceHostFactory(),
                   typeof(Users)));

现在这显然有效。

【讨论】:

    猜你喜欢
    • 2011-09-19
    • 2011-02-04
    • 2011-04-18
    • 2011-12-26
    • 2011-06-15
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多