WCF进阶01 服务配置
2011-06-12 01:01 Ecin 阅读(325) 评论(0) 编辑 收藏 举报1. 准备工作
在进行WCF服务配置的时候,我们先做一些准备性的工作,定义好服务协定,数据协定,下面简单地例举出关于服务协定代码,数据协定的代码片段。
服务协定如下:
[ServiceContract]
public interface IContactServie
{
[OperationContract]
List<Contact> GetContracts();
}
public interface IContactServie
{
[OperationContract]
List<Contact> GetContracts();
}
其中,ServiceContract公布服务接口名称,OperationContract公布其服务协定操作方法,接口中未用特性标记OperationContract的方法不会公开被使用。
数据协定如下:
[DataContract]
public class Contact
{
[DataMember]
public String FirstName { get; set; }
[DataMember]
public String LastName { get; set; }
[DataMember]
public String EmailAddress { get; set; }
}
public class Contact
{
[DataMember]
public String FirstName { get; set; }
[DataMember]
public String LastName { get; set; }
[DataMember]
public String EmailAddress { get; set; }
}
需要注意的是,属性访问级别不影响数据协定中属性的公开。
对于服务实现的代码片段如下:
public class ContactService:IContactServie
{
public List<DataContractDomain.Person.Contact> GetContracts()
{
List<DataContractDomain.Person.Contact> contacts = new List<DataContractDomain.Person.Contact>();
using (EntityProvider provider=new EntityProvider())
{
var data = from contact in provider.Contact.Take(10)
select new { FirstName = contact.FirstName, LastName = contact.LastName, EmailAddress=contact.EmailAddress };
foreach (var item in data)
{
contacts.Add(new DataContractDomain.Person.Contact
{
FirstName=item.FirstName,
LastName=item.LastName,
EmailAddress=item.EmailAddress
});
}
}
return contacts;
}
}
{
public List<DataContractDomain.Person.Contact> GetContracts()
{
List<DataContractDomain.Person.Contact> contacts = new List<DataContractDomain.Person.Contact>();
using (EntityProvider provider=new EntityProvider())
{
var data = from contact in provider.Contact.Take(10)
select new { FirstName = contact.FirstName, LastName = contact.LastName, EmailAddress=contact.EmailAddress };
foreach (var item in data)
{
contacts.Add(new DataContractDomain.Person.Contact
{
FirstName=item.FirstName,
LastName=item.LastName,
EmailAddress=item.EmailAddress
});
}
}
return contacts;
}
}
以上是实现WCF服务最基本的操作,代码片段中服务协定我们使用了消息通信请求-应答的模式,另外还有单工、双工通信的方式,简单的实现方式需要在
特性ServiceContract的特性属性中指定IsOneWay=
true
,双工通信需要指定客户端回调,使用特性属性CallbackContract指定需要调用的服务
协定,回调的服务协定需要在客户端调用WCF时实现服务接口,以此来完成客户端、服务端的双向交互。
2.WCF服务端配置
服务端配置中包括服务终结点、绑定、行为,其中最重要的是配置服务终结点,终结点要指定地址、绑定类型、服务协定接口三个。终结点中的地址可以使用基地址 来指定;绑定类型有BasicHttpBinding,WsHtppBinding(BasicHttpBinding的“升级 版”),NetNamedPipeBinding,NetMsmqBinding等,另外,我们也可以实现自己的自定义绑定;服务协定接口需要对其命名空间 进行限定。 下面是简单配置举例:
<system.serviceModel>
<services>
<service name="ServiceContractImpl.Person.ContactService" behaviorConfiguration="ContactServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8005/AdventureWorks/Person/ContactService"/>
<add baseAddress="net.tcp://localhost:8003/AdventureWorks/Person/ContactService"/>
</baseAddresses>
</host>
<endpoint address="" binding="wsHttpBinding" contract="ServiceContractDomain.Person.IContactServie">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<endpoint address="" binding="netTcpBinding" contract="ServiceContractDomain.Person.IContactServie"/>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ContactServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<services>
<service name="ServiceContractImpl.Person.ContactService" behaviorConfiguration="ContactServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8005/AdventureWorks/Person/ContactService"/>
<add baseAddress="net.tcp://localhost:8003/AdventureWorks/Person/ContactService"/>
</baseAddresses>
</host>
<endpoint address="" binding="wsHttpBinding" contract="ServiceContractDomain.Person.IContactServie">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<endpoint address="" binding="netTcpBinding" contract="ServiceContractDomain.Person.IContactServie"/>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ContactServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
需要特别注意,服务名字需要明确指定命名空间;我们可以发布元数据,使用WCF框架内实现的IMetadataExchange服务协定来实现。
服务托管后,可以通过浏览器浏览公开的服务发布的元数据信息:
除了使用配置文件配置WCF服务端外,还可以通过编程方式实现:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Servcie Starting....");
using (ServiceHost host = new ServiceHost(typeof(ServiceContractImpl.Person.ContactService)))
{
host.AddServiceEndpoint(typeof(ServiceContractDomain.Person.IContactServie), new NetTcpBinding(), "net.tcp://localhost:8003/AdventureWorks/Person/ContactService");
host.AddServiceEndpoint(typeof(ServiceContractDomain.Person.IContactServie), new BasicHttpBinding(), "http://127.0.0.1:8003/ServiceContractImpl/Person/ContactService");
host.Open();
Console.WriteLine("Press <ENTER> to terminate client once the output is displayed.");
Console.ReadLine();
}
}
}
{
static void Main(string[] args)
{
Console.WriteLine("Servcie Starting....");
using (ServiceHost host = new ServiceHost(typeof(ServiceContractImpl.Person.ContactService)))
{
host.AddServiceEndpoint(typeof(ServiceContractDomain.Person.IContactServie), new NetTcpBinding(), "net.tcp://localhost:8003/AdventureWorks/Person/ContactService");
host.AddServiceEndpoint(typeof(ServiceContractDomain.Person.IContactServie), new BasicHttpBinding(), "http://127.0.0.1:8003/ServiceContractImpl/Person/ContactService");
host.Open();
Console.WriteLine("Press <ENTER> to terminate client once the output is displayed.");
Console.ReadLine();
}
}
}
温馨提示,调试环境中,以托管方式承载服务的http绑定时,如果您的操作系统为Vista或Win7,VS开发环境需要以管理员权限运行才能托管服务。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。