WCF 傻瓜教程

时间:2023-03-08 17:06:32

第一步,新建WCF服务应用程序

WCF 傻瓜教程

第二步,定义接口:

  修改接口类,定义你要的方法接口默认文件名:IService1.cs

第三步,实现接口:

  在svc文件里实现接口方法,默认文件名:Service1.svc

第四步,Web.config配制(很重要):

  基本上一般有问题都在这里。直接上传一个最简单的配制文件见下面代码:

<?xml version="1.0" encoding="utf-8"?>
<configuration> <system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel> <bindings>
<basicHttpBinding>
<binding name="centerBasicHttp" maxReceivedMessageSize="">
<readerQuotas maxDepth="" maxStringContentLength="" />
<security mode="None">
<transport clientCredentialType="None"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<!--******************要手工配的 开始******************-->
<services>
<!--添加服务 behaviorConfiguration 对应的值为 <serviceBehaviors>-<behavior>对应name值-->
<service name="WcfDemo.Service1" behaviorConfiguration="CalculatorServiceBehavior">
<!--name 必须与代码中的host实例初始化的服务一样
behaviorConfiguration 行为配置 -->
<!--binding属性要和上面bindings节点下的子节点相同,bindingConfiguration属性应该和binding节点中name属性值相同 name随便起 contract命名空间.类名-->
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="centerBasicHttp" name="server1" contract="WcfDemo.IService1"></endpoint>
</service>
</services>
<!--******************要手工配的 结束******************--> <!--定义CalculatorServiceBehavior的行为-->
<behaviors>
<serviceBehaviors>
<!-- ****************要配一个对应名称 ****************-->
<behavior name="CalculatorServiceBehavior">
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
<serviceMetadata httpGetEnabled="true" />
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
</system.webServer>
</configuration>

webconfig

  

第五步,建网站

  WCF 傻瓜教程

  如果你访问你的  分析器错误消息: 无法识别的属性“targetFramework”。请注意属性名称区分大小写。 那就是你的应用程序池选的不对。

  主机名可以是用配置