首页 > 解决方案 > 将带有 WCF 的 WebForms 部署到 IIS 8,不会启动 WCF

问题描述

我有一个带有 WebForms(称为 EDWMS)和 WCF 项目(称为 EDWMS.SYNC)的 VisualStudio 解决方案。通过使用 Discover 功能将 WCF 作为服务引用添加到 WebForms 项目。当以“发布”模式在本地运行它时,WCF 会从应用程序中正确调用并且工作正常(尽管我需要以管理员身份运行 VS 才能使其工作)。但是,当通过 Web 部署包和 FTP 发布将 webforms 项目发布到 IIS 8 时,WCF 似乎没有发布。

我尝试在 IIS 中将 WCF 创建为单独的站点,然后在 VS 中将 WCF 项目设置为“启动项目”并将其发布到新的站点插槽,还将其端口添加到站点绑定。在这种情况下,我可以浏览 .svc 文件并查看它是否已设置您已经创建了一个服务。要测试此服务,您将需要... 但是当我从 WebForms 应用程序调用它时,它仍然无法响应此错误:

在 http://localhost:8733/Design_Time_Addresses/ED_WMS.SYNC/Sync/ 上没有可以接受消息的端点侦听。这通常是由不正确的地址或 SOAP 操作引起的。
远程服务器返回错误:(404) Not Found。

这是来自调用WCF 的 WebForms 应用程序的代码:

var client = new SyncClient("BasicHttpBinding_ISync");
client.InnerChannel.OperationTimeout = new TimeSpan(2, 00, 0);
var message = string.Empty;
try
{
    var syncResult = new SyncResult();
    syncResult = client.GetInventory(1, 2, 3);
    message += $"Sync Result<hr/>Added: {syncResult.ItemsAdded}<br/>Updated: {syncResult.ItemsUpdated}<br/>Duration: {syncResult.Duration}";
    ((ICommunicationObject)client).Close();
}
catch (System.Exception ex)
{
    (client as ICommunicationObject)?.Abort();
    message += $"Error<hr/>{ex?.Message ?? "null"}<hr/>{ex?.InnerException?.Message ?? "null"}";
}
inventoryItemsLbl.Text = message;

这是我的 WebForms Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>...</connectionStrings>
  <appSettings>...</appSettings>
  <system.web>
    <authentication mode="Forms">
      <forms loginUrl="Login.aspx" timeout="2880" />
    </authentication>
    <compilation targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" executionTimeout="900" />
    <pages>...</pages>
  </system.web>
  <system.webServer><modules>...</modules></system.webServer>
  <runtime>...</runtime>
  <system.codedom><compilers>...</compilers></system.codedom>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true">
    </serviceHostingEnvironment>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ISync" />
      </basicHttpBinding>
    </bindings>
    <client>
<endpoint address="http://localhost:8733/Design_Time_Addresses/ED_WMS.SYNC/Sync/" 
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISync" 
contract="ServiceReferenceWMS.ISync" name="BasicHttpBinding_ISync" />
    </client>
  </system.serviceModel>
</configuration>

这是我的 WCF App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>...</configSections>
  <entityFramework>...</entityFramework>
  <runtime>...</runtime>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="MobilityBeanSoapBinding" closeTimeout="02:00:00" openTimeout="02:00:00"
            receiveTimeout="02:00:00" sendTimeout="02:00:00" maxReceivedMessageSize="100000000"
            maxBufferSize="100000000" maxBufferPoolSize="100000000">
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://warehouse:8080/wmwebservice_ejb/MobilityBean" binding="basicHttpBinding" bindingConfiguration="MobilityBeanSoapBinding" contract="EdWmsReference.MobilityBean" name="MobilityRemotePort" />
    </client>
    <services>
      <service name="ED_WMS.SYNC.Sync" behaviorConfiguration="MyServiceTypeBehaviors">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/ED_WMS.SYNC/Sync/" />
          </baseAddresses>
        </host>
        <endpoint name="basicHttpEndpoint" address="" binding="basicHttpBinding" contract="ED_WMS.SYNC.ISync" bindingConfiguration="MobilityBeanSoapBinding"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors" >
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

标签: c#asp.netwcfwebformsiis-8

解决方案


当您从 DEV -> Production(当 WCF 应用程序现在托管在 IIS 中时)移动时,设计时 URL 会发生变化,您现在需要在 Webforms 应用程序中设置正确的 designTime URL。

有几个选项可用,但它们都指向更改与服务关联的 URI

client.Endpoint.Address = new EndpointAddress(Server.IsLiveServer() ?
    @"LiveUrl" : @"TestURl"); 

其中 TestURI 是设计时间 URL,而 LiveURL 是生产 URL。IsLiveServer 只是一个布尔值,用于检查您是处于开发阶段还是生产阶段。

您可以采取的另一种方法是创建一个 Realease vs Debug webconfig,该版本将具有已部署应用程序的 URL,然后使用 appSetting 或类似的东西设置客户端端点

第三个选项需要使用IIS 管理DLL 来查询托管网站并找出 WCF 服务的 URL 并在您的 Web 表单中使用它。


推荐阅读