1: [AttributeUsage(AttributeTargets.Class)]
2: public class ServiceMetadataBehaviorAttribute : Attribute, IServiceBehavior
3: {
4: //其他成员
5: private const string SingletonInstanceContextProviderType = "System.ServiceModel.Dispatcher.SingletonInstanceContextProvider,System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
6: private const string SyncMethodInvokerType = "System.ServiceModel.Dispatcher.SyncMethodInvoker,System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
7: private const string MessageOperationFormatterType = "System.ServiceModel.Dispatcher.MessageOperationFormatter, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
8:
9: private static void CreateHttpGetChannelDispatcher(ServiceHostBase host, Uri listenUri, MetadataSet metadata)
10: {
11: //创建Binding
12: TextMessageEncodingBindingElement messageEncodingElement = new TextMessageEncodingBindingElement() { MessageVersion = MessageVersion.None };
13: HttpTransportBindingElement transportElement = new HttpTransportBindingElement();
14: Utility.SetPropertyValue(transportElement, "Method", "GET");
15: Binding binding = new CustomBinding(messageEncodingElement, transportElement);
16:
17: //创建ChannelListener
18: IChannelListener listener = binding.BuildChannelListener<IReplyChannel>(listenUri, string.Empty, ListenUriMode.Explicit, new BindingParameterCollection());
19: ChannelDispatcher dispatcher = new ChannelDispatcher(listener, "ServiceMetadataBehaviorHttpGetBinding", binding) { MessageVersion = binding.MessageVersion };
20:
21: //创建EndpointDispatcher
22: EndpointDispatcher endpoint = new EndpointDispatcher(new EndpointAddress(listenUri), "IHttpGetMetadata", "http://www.artech.com/");
23:
24: //创建DispatchOperation,并设置DispatchMessageFormatter和OperationInvoker
25: DispatchOperation operation = new DispatchOperation(endpoint.DispatchRuntime, "Get", "*", "*");
26: operation.Formatter = Utility.CreateInstance<IDispatchMessageFormatter>(MessageOperationFormatterType, Type.EmptyTypes, new object[0]);
27: MethodInfo method = typeof(IHttpGetMetadata).GetMethod("Get");
28: operation.Invoker = Utility.CreateInstance<IOperationInvoker>(SyncMethodInvokerType, new Type[] { typeof(MethodInfo) }, new object[] { method });
29: endpoint.DispatchRuntime.Operations.Add(operation);
30:
31: //设置SingletonInstanceContext和InstanceContextProvider
32: MetadataProvisionService serviceInstance = new MetadataProvisionService(metadata);
33: endpoint.DispatchRuntime.SingletonInstanceContext = new InstanceContext(host, serviceInstance);
34: endpoint.DispatchRuntime.InstanceContextProvider = Utility.CreateInstance<IInstanceContextProvider>(SingletonInstanceContextProviderType, new Type[] { typeof(DispatchRuntime) }, new object[] { endpoint.DispatchRuntime });
35: dispatcher.Endpoints.Add(endpoint);
36:
37: //设置ContractFilter和AddressFilter
38: endpoint.ContractFilter = new MatchAllMessageFilter();
39: endpoint.AddressFilter = new MatchAllMessageFilter();
40:
41: host.ChannelDispatchers.Add(dispatcher);
42: }
43: }