學習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」。

 四 源代碼下載

相關文章
相關標籤/搜索