我们把WCF寄宿在IIS之上,在IIS中宿主一个服务的主要优点是在发生客户端请求时宿主进程会被自动启动,并且你可以依靠IIS来管理宿主进程的生命周期。在开发和使用的过程与Web Service非常相似。

1) 为WCF服务创建.svc文件

       对于ASP.NET Web Service的每个服务都具有一个.asmx文件,客户端通过访问.asmx文件实现对相应Web Service的调用。WCF服务与Web Service类似,每个WCF服务也具有一个对应的文 件,其文件扩展名为.svc。基于IIS的服务寄宿要求相应的WCF服务具有相应的.svc文件,.svc文件部署于IIS站点中,对WCF服务的调用体现在对.svc文件的访问上。

       .svc文件的内容很简单,仅仅包含一个ServiceHost指令(Directive),该指令具有一个必须的Service属性和一些可选的 属性。所以最简单的.svc仅仅具有一个包含Service属性(该属性指明了相应的WCF服务的有效类型)的ServiceHost指令。 BookService对应的.svc如下所示,我们把该.svc放在Services项目的根目录下,并将文件命名为 BookService.svc。
         <%@ ServiceHost Language="C#" Debug="true" Service="WcfServiceLib.BookService" %>

2) 为WCF创建web.config文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <directoryBrowse enabled="true" />
    </system.webServer>
         <system.serviceModel>
    <services>
      <service name="WcfServiceLib.BookService">
        <endpoint address="" binding="basicHttpBinding" contract="WcfServiceLib.IBookService">
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 为避免泄漏元数据信息,
          请在部署前将以下值设置为 false -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
          <!-- 要接收故障异常详细信息以进行调试,
          请将以下值设置为 true。在部署前设置为 false
          以避免泄漏异常信息 -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
    <system.web>
        <compilation defaultLanguage="c#" />
    </system.web>
</configuration>

3) 将上述文件和程序集放到相应目录下

        创建一个文件夹,将上述文件和程序集放到想应目录下。这里,创建文件夹‘WCF’,将文件‘BookService.svc’,‘Web.config’放入。
因为一个Web应用在运行的时候会默认从位于根目录下的Bin目录加载程序集,所以,程序集‘WcfServiceLib.dll’放入子目录‘bin'下,如下图。

【转】WCF服务部署到IIS7.5(备忘)

4) 为WCF服务创建WebSite

1. 在IIS 7.5中,在”Default Web Site”上右键弹出快捷菜单,点击“添加应用程序…”。
【转】WCF服务部署到IIS7.5(备忘)


2. 在“添加应用程序”对话框中,分别在“别名”、“物理路径”文本框中输入内容,同时点击“选择”按钮,在弹出的“选择应用程序池”中选择“ASP.NET v4.0 Classic”。
【转】WCF服务部署到IIS7.5(备忘)


3. 创建完成之后的结果,如下图。
【转】WCF服务部署到IIS7.5(备忘)


4. 浏览BookService.svc页面。如下图。
【转】WCF服务部署到IIS7.5(备忘)


5. 最后结果,如下图。
【转】WCF服务部署到IIS7.5(备忘)
5) 寄宿过程中的错误解决方法
1. HTTP 错误 404.3 - Not Found由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。如下图。

【转】WCF服务部署到IIS7.5(备忘)

解决方案:

方法如下:控制面板—>所有面板项—>程序和功能—>打开或关闭Windows功能—>

【转】WCF服务部署到IIS7.5(备忘)

2.  未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载类型 “System.ServiceModel.Activation.HttpModule”说明执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息System.TypeLoadException: 未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载类型 “System.ServiceModel.Activation.HttpModule”。如下图
【转】WCF服务部署到IIS7.5(备忘)

解决方案:

      在CMD命令窗口中执行以下指令。 aspnet_regiis.exe -i。如下图
【转】WCF服务部署到IIS7.5(备忘)

3.“/CoreService”应用程序中的服务器错误。

Schema specified is not valid. Errors:
JetSun.DataModel.Cis.Oracle.EdmActSite.ssdl(2,2) : error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'Oracle.ManagedDataAccess.Client'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

或者

找不到请求的 .Net Framework Data Provider。 可能没有安装。

【转】WCF服务部署到IIS7.5(备忘)

 

解决方案:

1、需要存在下面3个文件。Web.config内容需正确

【转】WCF服务部署到IIS7.5(备忘)

Web.config的正确内容(WIN10则需要在Application的BIN路径下都存在该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"/>
    <section name="oracle.manageddataaccess.client"
      type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
  </configSections>

 
    <system.webServer>
        <defaultDocument>
            <files>
                <add value="Default.html" />
            </files>
        </defaultDocument>
    </system.webServer>

<connectionStrings>
</connectionStrings>

  <system.diagnostics >
    <sharedListeners>
      <add name="sharedListener"
           type="System.Diagnostics.XmlWriterTraceListener"
           initializeData="c:/temp/trace.svclog" />
    </sharedListeners>
    <sources>
      <source name="System.ServiceModel" switchValue="Verbose, ActivityTracing" >
        <listeners>
          <add name="sharedListener" />
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging" switchValue="Verbose">
        <listeners>
          <add name="sharedListener" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
  


  <oracle.manageddataaccess.client>
    <version number="4.121.2.0">
      <edmMappings>
        <edmMapping dataType="number">
          <add name="bool" precision="1" />
        </edmMapping>
      </edmMappings>
    </version>
  </oracle.manageddataaccess.client>

   <runtime>
      <gcServer enabled="true"/>
   </runtime>

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb"/>
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
      <provider invariantName="Oracle.ManagedDataAccess.Client"
        type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
    </providers>
  </entityFramework>

</configuration>

 

4. 它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素 system.serviceModel/serviceHostingEnvironment/serviceActivations 中提供。

解决方案:

   打开.svc文件,你会看到<%@ ServiceHost Language="C#" Debug="true" Service="WcfServiceLib.BookService" %> 注意红色字体要与WCF提供的类一致

 

相关文章: