小弟没有接触过WCF,最近用到了从网上下载了一个很小的实例,已经查了MSDN了,但还是有点看不明白,请各位大侠指点下,这是服务器端的程序:
主程序“Progrom.cs”:using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace ServiceTest1
{
class Program
{
static void Main(string[] args)
{
//A:定义URI 根据绑定协议的不同uri的格式也不同
Uri ServiceAddress = new Uri("http://localhost:10000/ServiceTest1");
问题一:这里是不是根据所使用的“协议”返回Uri统一资源定位符。这个统一资源符标记了这个服务器端的地址,以便被客户端访问。小弟理解的对吗?如果是,公网ID、域名或是一个普通的IP地址可以吗?
//建立服务承载主机,把服务装载入承载主机中
ServiceHost ServiceHost1 = new ServiceHost(typeof(CPCalculatorHello), ServiceAddress);
问题二:神马叫做承载主机?有神马作用?问题三:把用到的方法用于实例化“ServiceHost”,就相当于把用于“服务的方法
”载入主机了?
try
{
//添加服务终结点,并加入绑定B(Binding)
ServiceHost1.AddServiceEndpoint(typeof(CalculatorHello), new WSHttpBinding(),"CPCalculatorHello");问题四:神马叫做“服务终结点”? 有神吗作用?问题五:神马是“加入绑定”,有什么作用?WSHttpBinding()是什么方法?
//通过Service行为开启元数据交换功能,以便通过SvcUtil.exe工具获取服务元数据创建客户端代理类
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
问题六:什么是“元数据”交换功能?问题七:什么是“客户端代理类”,是不是客户端调用服务器中类的方法?
smb.HttpGetEnabled = true;
ServiceHost1.Description.Behaviors.Add(smb);
问题八:这一句什么意思?
//开启服务
ServiceHost1.Open();
Console.WriteLine("服务已经启动");
Console.WriteLine("Press <ENTER> 关闭服务");
Console.WriteLine();
Console.ReadLine();问题九:这里的ReadLine啥意思?
//关闭服务
ServiceHost1.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine(ce.Message + "错误");
ServiceHost1.Abort(); } }
}
}
一个接口“ICalculatorHello.cs”:
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace ServiceTest1
{
class ICalculatorHello
{
} //C:契约标签标识服务类或服务接口
[ServiceContract]
问题十:这是什么啊?有什么作用?
public interface CalculatorHello
{
//操作方法契约标签
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
void HelloWorld(string name);
}
}接口的实现方法“PCalculatorHello.cs”:
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace ServiceTest1
{
class PCalculatorHello
{
}
public class CPCalculatorHello : CalculatorHello
{
//继承接口,实现接口中申明的方法
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Received Add({0},{1})", n1, n2);
Console.WriteLine("Return:{0}", result);
return result;
} public void HelloWorld(string name)
{
Console.WriteLine(name + " OnLine");
} }
}
主程序“Progrom.cs”:using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace ServiceTest1
{
class Program
{
static void Main(string[] args)
{
//A:定义URI 根据绑定协议的不同uri的格式也不同
Uri ServiceAddress = new Uri("http://localhost:10000/ServiceTest1");
问题一:这里是不是根据所使用的“协议”返回Uri统一资源定位符。这个统一资源符标记了这个服务器端的地址,以便被客户端访问。小弟理解的对吗?如果是,公网ID、域名或是一个普通的IP地址可以吗?
//建立服务承载主机,把服务装载入承载主机中
ServiceHost ServiceHost1 = new ServiceHost(typeof(CPCalculatorHello), ServiceAddress);
问题二:神马叫做承载主机?有神马作用?问题三:把用到的方法用于实例化“ServiceHost”,就相当于把用于“服务的方法
”载入主机了?
try
{
//添加服务终结点,并加入绑定B(Binding)
ServiceHost1.AddServiceEndpoint(typeof(CalculatorHello), new WSHttpBinding(),"CPCalculatorHello");问题四:神马叫做“服务终结点”? 有神吗作用?问题五:神马是“加入绑定”,有什么作用?WSHttpBinding()是什么方法?
//通过Service行为开启元数据交换功能,以便通过SvcUtil.exe工具获取服务元数据创建客户端代理类
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
问题六:什么是“元数据”交换功能?问题七:什么是“客户端代理类”,是不是客户端调用服务器中类的方法?
smb.HttpGetEnabled = true;
ServiceHost1.Description.Behaviors.Add(smb);
问题八:这一句什么意思?
//开启服务
ServiceHost1.Open();
Console.WriteLine("服务已经启动");
Console.WriteLine("Press <ENTER> 关闭服务");
Console.WriteLine();
Console.ReadLine();问题九:这里的ReadLine啥意思?
//关闭服务
ServiceHost1.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine(ce.Message + "错误");
ServiceHost1.Abort(); } }
}
}
一个接口“ICalculatorHello.cs”:
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace ServiceTest1
{
class ICalculatorHello
{
} //C:契约标签标识服务类或服务接口
[ServiceContract]
问题十:这是什么啊?有什么作用?
public interface CalculatorHello
{
//操作方法契约标签
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
void HelloWorld(string name);
}
}接口的实现方法“PCalculatorHello.cs”:
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace ServiceTest1
{
class PCalculatorHello
{
}
public class CPCalculatorHello : CalculatorHello
{
//继承接口,实现接口中申明的方法
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Received Add({0},{1})", n1, n2);
Console.WriteLine("Return:{0}", result);
return result;
} public void HelloWorld(string name)
{
Console.WriteLine(name + " OnLine");
} }
}
http://blog.csdn.net/fangxinggood/archive/2010/12/28/6101790.aspx
大哥,你的问题,难道google不到吗?给你个网址WCF步步为营,写的不错。
webservice学起Webservice是什么啊???
时间有点来不及了从WebService?它不是发布B/S的吗?可小弟正在做C/S下的系统啊???