WCF簡單使用(分別部署在控制檯和IIS上)

 

WCF部署到控制檯html

1.下面經過一個簡單的服務示例來認識WCF
1.新建項目,名稱IBLL,解決方案名稱WcfDemo,模板選擇類庫
2.修改Class1.cs文件名稱爲 IUserInfoService.cs
3.添加引用 System.ServiceModel
4.修改IUserInfoService.cs代碼以下:瀏覽器

複製代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace IBLL
{
    [ServiceContract]
    public interface IUserInfoService
    {
        [OperationContract]
        int Add(int a, int b);
    }
}
複製代碼

咱們定義了一個IUserInfoService接口,注意在接口上申明瞭ServiceContract特性,即服務契約,代表該接口是一個服務。方法上聲明瞭OperationContract特性,表示該方法是IUserInfoService的一個服務方法,客戶端可遠程調用該方法app

5.新建項目,名稱BLL,模板選擇類庫,修改Class1.cs文件名稱爲 UserInfoService.cs,引用項目IBLL,代碼以下:post

複製代碼
using IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class UserInfoService : IUserInfoService
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
}
複製代碼

OK,到此咱們的服務代碼已經編寫完成,下面咱們必須爲服務提供一個運行的宿主,經過該宿主程序來啓動咱們的服務。
6.在同一解決方案下新建一個項目,名稱爲WcfHost,類型爲控制檯應用程序spa

7.WcfHost項目中添加引用,引用項目IBLL,而後再添加引用:System.ServiceModel.net

8.修改Program.cs代碼以下:debug

複製代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace WcfHost
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(BLL.UserInfoService)))
            {
                host.Open();
                Console.WriteLine("服務已啓動");
                Console.ReadKey();
                host.Close();
            }
        }
    }
}
複製代碼

以上,咱們已經實現了服務以及爲服務提供了一個運行宿主,即契約部分已經完成,下面咱們爲服務指定地址及綁定
9.修改app.config內容以下:調試

複製代碼
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      
      <service name="BLL.UserInfoService" behaviorConfiguration="behaviorConfiguration"><!--服務的對象-->
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/"/><!--服務的IP和端口號-->
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" contract="IBLL.IUserInfoService"></endpoint><!--contract:服務契約-->
      </service>   
    </services>
    
    <behaviors>
      <serviceBehaviors>
        <behavior name="behaviorConfiguration">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
複製代碼

10.設置WcfHost項目爲啓動項目,啓動調試。控制檯上顯示服務已啓動後,打開瀏覽器輸入服務地址:http://localhost:8000/ ,瀏覽器中會打開咱們的服務頁面,這表示咱們的服務已經啓動成功,客戶端可經過該地址訪問咱們的服務了。
下面,咱們將建立一個客戶端來訪問咱們的服務
11.在同一解決方案下新建一個項目,名稱爲WcfClient,類型爲控制檯應用程序,添加服務引用地址:http://localhost:8000/,客戶端代碼以下:code

複製代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WcfClient
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceReference1.UserInfoServiceClient client = new ServiceReference1.UserInfoServiceClient();
            Console.WriteLine(client.Add(3, 4));
            Console.ReadKey();
        }
    }
}
複製代碼

12.啓動WcfHost,啓動WcfClient,(記得找到bin/debug找.exe以管理員運行xml

 

WCF部署到IIS

1-5步服務代碼已經編寫相同,下面咱們必須爲服務提供一個IIS宿主,那麼當IIS啓動起來後,咱們的Wcf服務就起來了。

6.爲WCF建立.svc文件(新建類,後綴改成.svc便可),僅僅包含一個ServiceHost指令,該指令具備一個必須的Service屬性(該屬性指明瞭相應的WCF服務的類型)和一些可選的屬性,該.svc文件應放在Services項目的根目錄下

<%@ ServiceHost Service="BLL.UserInfoService" %>

7.在根目錄下建立一個Web.Config,將WCF的相應配置添加到配置文件中,與寄宿在上面控制檯方式不一樣的是,在添加的終結點中無需指定地址,由於.svc所在的地址就是服務端地址,代碼以下:(該配置放在configuration根節點下)

複製代碼
  <system.serviceModel><!--部署Wcf服務-->
    <services>
      <service name="BLL.UserInfoService" behaviorConfiguration="behaviorConfiguration"><!--服務的對象-->
      <endpoint address="" binding="basicHttpBinding" contract="IBLL.IUserInfoService"></endpoint><!--contract:服務契約-->
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="behaviorConfiguration">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
複製代碼

好了,WCF服務端配置好了,下面配置客戶端

8.客戶端配置,添加服務引用,地址填.svc所在的地址,如:http://localhost:8707/UserInfoService.svc,命名空間:WcfServiceReference

添加完成後,會在服務端的Web.Config中自動添加下面代碼

複製代碼
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IUserInfoService" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:8707/UserInfoService.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IUserInfoService"
        contract="WcfServiceReference.IUserInfoService" name="BasicHttpBinding_IUserInfoService" />
    </client>
  </system.serviceModel>
複製代碼

9.在客戶端調用服務端方法(在Home控制器下添加該方法)

複製代碼
        public ActionResult Add()
        {
            WcfServiceReference.UserInfoServiceClient client = new WcfServiceReference.UserInfoServiceClient();
            int sum = client.Add(3, 4);
            return Content(sum.ToString());
        }
複製代碼

 

 
分類:  .net
相關文章
相關標籤/搜索