WCF部署到控制台
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的一個服務方法,客戶端可遠程調用該方法
5.新建項目,名稱BLL,模板選擇類庫,修改Class1.cs文件名稱為 UserInfoService.cs,引用項目IBLL,代碼如下:
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,類型為控制台應用程序
7.WcfHost項目中添加引用,引用項目IBLL,然后再添加引用:System.ServiceModel
8.修改Program.cs代碼如下:
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/,客戶端代碼如下:
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以管理員運行

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());
}


