xlid 发表于 2015-8-13 09:11:51

WCF入门教程(VS2008 + IIS)

  最近一个项目需要使用WCF,参考了很多网上资料和MSDN,总结如下:
  1:建立一个IIS作为HOST的WCF工程
  选择NEW Project,然后选择C#,然后选择WEB,然后选择WCF Service Application
  VS2008将自动帮你生成一个示例项目,只需要在它基础上修改就可以。
  2:修改WEB.CONFIG文件
  如果你想在不同的机器访问WCF服务,需要设置身份验证,最简单是设置为None。如下:
  <system.serviceModel>
   
<services>
   <service behaviorConfiguration="WCFService.WCFServiceBehavior" name="WCFService.WCFService">
    <endpoint address="http://localhost:9000/wcfservice" binding="wsHttpBinding" bindingConfiguration="WCFBindConfig"contract="WCFService.IWCFService">
   <identity>
      <dns value="localhost"/>
   </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
   </service>
</services>
   
<behaviors>
   <serviceBehaviors>
    <behavior name="WCFService.WCFServiceBehavior">
   <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
   <serviceMetadata httpGetEnabled="true"/>
   <!-- To receive exception details in faults for debugging purposes, set the value below to true.Set to false before deployment to avoid disclosing exception information -->
   <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
   </serviceBehaviors>
</behaviors>
    <!--
    using System.IdentityModel
    -->
    <bindings>
      <wsHttpBinding>
      <binding name ="WCFBindConfig">
          <security mode ="None">
          </security>
      </binding>
      </wsHttpBinding>
    </bindings>
   
</system.serviceModel>
  3:然后客户端也相应的设置为None就可以访问,这里需要特别注意的就是:
  <bindings>
      <wsHttpBinding>
      <binding name ="WCFBindConfig">
          <security mode ="None">
          </security>
      </binding>
      </wsHttpBinding>
    </bindings>

  和上面的一行
  <endpoint address="http://localhost:9000/wcfservice" binding="wsHttpBinding" bindingConfiguration="WCFBindConfig"contract="WCFService.IWCFService">

  4:如果想使用用户名密码验证,WEB.CONFIG配置如下:
  
  <system.serviceModel>
   
<services>
   <service behaviorConfiguration="WCFService.WCFServiceBehavior" name="WCFService.WCFService">
    <endpoint address="http://localhost:9000/wcfservice" binding="wsHttpBinding" bindingConfiguration="WCFBindConfig"contract="WCFService.IWCFService">
   <identity>
      <dns value="localhost"/>
   </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
   </service>
</services>
   
<behaviors>
   <serviceBehaviors>
    <behavior name="WCFService.WCFServiceBehavior">
   <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
   <serviceMetadata httpGetEnabled="true"/>
   <!-- To receive exception details in faults for debugging purposes, set the value below to true.Set to false before deployment to avoid disclosing exception information -->
   <serviceDebug includeExceptionDetailInFaults="false"/>
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode ="Custom"customUserNamePasswordValidatorType="WCFService.WCFService+MyUserNamePasswordValidator,WCFService"/>
          </serviceCredentials>
    </behavior>
   </serviceBehaviors>
</behaviors>
    <!--
    using System.IdentityModel
    -->
    <bindings>
      <wsHttpBinding>
      <binding name ="WCFBindConfig">
          <security mode ="Message">
            <message clientCredentialType ="Windows" establishSecurityContext ="false"/>
          </security>
      </binding>
      </wsHttpBinding>
    </bindings>
   
</system.serviceModel>

  需要注意的如下:
  <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode ="Custom"customUserNamePasswordValidatorType="WCFService.WCFService+MyUserNamePasswordValidator,WCFService"/>
          </serviceCredentials>

  MyUserNamePasswordValidator是自己定义的一个验证类,必须从UserNamePasswordValidator继承。
  WCFService是你的程序集名称,也就是工程名。
  WCFService.WCFService是你的namespace.
  
  
  
页: [1]
查看完整版本: WCF入门教程(VS2008 + IIS)