完全使用接口方式調用WCF 服務


客戶端調用WCF服務可以通過添加服務引用的方式添加,這種方式使用起來比較簡單,適合小項目使用。服務端與服務端的耦合較深,而且添加服務引用的方式生成一大堆臃腫的文件。本例探討一種使用接口的方式使用WCF服務,克服通過服務引用方式產生的弊端。同時希望拋磚引玉,探討更好的方式使用WCF。

1. 架構概述

解決方案

說明:

接口層:數字計算接口

服務實現層:實現數字計算接口

發布:同過IIS方式發布WCF服務

客戶端:引用接口層,通過配置文件調用WCF服務

2. 接口層

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace Hbb0b0.WCF.Inteface
{
/// <summary>
/// 數學計算服務
/// </summary>
[ServiceContract]
public interface IMathService
{
/// <summary>
/// 相加服務
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
[OperationContract]
int Add(int p1,int p2);
}
}

3. 實現層

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Hbb0b0.WCF.Inteface;
namespace Hbb0b0.WCF.ServiceImp
{
/// <summary>
/// 計算服務實現
/// </summary>
public class MathService : IMathService
{
#region IMathService 成員
public int Add(int p1, int p2)
{
return p1 + p2;
}
#endregion
}
}

4. 發布層

1. SVC

<%@ServiceHost language=c# Debug="true" Service="Hbb0b0.WCF.ServiceImp.MathService" %>

2. Web.Config

<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="NewBinding0" />
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="MyServiceTypeBehaviors" name="Hbb0b0.WCF.ServiceImp.MathService">
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint address="http://localhost:1331/MathService.s vc" binding="basicHttpBinding" bindingName="NewBinding0"
name="address" contract="Hbb0b0.WCF.Inteface.IMathService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors" >
<!-- 將下列元素添加到服務行為配置中。 -->
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

3. 調用層

1. Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Configuration;
using System.ServiceModel.Configuration;
using Hbb0b0.WCF.Inteface;
using System.Reflection;
using System.ServiceModel.Channels;
namespace MathServiceClient
{
class Program
{
/// <summary>
/// ServiceModel 屬性
/// </summary>
static ServiceModelSectionGroup ServiceModelConfig;
/// <summary>
/// 構造函數中初始化ServiceModelConfig
/// </summary>
static Program()
{
if(ServiceModelConfig==null)
{
ServiceModelConfig = GetServiceModelSectionGroup();
}
}
static void Main(string[] args)
{
//不知道如何使用配置初始化Binding
//Binding binding = Assembly.GetCallingAssembly().CreateInstance(ServiceModelConfig.Bindings["NewBinding0"].BindingType.FullName) as Binding;
//初始化Endpoint
EndpointAddress point = new EndpointAddress(ServiceModelConfig.Client.Endpoints[0].Address);
//創建通道
IMathService service = ChannelFactory<IMathService>.CreateChannel(
new BasicHttpBinding () ,
point);
//調用
int result= service.Add(2, 3);
Console.WriteLine(string.Format("result={0}", result));
Console.Read();
}
/// <summary>
/// 獲取ServiceModel配置信息
/// </summary>
/// <returns></returns>
static ServiceModelSectionGroup GetServiceModelSectionGroup()
{
Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
ServiceModelSectionGroup s vcmod = (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");
return svcmod;
}
}
}
2. AppConfig
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://localhost:1331/MathService.svc" binding="basicHttpBinding"
contract="Hbb0b0.WCF.Inteface.IMathService" name="mathService" />
</client>
</system.serviceModel>
</configuration>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM