1: public static void Create<T>(out MessageEncoderFactory encoderFactory,
<!--CRLF-->
2: out IDictionary<string, IClientMessageFormatter> clientFormatters,
<!--CRLF-->
3: out IDictionary<string, IDispatchMessageFormatter> dispatchFormatters,
<!--CRLF-->
4: out IDictionary<string, IOperationInvoker> operationInvokers,
<!--CRLF-->
5: out IDictionary<string, MethodInfo> methods)
<!--CRLF-->
6: {
<!--CRLF-->
7: //确保类型T是应用了ServiceContractAttribute的服务契约
<!--CRLF-->
8: object[] attributes = typeof(T).GetCustomAttributes(typeof(ServiceContractAttribute), false);
<!--CRLF-->
9: if (attributes.Length == 0)
<!--CRLF-->
10: {
<!--CRLF-->
11: throw new InvalidOperationException(string.Format("The type \"{0}\" is not a ServiceContract!", typeof(T).AssemblyQualifiedName));
<!--CRLF-->
12: }
<!--CRLF-->
13:
<!--CRLF-->
14: //创建字典保存IClientMessageFormatter、IDispatchMessageFormatter、IOperationInvoker和MethodInfo
<!--CRLF-->
15: clientFormatters = new Dictionary<string, IClientMessageFormatter>();
<!--CRLF-->
16: dispatchFormatters = new Dictionary<string, IDispatchMessageFormatter>();
<!--CRLF-->
17: operationInvokers = new Dictionary<string, IOperationInvoker>();
<!--CRLF-->
18: methods = new Dictionary<string, MethodInfo>();
<!--CRLF-->
19:
<!--CRLF-->
20: //MessageEncoderFactory
<!--CRLF-->
21: string encoderFactoryType = "System.ServiceModel.Channels.TextMessageEncoderFactory,System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
<!--CRLF-->
22: encoderFactory = (MessageEncoderFactory)Activator.CreateInstance(Type.GetType(encoderFactoryType), MessageVersion.Default, Encoding.UTF8, int.MaxValue, int.MaxValue, new XmlDictionaryReaderQuotas());
<!--CRLF-->
23:
<!--CRLF-->
24: //得到OperationDecription列表
<!--CRLF-->
25: string defaultNamespace = "http://tempuri.org/";
<!--CRLF-->
26: ServiceContractAttribute serviceAttribute = (ServiceContractAttribute)attributes[0];
<!--CRLF-->
27: string serviceNamepace = string.IsNullOrEmpty(serviceAttribute.Namespace) ? defaultNamespace : serviceAttribute.Namespace;
<!--CRLF-->
28: string serviceName = string.IsNullOrEmpty(serviceAttribute.Name) ? typeof(T).Name : serviceAttribute.Name;
<!--CRLF-->
29: var operations = ContractDescription.GetContract(typeof(T)).Operations;
<!--CRLF-->
30:
<!--CRLF-->
31: //得到具体的IClientMessageFormatter、IDispatchMessageFormatter和IOperationInvoker的具体类型
<!--CRLF-->
32: //IClientMessageFormatter+IDispatchMessageFormatter:DataContractSerializerOperationFormatter
<!--CRLF-->
33: //IOperationInvoker:SyncMethodInvoker
<!--CRLF-->
34: string formatterTypeName = "System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter,System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
<!--CRLF-->
35: Type formatterType = Type.GetType(formatterTypeName);
<!--CRLF-->
36: ConstructorInfo formatterConstructor = formatterType.GetConstructor(new Type[] { typeof(OperationDescription), typeof(DataContractFormatAttribute), typeof(DataContractSerializerOperationBehavior) });
<!--CRLF-->
37: string operationInvokerTypeName = "System.ServiceModel.Dispatcher.SyncMethodInvoker,System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
<!--CRLF-->
38: Type operationInvokerType = Type.GetType(operationInvokerTypeName);
<!--CRLF-->
39:
<!--CRLF-->
40: foreach (MethodInfo method in typeof(T).GetMethods())
<!--CRLF-->
41: {
<!--CRLF-->
42: attributes = method.GetCustomAttributes(typeof(OperationContractAttribute), true);
<!--CRLF-->
43: if (attributes.Length > 0)
<!--CRLF-->
44: {
<!--CRLF-->
45: OperationContractAttribute operationAttribute = (OperationContractAttribute)attributes[0];
<!--CRLF-->
46: string operationName = string.IsNullOrEmpty(operationAttribute.Name) ? method.Name : operationAttribute.Name;
<!--CRLF-->
47: //通过OperationContractAttribute得到Action
<!--CRLF-->
48: string action;
<!--CRLF-->
49: if (string.IsNullOrEmpty(operationAttribute.Action))
<!--CRLF-->
50: {
<!--CRLF-->
51: action = string.Format("{0}{1}/{2}", serviceNamepace, serviceName, operationName);
<!--CRLF-->
52: }
<!--CRLF-->
53: else
<!--CRLF-->
54: {
<!--CRLF-->
55: action = operationAttribute.Action;
<!--CRLF-->
56: }
<!--CRLF-->
57:
<!--CRLF-->
58: OperationDescription operation = operations.Where(op => op.Name == operationName).ToArray<OperationDescription>()[0];
<!--CRLF-->
59: //通过反射创建DataContractSerializerOperationFormatter对象
<!--CRLF-->
60: object formatter = formatterConstructor.Invoke(new object[] { operation, new DataContractFormatAttribute(), null });
<!--CRLF-->
61: clientFormatters.Add(operationName, formatter as IClientMessageFormatter);
<!--CRLF-->
62: dispatchFormatters.Add(action, formatter as IDispatchMessageFormatter);
<!--CRLF-->
63:
<!--CRLF-->
64: //通过反射创建SyncMethodInvoker对象
<!--CRLF-->
65: IOperationInvoker operationInvoker = (IOperationInvoker)Activator.CreateInstance(operationInvokerType, method);
<!--CRLF-->
66: operationInvokers.Add(action, operationInvoker);
<!--CRLF-->
67: methods.Add(action, method);
<!--CRLF-->
68: }
<!--CRLF-->
69: }
<!--CRLF-->
相关推荐
为了能够方便读者的学习,本书前面几个章节详细的讲解了ASP.NET 开发工具的安装,数据库系统的 安装以及ASP.NET 的基本知识。ASP.NET 使用的是面向对象的思想进行应用程序开发,本书还详细的讲解 了面向对象的概念...
本书在实例章节中,详细的介绍了如何进行规范的应用程序开发,包括设计需求分析文档以及编写类图等,规范的应用程序开发是非常重要的,同时本书还介绍了ASP.NET应用程序开发技巧,以便读者能够规范的、快速的编写...
此为《C#与.NET 4高级程序设计:第5版》中文版一书的源码。 Amazon超级畅销书,权威新版王者归来 全面涵盖C# 2010,用IL深入揭示语言特性 多位微软MVP联手翻译,名著佳译相得益彰 本书是C# 领域久负盛名的经典著作...
书中介绍了c#的各种语言构造、.net 2.0的类、核心api、公共中间语言(cil)、动态程序集和asp.net扩展等内容;同时也介绍了.net 3.0和.net 3.5中新的编程api,包括wpf 、wcf和wf 的功能;另外,还介绍了最新的c# 3.0...
蒋金楠,网名Artech,现就职于某知名软件公司,担任高级软件顾问(Senior Software Consultant)。微软解决方案架构(Solutions ...属国内较早接触WCF的人之一,同时对.NET Remoting、MSMQ通信技术有深入的理解。
本期MSDN杂志的主要内容有: ...• 非常 ASP.NET: 使用 ASP.NET 控件封装 Silverlight • { End Bracket }: 技术人员的激情 • 编者按: 新年新视角 • 工具箱: 高级单元测试、对象模拟、剖析应用程序及更多内容
本书在实例章节中,详细的介绍了如何进行规范的应用程序开发,包括设计需求分析文档以及编写类图等,规范的应用程序开发是非常重要的,同时本书还介绍了ASP.NET应用程序开发技巧,以便读者能够规范的、快速的编写...
本书在实例章节中,详细的介绍了如何进行规范的应用程序开发,包括设计需求分析文档以及编写类图等,规范的应用程序开发是非常重要的,同时本书还介绍了ASP.NET应用程序开发技巧,以便读者能够规范的、快速的编写...
本书在实例章节中,详细的介绍了如何进行规范的应用程序开发,包括设计需求分析文档以及编写类图等,规范的应用程序开发是非常重要的,同时本书还介绍了ASP.NET应用程序开发技巧,以便读者能够规范的、快速的编写...
1.5 ASP.NET应用程序基础 1.5.1 创建ASP.NET应用程序 1.5.2 运行ASP.NET应用程序 1.5.3 编译ASP.NET应用程序 1.6 小结 第2章 C# 3.0程序设计基础 2.1 C#程序 2.1.1 C#程序的结构 2.1.2 C# IDE的代码设置 2.2 变量 ...
书中介绍了C#的各种语言构造、.NET 2.0的类、核心API、公共中间语言(CIL)、动态程序集和ASP.NET扩展等内容;同时也介绍了.NET 3.0和.NET 3.5中新的编程API,包括WPF 、WCF和WF 的功能;另外,还介绍了最新的C# 3.0...
1.5 ASP.NET应用程序基础 1.5.1 创建ASP.NET应用程序 1.5.2 运行ASP.NET应用程序 1.5.3 编译ASP.NET应用程序 1.6 小结 第2章 C# 3.0程序设计基础 2.1 C#程序 2.1.1 C#程序的结构 2.1.2 C# IDE的代码设置 2.2 变量 ...
1.5 ASP.NET应用程序基础 1.5.1 创建ASP.NET应用程序 1.5.2 运行ASP.NET应用程序 1.5.3 编译ASP.NET应用程序 1.6 小结 第2章 C# 3.0程序设计基础 2.1 C#程序 2.1.1 C#程序的结构 2.1.2 C# IDE的代码设置 2.2 变量 ...
书中介绍了C# 的各种语言构造、.NET 2.0 的类、核心API 、公共中间语言(CIL)、动态程序集和ASP.NET 扩展等内容;同时也介绍了.NET 3.0 和.NET 4 中新的编程API,包括WPF 、WCF 和WF 的功能;另外,还介绍了最新的...
《C#与.NET 3.5高级程序设计》(第4版)是C#领域久负盛名的经典著作,深入全面地叙述了C#编程语言和.NET平台核心,并以大量示例剖析相关概念。书中介绍了C#的各种语言构造、.NET 2.0的类、核心API、公共中间语言(CIL)...
11.2.4 一个高质量的缩略图水印模块 133 11.3 使用第三方组件生成缩略图水印 136 11.3.1 “AspJpeg”组件的功能特点 136 11.3.2 在项目中添加“AspJpeg”组件 137 11.3.3 使用“AspNetImage”组件生成缩略...
《C#与.NET3.5高级程序设计(第4版)》是C#领域久负盛名的经典著作,深入全面地叙述了C#编程语言和.NET平台核心,并以大量示例剖析相关概念。书中介绍了C#的各种语言构造、.NET2.0的类、核心API、公共中间语言(CIL)、...
书中介绍了C# 的各种语言构造、.NET 2.0 的类、核心API、公共中间语言(CIL)、动态程序集和ASP.NET 扩展等内容;同时也介绍了.NET 3.0 中的新的编程API 包括WPF、WCF 和WF 的功能;另外,还介绍了最新的C# 3.0 编程...