笔记--Wcf全面解析(上)---(1)

时间:2023-03-09 16:20:05
笔记--Wcf全面解析(上)---(1)
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using WcfServer2; namespace UnitTest
{
public class WCFBase
{
public WCFBase()
{
/*2.1,Address:通过URI做唯一标识、
* URI类型:
* ----HTTP/HTTPS
* ----Net.TCP
* ----Net.Pipe
* ----Net.Msmq
*/ /*2.2,EndPointAddress
*
* public class ServiceEndpoint
{
* 标识终结点地址
1,public EndpointAddress Address {get;set;}
* System.ServiceModel.EndpointAddress
* {
* Uri(定位),Headers(辅助寻址),Identity(身份识别) 三个只读属性
* }
* ServiceEndpoint通过 基类ServeiceHostBase中的
* AddServiceEndpoint方法增加终结点
*/
//代码
using (System.ServiceModel.ServiceHost service = new ServiceHost(typeof(IDataTransferCallback)))
{
service.AddServiceEndpoint(typeof(IDataTransferCallback), //实现契约的接口,只能以字符串和Type形式绑定
new WSHttpBinding(), //绑定类型
"http://www.baidu.com"); //地址 可以是URI类型,也可以是String类型的地址
service.Open(); //service.Description.Endpoints 可以获取该服务的所有终结点信息
}
//配置
//<system.serviceModel>表示wcf的配置节点
// 其中<services>包含了一组表示单个服务的<service>子节点
// <service>节点中的Name属性表示服务的名称
// 还包含了一组<endpoint>,通过address,binding,contract做为终结点的3要素 //ServiceHost构造函数
//前面参数serviceType 为服务类型,后面Uri表示可以访问到此服务的所有地址
//public ServiceHost(System.Type serviceType, params Uri[] baseAddresses);
//public ServiceHost(object singletonInstance, params Uri[] baseAddresses); // //1,客户端使用源数据生成接口
// class DataTransferCallbackClient : ClientBase<IDataTransferCallback>, IDataTransferCallback
// { // public void ReturnResult(string strJson)
// {
// base.Channel.ReturnResult(strJson); // } // }
////使用Channel生成
// //var factory = new ChannelFactory<IDataTransferCallback>();
// //var instance=factory.CreateChannel(new EndpointAddress("http://test.com/DataTransferCallback"));
// //instance.ReturnResult(); /*
2,public ContractDescription Contract{get;set;}
*
3,public Binding Binding {get;set;}
}
*
*/ /*客户端终结点*/ //服务调用的本质:采用匹配的终结点对目标终结点调用 p34 }
} }