View Code
staticvoid Main(string[] args)
{
Console.WriteLine(
"请输入num1,num2");
int num1 = Convert.ToInt32(Console.ReadLine());
int num2 = Convert.ToInt32(Console.ReadLine());

//获取计算加法服务
MySumServiceMex.WcfServiceClient sum =new client2.MySumServiceMex.WcfServiceClient();
//将之传给服务端
int num = sum.Sum(num1, num2);
//输出返回结果
Console.WriteLine("和为{0}", num);
//获取person服务
MySumServiceMex.Person persongender =new client2.MySumServiceMex.Person();


Console.WriteLine(
"请输入性别和姓名:");
//取出用户输入的姓名和性别
persongender.Gender = Console.ReadLine();
persongender.Name
= Console.ReadLine();
//将姓名和性别重新返回给服务端,定义心得对象newperson来接受返回值
MySumServiceMex.Person newperson = sum.ChangeGender(persongender);


//将新的对象性别和姓名输出
Console.WriteLine("性别为{0},姓名为{1}", newperson.Gender, newperson.Name);
Console.ReadKey();
}

建立wf文件名为IWcf

View Code
namespace IWcf
{

//ServiceContract定义wcf服务
[ServiceContract]
publicinterface IWcfService
{
//OperationContract将服务方法暴露给用户
[OperationContract]
int Sum(int num1,int num2);

[OperationContract]
Person ChangeGender(Person person);
}
//DataContract定义次对象可以被序列化,但是仅仅定义了这个而不定义DataMember的话用户是不能序列化特定属性的
[DataContract]
publicclass Person
{
string gander =string.Empty;
string name =string.Empty;

//DataMember定义某个属性可以被序列化
[DataMember]
publicstring Gender
{
get { return gander; }
set { gander = value; }
}
[DataMember]
publicstring Name
{
get { return name; }
set { name = value; }
}

}
}

实现类

View Code
namespace IWcf
{
publicclass WcfSerivce:IWcfService
{

#region IWcfService 成员

//实现加法运算
publicint Sum(int num1, int num2)
{
return num1 + num2;
}
#endregion


#region IWcfService 成员

//将客户端的数据进行逻辑处理,即将性别翻转,这样就实现客户端与服务端的数据通讯交互,即序列化得实现
public Person ChangeGender(Person person)
{

if (person.Gender =="")
{
person.Gender
="";
}
elseif (person.Gender =="")
{
person.Gender
="";
}
return person;
}

#endregion
}
}

配置文件

View Code
<configuration>
<system.serviceModel>
<bindings />
<client />
<services>
<service name="IWcf.WcfSerivce" behaviorConfiguration="sumservicewcf">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8008/mysumservice"/>
</baseAddresses>
</host>
<endpoint address="" binding="wsHttpBinding" contract="IWcf.IWcfService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="sumservicewcf">
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

服务启动

View Code
publicstaticvoid Main(string[] args)
{
ServiceHost host
=new ServiceHost(typeof(IWcf.WcfSerivce));
try
{
host.Open();
Console.WriteLine(
"Service open");
Console.ReadLine();
}
catch
{
Console.WriteLine(
"WCF服务异常");
Console.ReadLine();
}
}

客户端实现

View Code
1 staticvoid Main(string[] args)
2 {
3 Console.WriteLine("请输入num1,num2");
4 int num1 = Convert.ToInt32(Console.ReadLine());
5 int num2 = Convert.ToInt32(Console.ReadLine());
6
7 //获取计算加法服务
8   MySumServiceMex.WcfServiceClient sum =new client2.MySumServiceMex.WcfServiceClient();
9 //将之传给服务端
10  int num = sum.Sum(num1, num2);
11 //输出返回结果
12   Console.WriteLine("和为{0}", num);
13 //获取person服务
14   MySumServiceMex.Person persongender =new client2.MySumServiceMex.Person();
15
16
17 Console.WriteLine("请输入性别和姓名:");
18 //取出用户输入的姓名和性别
19   persongender.Gender = Console.ReadLine();
20 persongender.Name = Console.ReadLine();
21 //将姓名和性别重新返回给服务端,定义心得对象newperson来接受返回值
22 MySumServiceMex.Person newperson = sum.ChangeGender(persongender);
23
24
25 //将新的对象性别和姓名输出
26 Console.WriteLine("性别为{0},姓名为{1}", newperson.Gender, newperson.Name);
27 Console.ReadKey();
28 }
posted on 2011-04-28 15:34  高兴happy  阅读(366)  评论(0编辑  收藏  举报