WCF自寄宿
WCF很早就出现了,然而我感受到能够让新手重点去学习WCF而不是WebService是最近两年。我相信大部分人初步了解WCF的时候会很痛苦,尤其是生成代理类,以及配置的问题。我本人其实比较讨厌配置编程,但喜欢轻量配置,因此也一直研究如何自己定义配置节点,去让WCF服务识别,然后重量级ABC操作由编码来完成。下面,我将会提供一个我本人用于学习其他知识需要使用WCF时的开发模式,另外说明,为了更好的说明,我将提供的都是精简版本,并不适合用于测试生产的例子。
1.定义适合自己的配置
public class WCFServiceElement : ConfigurationElement
{
[ConfigurationProperty(name: "ServiceName", IsRequired = false)]
public string ServiceName
{
get
{
return (base["ServiceName"] as string);
}
set
{
base["ServiceName"] = value;
}
} [ConfigurationProperty(name: "ServiceAssembly", IsRequired = false)]
public string ServiceAssembly
{
get
{
return (base["ServiceAssembly"] as string);
}
set
{
base["ServiceAssembly"] = value;
}
} [ConfigurationProperty(name: "InterfaceServiceName", IsRequired = false)]
public string InterfaceServiceName
{
get
{
return (base["InterfaceServiceName"] as string);
}
set
{
base["InterfaceServiceName"] = value;
}
} [ConfigurationProperty(name: "InterfaceServiceAssembly", IsRequired = false)]
public string InterfaceServiceAssembly
{
get
{
return (base["InterfaceServiceAssembly"] as string);
}
set
{
base["InterfaceServiceAssembly"] = value;
}
} [ConfigurationProperty(name: "IPAddress", IsRequired = false)]
public string IPAddress
{
get
{
return (base["IPAddress"] as string);
}
set
{
base["IPAddress"] = value;
}
} [ConfigurationProperty(name: "Binding", IsRequired = false)]
public string Binding
{
get
{
return (base["Binding"] as string);
}
set
{
base["Binding"] = value;
}
}
}
public class WCFServicesElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
ConfigurationElement _element = new WCFServiceElement();
return _element;
} protected override object GetElementKey(ConfigurationElement element)
{
WCFServiceElement _element = (WCFServiceElement)element;
return _element.ServiceName;
} protected override string ElementName
{
get
{
return "WCFService";
}
} public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
}
public class WCFServiceSection : ConfigurationSection
{
[ConfigurationProperty(name: "", IsDefaultCollection = true, IsRequired = false)]
public WCFServicesElementCollection WCFServices
{
get
{
return (base[""] as WCFServicesElementCollection);
}
set
{
base[""] = value;
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="WCFServices" type="Basic.Configuration.WCFServiceSection,Basic.Data"/>
</configSections>
<WCFServices>
<WCFService ServiceName="AppManagementImplementation"
ServiceAssembly="Service.BackgroundManagement.Implementation"
InterfaceServiceName="IAppManagement"
InterfaceServiceAssembly="Service.BackgroundManagement.Interface"
IPAddress="http://192.168.1.4:8085/AppManagementService"
Binding="WSHttpBinding">
</WCFService>
</WCFServices>
</configuration>
这里注意的是configSections一定要放在第一行,这个倒是听让人无语的。当然配置可以用自己希望方式配置,我这里提供一个简单的思路。
2.定义适合自己的WCFServerConfigurationManagement
public abstract class WCFServerConfigurationManagement
{
private static HybridDictionary _Collection = new HybridDictionary(); public static void Register()
{
WCFServiceSection section = ConfigurationManager.GetSection("WCFServices") as WCFServiceSection;
if (null == section)
{
throw new NullReferenceException("未能识别WCFServices,请确认configSections配置节点");
} _Collection.Clear(); foreach (WCFServiceElement element in section.WCFServices)
{
Assembly assemblyInterfaceService = Assembly.Load(element.InterfaceServiceAssembly);
Assembly assemblyService = Assembly.Load(element.ServiceAssembly); Type typeInterface = assemblyInterfaceService.ExportedTypes.FirstOrDefault(type => type.Name.Equals(element.InterfaceServiceName));
Type typeImplement = assemblyService.ExportedTypes.FirstOrDefault(type => type.Name.Equals(element.ServiceName)); if (null == typeInterface && null == typeImplement)
{
throw new NullReferenceException("无法加载服务");
} ServiceHost _NewHost = new ServiceHost(typeImplement);
Binding binding = CreateBinding(element.Binding);
_NewHost.AddServiceEndpoint(typeInterface, binding, element.IPAddress); _NewHost.Opened += (sender, e) =>
{
#if DEBUG
Console.WriteLine($"服务:{element.ServiceName} 已经开启.");
#endif
};
_NewHost.Open();
}
} public static Binding CreateBinding(string bindingType)
{
Binding _Binding = default(Binding);
switch (bindingType)
{
case "BasicHttpBinding":
_Binding = new BasicHttpBinding();
break;
case "WSHttpBinding":
_Binding = new WSHttpBinding();
break;
case "WS2007HttpBinding":
_Binding = new WS2007HttpBinding();
break;
default:
throw new ArgumentNullException("根据接口名称无法匹配绑定类型");
} return _Binding;
}
}
这里主要用来根据配置动态配置WCF ServiceHost 和 Binding 的.然后再您的Custom Host 里面调用如下:
class Program
{
static void Main(string[] args)
{
WCFServerConfigurationManagement.Register(); Console.ReadLine();
}
}
3.定义适合自己的WCFClientConfigurationManagement
public abstract class WCFClientConfigurationManagement
{
private static HybridDictionary _Collection = new HybridDictionary(); public static void Register()
{
WCFServiceSection section = ConfigurationManager.GetSection("WCFServices") as WCFServiceSection;
if (null == section)
{
throw new NullReferenceException("未能识别WCFServices,请确认configSections配置节点");
} _Collection.Clear(); foreach (WCFServiceElement element in section.WCFServices)
{
_Collection.Add(element.InterfaceServiceName , element);
}
} public static Binding CreateBinding(string serviceName)
{
Binding _Binding = default(Binding);
WCFServiceElement element = _Collection[serviceName] as WCFServiceElement;
if (null == element)
{
Register();
element = _Collection[serviceName] as WCFServiceElement;
if (null == element)
{
throw new NullReferenceException("可能配置出现严重错误,根据接口名称无法获取配置文件信息.");
}
} switch (element.Binding)
{
case "BasicHttpBinding":
_Binding = new BasicHttpBinding();
break;
case "WSHttpBinding":
_Binding = new WSHttpBinding();
break;
case "WS2007HttpBinding":
_Binding = new WS2007HttpBinding();
break;
default:
throw new ArgumentNullException("根据接口名称无法匹配绑定类型");
} return _Binding;
} public static EndpointAddress CreateAddress(string serviceName)
{
WCFServiceElement element = _Collection[serviceName] as WCFServiceElement;
if (null == element)
{
Register();
element = _Collection[serviceName] as WCFServiceElement;
if (null == element)
{
throw new NullReferenceException("可能配置出现严重错误,根据接口名称无法获取配置文件信息.");
}
} EndpointAddress _Address = new EndpointAddress(element.IPAddress);
return _Address;
}
}
这里主要用来根据配置动态配置WCF Client 和 Binding 的。然后再您的客户端以及配置文件注册如下(我这里提供的是ASP.NET MVC 5 的环境):
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles); WCFClientConfigurationManagement.Register();
}
}
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="WCFServices" type="Basic.Configuration.WCFServiceSection,Basic.Data"/>
</configSections>
<WCFServices>
<WCFService InterfaceServiceName="IAppManagement"
IPAddress="http://192.168.1.4:8085/AppManagementService"
Binding="WSHttpBinding">
</WCFService>
</WCFServices>
</configuration>
4.定义适合自己的WCF ClientBase
public class WCFClient
{
public static TResult ClientInvoke<TChannel , TResult>(Func<TChannel , TResult> functionInvoke)
{
string _ServiceName = typeof(TChannel).Name;
Binding binding = WCFClientConfigurationManagement.CreateBinding(_ServiceName);
EndpointAddress address = WCFClientConfigurationManagement.CreateAddress(_ServiceName);
TChannel _channel = ChannelFactory<TChannel>.CreateChannel(binding, address);
TResult _Result = functionInvoke.Invoke(_channel);
return _Result;
}
}
以上交代完毕,当然提醒初步了解WCF的人IPAddress 配置也可以写*.svc地址,可以在自寄宿在Windows Service 里,也可以是IIS宿主环境。写到这里,希望这篇随笔能帮助到需要帮助的人,这也就是它的价值所在了。
WCF自寄宿的更多相关文章
- 创建WCF服务寄宿到IIS
一.WCF简介: Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. 整合了原有的win ...
- WCF技术剖析之四:基于IIS的WCF服务寄宿(Hosting)实现揭秘
原文:WCF技术剖析之四:基于IIS的WCF服务寄宿(Hosting)实现揭秘 通过<再谈IIS与ASP.NET管道>的介绍,相信读者已经对IIS和ASP.NET的请求处理管道有了一个大致 ...
- WCF自寄宿实现Https绑定
一.WCF配置 1 Address 将服务端发布地址和客户端访问地址都配置为https开始的安全地址.参考如下. <add key="SrvUrl" value=" ...
- 关于WCF引用方式之WCF服务寄宿控制台
1.创建解决方案WCFService 依次添加四个项目,如上图,Client和Hosting为控制台应用程序,Service和Service.Interface均为类库. 2.引用关系 Service ...
- WCF服务寄宿应用程序
1.先创建一个WCF服务库 2.创建一个Console控制台,服务将寄宿在该应用程序上,该程序一旦关闭,服务将停止. 控制台代码: using System; using System.Collect ...
- WCF服务寄宿到IIS
一.WCF简介: Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台.整合了原有的wind ...
- WCF服务寄宿IIS与Windows服务 - C#/.NET
WCF是Windows平台下程序间通讯的应用程序框架.整合和 .net Remoting,WebService,Socket的机制,是用来开发windows平台上分布式开发的最佳选择.wcf程序的运行 ...
- WCF服务寄宿IIS与Windows服务
WCF是Windows平台下程序间通讯的应用程序框架.整合和 .net Remoting,WebService,Socket的机制,是用来开发windows平台上分布式开发的最佳选择.wcf程序的 ...
- 将使用netTcp绑定的WCF服务寄宿到IIS7上全记录 (这文章也不错)
原文地址:http://www.cnblogs.com/wengyuli/archive/2010/11/22/wcf-tcp-host-to-iis.html 摘要 在项目开发中,我们可能会适时的选 ...
随机推荐
- Android开发学习之路-SnackBar使用心得
SnackBar是DesignSupportLibrary中的一个重要的控件,用于在界面下面提示一些关键信息,跟Toast不同的地方是SnackBar允许用户向右滑动消除它,同时,也允许在SnackB ...
- JavaEE:JavaEE技术组成
Java平台版本: JavaSE:Java Platform Standard Edition,标准版,用来开发桌面应用系统: JavaEE:Java Plateform Enterprise Edi ...
- 学习nodejs有感
接触nodejs一段时间了,不断的去接触接触,nodejs是一个能让前端程序员做后台开发的一项技术. 随着学习,让我更好的理解了前后端,以及浏览器是如何运作的
- LINQ系列:LINQ to SQL Where条件
1. 单一条件查询 var expr = context.Products .Where(p => p.ProductName == "LINQ to SQL"); SELE ...
- SQL Server中的窗口函数
简介 SQL Server 2012之后对窗口函数进行了极大的加强,但对于很多开发人员来说,对窗口函数却不甚了解,导致了这样强大的功能被浪费,因此本篇文章主要谈一谈SQL Server中窗口函 ...
- 【Win 10开发】协议-上篇:自定义应用协议
就像系统许多内置应用可以通过URI来启动(如ms-settings-bluetooth:可以打开蓝牙设置页),我们自己开发的应用程序,如果需要的话,可以为应用程序自定义一个协议.应用程序协议在安装时会 ...
- AVEVA Model Data Exchange Exports Structure Models
AVEVA Model Data Exchange Exports Structure Modelseryar@163.com Use Model Data Exchange Addin to exp ...
- 记录一则ORA-00054,ORA-00031解决过程
生产环境:AIX 5.3 + Oracle 10.2.0.5 任务要求:普通表改造分区表,历史数据不要 这个需求很简单: pl/sql导出建表语句,依次修改成分区的建表语句,注意将索引修改成本地索 ...
- 1Z0-053 争议题目解析685
1Z0-053 争议题目解析685 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 685.In your test database: -You are using Recover ...
- 【LeetCode】Reconstruct Itinerary(332)
1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...