学习WCF笔记之二

1、学习文章http://www.cnblogs.com/iamlilinfeng/archive/2012/09/25/2700049.htmlhtml

2、步骤浏览器

     学习WFC,按照大神的文章一步步学习,不过看似简单的过程,中间还会有各类莫名其妙的bug,本身记录了一下框架

     一、新建空白解决方案 WcfServiceWebide

     二、新建WcfService项目,类型为WCF应用程序。删除系统生成的两个文件IService1.cs与Service1.svc。学习

三、添加新项CalculatorService.svc测试

四、在自动生成的ICalculatorService.cs中添加契约代码网站

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfService
{
    // 注意: 使用“重构”菜单上的“重命名”命令,能够同时更改代码和配置文件中的接口名“ICalculatorService”。
    [ServiceContract]
    public interface ICalculatorService
    {
        [OperationContract]
        void DoWork();
        [OperationContract]
        string ShowName(string Name);
    }
}

  五、在CalculatorService.cs中添加代码this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfService
{
    // 注意: 使用“重构”菜单上的“重命名”命令,能够同时更改代码、svc 和配置文件中的类名“CalculatorService”。
    public class CalculatorService : ICalculatorService
    {
        public void DoWork()
        {
        }


        public string ShowName(string Name)
        {
            return Name;
        }
    }
}

  六、此时Web.config配置文件以下,注意红字部分包含命名空间,要写对spa

 <system.serviceModel>
    <services>
      <service behaviorConfiguration="WcfService.CalculatorServiceBehavior"
        name="WcfService.CalculatorService">
        <endpoint address="" binding="wsHttpBinding" contract="WcfService.ICalculatorService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfService.Service1Behavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="WcfService.CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

七、在文件CalculatorService.svc上右键-在浏览器中查看,以下图:.net

八、按照后面步骤三(1.3)发布网站,三(1.4)在发布到IIS中,三(1.3)、三(1.4)在后面会讲到

九、客户端测试,添加新项目WebApplication1

十、在WebApplication1--引用--添加服务引用,点击发现会列出服务,肯定。

十一、 在Default.aspx中添加一个按钮和一个文本框,添加代码以下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ServiceReference.CalculatorServiceClient client = new ServiceReference.CalculatorServiceClient();
            this.TextBox1.Text= client.ShowName("张三");
        }
    }
}

  十二、点击按钮,测试结果

3、问题点记录

一、如何发布网站

1.一、开启IIS服务

1.二、IIS注册到VS中 

  • 开始——运行中输入cmd——进入命令字符界面首先输入cd C:\Windows\Microsoft.NET\Framework\v4.0.30319—而后输入aspnet_regiis.exe -i

1.3 发布项目,项目-右键-发布,发布到一个地址,如D:\Web发布

 

1.4  添加网站

计算机右键——管理——服务和应用程序——Internet信息服务(IIS)管理器——网站右键——添加网站,在出现的提示框中输入网站名称,选择物理路径(1.3中的路径),选择IP地址便可。

     详细参见http://blog.csdn.net/zwk626542417/article/details/9796259

二、发布时遇到的问题(执行第一步时没注册aspnet_regiis.exe,由于不知道做用)

2.1 问题1

HTTP 错误 404.3 - Not Found

因为扩展配置问题而没法提供您请求的页面。若是该页面是脚本,请添加处理程序。若是应下载文件,请添加 MIME 映射。

缘由:系统没有默认为IIS注册WCF服务的svc文件的MIME映射。

解决方法:管理员身份运行C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe -i

步骤:以管理员身份打开cmd.exe,cd 进入目录C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation

而后输入:ServiceModelReg.exe -i  回车

 

2.二、问题2

HTTP 错误 500.21 - Internal Server Error

处理程序“svc-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”

缘由:没有注册.NET 4.0框架。

解决方法:管理员身份运行C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i

详细参见http://blog.csdn.net/mazhaojuan/article/details/7660657

 三、试着将类CalculatorService改成Calculator,Calculator.svc--右键--浏览--报错

解决办法:

<%@ ServiceHost Language="C#" Debug="true" Service="WcfService.CalculatorService" CodeBehind="Calculator.svc.cs" %>

改成

<%@ ServiceHost Language="C#" Debug="true" Service="WcfService.Calculator" CodeBehind="Calculator.svc.cs" %>

注意,在修改服务类的时候,要 使用“重构”菜单上的“重命名”命令,能够同时更改代码、svc 和配置文件中的类名“CalculatorService”。

 四 源代码下载

相关文章
相关标签/搜索