65.9K
CodeProject 正在变化。 阅读更多。
Home

WCF 与共享程序集( 仅供接口使用)

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.81/5 (10投票s)

2007年3月26日

CPOL

2分钟阅读

viewsIcon

53033

downloadIcon

291

将 WCF 用于业务解决方案(无配置文件,仅公开接口)

引言

这是我第一次为社区做贡献。受到 Bruno Hunziker 在 dotnetpro 4-2007 中一篇有趣文章的启发,我现在展示一个示例,说明如何使用 WCF 构建业务解决方案。

展示以下目标

没有 app.config 文件

相反,Service-Interface 上的一个属性(也包含 ServiceContract 属性)指定了默认设置。

无需生成代理

相反,共享程序集 (DLL) 在服务器和客户端定义了 Service-Interfaces。

没有公开的类

仅通过接口授予访问权限,所有类都是内部的。由于不需要互操作性(在这种情况下也不可能!),因此使用 NetDataContractSerializer 来实现这一点。

用于轻松处理的工厂

一些辅助函数使生活更轻松,并支持仅接口行为。

WCF 似乎是一个完美的系统,即使在局域网中也具有出色的客户端/服务器功能。目前,我正在寻找可能也符合现成产品需求的实现。希望我的想法能帮助其他人受到 WCF 的启发,或者一些反馈也能帮助我进一步改进。

该示例包含一个主机,该主机为 3 个绑定(命名管道、TCP、HTTP)打开一个服务。客户端通过使用其接口来添加或检索对象。在这种情况下,它是 IPerson IList<IPerson>

使用代码

最重要的类是工厂的辅助类。请参阅下文,了解辅助程序的最重要用途

namespace wcfClasses

Enum - EBindingType
Interface - IwcfConfig
Interface - IwcfRunning
Interface - IwcfInterfaceClass
Attribute - AppConfigAttribute     
    // Attribute on Service-Interface for Default Settings as in the app.config
Attribute - FactoryCreateAttribute 
    // Attribute on Interface for CreateInstance (used by GetClassType)
Class     - wcfConfig              
    // for Caching the Configuration for each Service-Interface
Class     - wcfRunning             
    // for Caching the running Hosts for each Serfice-Interface
Class     - wcfInterfaceClass      
    // for Caching the Classname for an Interface

static class wcfFactory

Helper    - Config<I>              
    // return the Configuration of a Service-Interface
Helper    - Running<I>             
    // return the Running Host of a Service-Interface (Serverside)
Helper    - CreateObject<I>        
    // creates a Instance for the given Interface
Helper    - CreateClient<I>        
    // creates a proxy for the given Service-Interface (Clientside)
Helper    - CloseClient<I>         
    // closes Client for a proxy (Clientside)
Helper    - CreateHost<I>          
    // creates a Host for the given Service-Interface (Serverside)
Helper    - CloseHost<I>           
    // closes a running Host (Serverside)
Helper    - GetClassType           
    // return the Type for a Class, given a Type of an Interface

在使用 static 辅助程序之前,应该使用所需的属性来增强 Service-Interfaces。

工厂的属性

AppConfig-属性设置默认设置,就像它们在 app.config 文件中设置一样。如果未设置它们,则必须在启动时由应用程序设置一次。由于它们被缓存,因此将在进一步使用时被重用。Operation 上的 NetDataContract 属性告诉 Serializer 使用 IPerson interface。只有 NetDataContract 包含 CLR 类型信息,与默认的 DataContractSerializer 相反。

[ServiceContract]
[AppConfig("wcfClasses.MgrPerson", BindingType  = EBindingType.PIPE,
                                   EndPointTCP  = "net.tcp://:8001/",
                                   EndPointHTTP = "https://:8000/")]
public interface IMgrPerson
{
   [OperationContract]
   [NetDataContract]
   void AddPerson(IPerson Person);

   [OperationContract]
   [NetDataContract]
   IList<IPerson> GetPersons();
}

FactoryCreate-属性设置实例化 interface 实例的默认类名

[FactoryCreate("wcfClasses.Person")]
public interface IPerson{...}

在主机上启动 Service-Interface

要在主机内启动 Service-interface ,只需将 interface 指定为 <I>。如果省略了 BindingType ,则将使用默认的 Binding

wcfFactory.CreateHost<IMgrPerson>(EBindingType.PIPE);

可以在启动之前通过覆盖缓存的设置来覆盖设置

wcfFactory.Config<IMgrPerson>().EndpointTCP = "net.tcp://:8002/";
wcfFactory.CreateHost<IMgrPerson>(EBindingType.TCP); 

在客户端连接 Service-Interface

在客户端使用 Service-interface 很简单

void AddContact(EBindingType BindingType)
{
   IPerson _person   = wcfFactory.CreateObject<IPerson>();
   _person.FirstName = edFirstName.Text;
   _person.LastName  = edLastName .Text;
   _person.Gender    = rbMale.Checked ? EGender.Male : EGender.Female;

   IMgrPerson _proxy = wcfFactory.CreateClient<IMgrPerson>(BindingType);
   _proxy.AddPerson(_person);
   wcfFactory.CloseClient<IMgrPerson>(_proxy);
}
void GetContacts(EBindingType BindingType)
{
   IMgrPerson _proxy = wcfFactory.CreateClient<IMgrPerson>(BindingType);
   IList<IPerson> _persons = _proxy.GetPersons();

   m_ContactsGrid.Visible    = false;
   m_ContactsGrid.DataSource = _persons;
   m_ContactsGrid.Visible    = true;

   wcfFactory.CloseClient<IMgrPerson>(_proxy);
}

就这样。从没想过为 CP 写一篇文章会这么难...
该示例快速而草率,让您在 10 分钟内了解了概况。
尽情享用!

参考文献

© . All rights reserved.