3 // ServiceProxyGenerator.cs
6 // Atsushi Enomoto <atsushi@ximian.com>
8 // Copyright (C) 2009 Novell, Inc. http://www.novell.com
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
32 using System.Reflection;
33 using System.ServiceModel.Channels;
34 using System.ServiceModel.Description;
35 using System.ServiceModel.Dispatcher;
36 using Mono.CodeGeneration;
37 using System.ServiceModel.MonoInternal;
39 namespace System.ServiceModel
41 internal class ServiceProxyGenerator : ProxyGeneratorBase
43 public static Type CreateCallbackProxyType (DispatchRuntime dispatchRuntime, Type callbackType)
45 var ed = dispatchRuntime.EndpointDispatcher;
46 var channelDispatcher = ed.ChannelDispatcher;
47 Type contractType = channelDispatcher != null ? channelDispatcher.Host.ImplementedContracts.Values.First (hcd => hcd.Name == ed.ContractName && hcd.Namespace == ed.ContractNamespace).ContractType : dispatchRuntime.Type;
49 var cd = ContractDescriptionGenerator.GetCallbackContract (contractType, callbackType);
50 string modname = "dummy";
51 Type crtype = typeof (DuplexServiceRuntimeChannel);
53 // public class __clientproxy_MyContract : ClientRuntimeChannel, [ContractType]
54 CodeClass c = new CodeModule (modname).CreateClass (
55 "__callbackproxy_" + cd.Name,
57 new Type [] {callbackType});
60 // public __callbackproxy_MyContract (
61 // IChannel channel, DispatchRuntime runtime)
62 // : base (channel, runtime)
66 Type [] ctorargs = new Type [] {typeof (IChannel), typeof (DispatchRuntime)};
67 CodeMethod ctor = c.CreateConstructor (
68 MethodAttributes.Public, ctorargs);
69 CodeBuilder b = ctor.CodeBuilder;
70 MethodBase baseCtor = crtype.GetConstructors (
71 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) [0];
72 if (baseCtor == null) throw new Exception ("INTERNAL ERROR: DuplexServiceRuntimeChannel.ctor() was not found.");
76 new CodeArgumentReference (typeof (IChannel), 1, "arg0"),
77 new CodeArgumentReference (typeof (DispatchRuntime), 2, "arg1"));
79 return CreateProxyTypeOperations (crtype, c, cd);