ChannelFactory.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System.Configuration;
  7. using System.Runtime;
  8. using System.ServiceModel.Channels;
  9. using System.ServiceModel.Description;
  10. using System.ServiceModel.Diagnostics;
  11. using System.ServiceModel.Diagnostics.Application;
  12. using System.ServiceModel.Security;
  13. using SecurityToken = System.IdentityModel.Tokens.SecurityToken;
  14. using FederatedClientCredentialsParameters = System.IdentityModel.Protocols.WSTrust.FederatedClientCredentialsParameters;
  15. public abstract class ChannelFactory : CommunicationObject, IChannelFactory, IDisposable
  16. {
  17. string configurationName;
  18. IChannelFactory innerFactory;
  19. ServiceEndpoint serviceEndpoint;
  20. ClientCredentials readOnlyClientCredentials;
  21. object openLock = new object();
  22. //Overload for activation DuplexChannelFactory
  23. protected ChannelFactory()
  24. : base()
  25. {
  26. TraceUtility.SetEtwProviderId();
  27. this.TraceOpenAndClose = true;
  28. }
  29. public ClientCredentials Credentials
  30. {
  31. get
  32. {
  33. if (this.Endpoint == null)
  34. return null;
  35. if (this.State == CommunicationState.Created || this.State == CommunicationState.Opening)
  36. {
  37. return EnsureCredentials(this.Endpoint);
  38. }
  39. else
  40. {
  41. if (this.readOnlyClientCredentials == null)
  42. {
  43. ClientCredentials c = new ClientCredentials();
  44. c.MakeReadOnly();
  45. this.readOnlyClientCredentials = c;
  46. }
  47. return this.readOnlyClientCredentials;
  48. }
  49. }
  50. }
  51. protected override TimeSpan DefaultCloseTimeout
  52. {
  53. get
  54. {
  55. if (this.Endpoint != null && this.Endpoint.Binding != null)
  56. {
  57. return this.Endpoint.Binding.CloseTimeout;
  58. }
  59. else
  60. {
  61. return ServiceDefaults.CloseTimeout;
  62. }
  63. }
  64. }
  65. protected override TimeSpan DefaultOpenTimeout
  66. {
  67. get
  68. {
  69. if (this.Endpoint != null && this.Endpoint.Binding != null)
  70. {
  71. return this.Endpoint.Binding.OpenTimeout;
  72. }
  73. else
  74. {
  75. return ServiceDefaults.OpenTimeout;
  76. }
  77. }
  78. }
  79. public ServiceEndpoint Endpoint
  80. {
  81. get
  82. {
  83. return this.serviceEndpoint;
  84. }
  85. }
  86. internal IChannelFactory InnerFactory
  87. {
  88. get { return this.innerFactory; }
  89. }
  90. // This boolean is used to determine if we should read ahead by a single
  91. // Message for IDuplexSessionChannels in order to detect null and
  92. // autoclose the underlying channel in that case.
  93. // Currently only accessed from the Send activity.
  94. [Fx.Tag.FriendAccessAllowed("System.ServiceModel.Activities")]
  95. internal bool UseActiveAutoClose
  96. {
  97. get;
  98. set;
  99. }
  100. protected internal void EnsureOpened()
  101. {
  102. base.ThrowIfDisposed();
  103. if (this.State != CommunicationState.Opened)
  104. {
  105. lock (this.openLock)
  106. {
  107. if (this.State != CommunicationState.Opened)
  108. {
  109. this.Open();
  110. }
  111. }
  112. }
  113. }
  114. // configurationName can be:
  115. // 1. null: don't bind any per-endpoint config (load common behaviors only)
  116. // 2. "*" (wildcard): match any endpoint config provided there's exactly 1
  117. // 3. anything else (including ""): match the endpoint config with the same name
  118. protected virtual void ApplyConfiguration(string configurationName)
  119. {
  120. this.ApplyConfiguration(configurationName, null);
  121. }
  122. void ApplyConfiguration(string configurationName, System.Configuration.Configuration configuration)
  123. {
  124. if (this.Endpoint == null)
  125. {
  126. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxChannelFactoryCannotApplyConfigurationWithoutEndpoint)));
  127. }
  128. if (!this.Endpoint.IsFullyConfigured)
  129. {
  130. ConfigLoader configLoader;
  131. if (configuration != null)
  132. {
  133. configLoader = new ConfigLoader(configuration.EvaluationContext);
  134. }
  135. else
  136. {
  137. configLoader = new ConfigLoader();
  138. }
  139. if (configurationName == null)
  140. {
  141. configLoader.LoadCommonClientBehaviors(this.Endpoint);
  142. }
  143. else
  144. {
  145. configLoader.LoadChannelBehaviors(this.Endpoint, configurationName);
  146. }
  147. }
  148. }
  149. protected abstract ServiceEndpoint CreateDescription();
  150. internal EndpointAddress CreateEndpointAddress(ServiceEndpoint endpoint)
  151. {
  152. if (endpoint.Address == null)
  153. {
  154. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxChannelFactoryEndpointAddressUri)));
  155. }
  156. return endpoint.Address;
  157. }
  158. protected virtual IChannelFactory CreateFactory()
  159. {
  160. if (this.Endpoint == null)
  161. {
  162. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxChannelFactoryCannotCreateFactoryWithoutDescription)));
  163. }
  164. if (this.Endpoint.Binding == null)
  165. {
  166. if (this.configurationName != null)
  167. {
  168. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxChannelFactoryNoBindingFoundInConfig1, configurationName)));
  169. }
  170. else
  171. {
  172. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxChannelFactoryNoBindingFoundInConfigOrCode)));
  173. }
  174. }
  175. return ServiceChannelFactory.BuildChannelFactory(this.Endpoint, this.UseActiveAutoClose);
  176. }
  177. void IDisposable.Dispose()
  178. {
  179. this.Close();
  180. }
  181. void EnsureSecurityCredentialsManager(ServiceEndpoint endpoint)
  182. {
  183. Fx.Assert(this.State == CommunicationState.Created || this.State == CommunicationState.Opening, "");
  184. if (endpoint.Behaviors.Find<SecurityCredentialsManager>() == null)
  185. {
  186. endpoint.Behaviors.Add(new ClientCredentials());
  187. }
  188. }
  189. ClientCredentials EnsureCredentials(ServiceEndpoint endpoint)
  190. {
  191. Fx.Assert(this.State == CommunicationState.Created || this.State == CommunicationState.Opening, "");
  192. ClientCredentials c = endpoint.Behaviors.Find<ClientCredentials>();
  193. if (c == null)
  194. {
  195. c = new ClientCredentials();
  196. endpoint.Behaviors.Add(c);
  197. }
  198. return c;
  199. }
  200. public T GetProperty<T>() where T : class
  201. {
  202. if (this.innerFactory != null)
  203. {
  204. return this.innerFactory.GetProperty<T>();
  205. }
  206. else
  207. {
  208. return null;
  209. }
  210. }
  211. internal bool HasDuplexOperations()
  212. {
  213. OperationDescriptionCollection operations = this.Endpoint.Contract.Operations;
  214. for (int i = 0; i < operations.Count; i++)
  215. {
  216. OperationDescription operation = operations[i];
  217. if (operation.IsServerInitiated())
  218. {
  219. return true;
  220. }
  221. }
  222. return false;
  223. }
  224. protected void InitializeEndpoint(string configurationName, EndpointAddress address)
  225. {
  226. this.serviceEndpoint = this.CreateDescription();
  227. ServiceEndpoint serviceEndpointFromConfig = null;
  228. if (configurationName != null)
  229. {
  230. serviceEndpointFromConfig = ConfigLoader.LookupEndpoint(configurationName, address, this.serviceEndpoint.Contract);
  231. }
  232. if (serviceEndpointFromConfig != null)
  233. {
  234. this.serviceEndpoint = serviceEndpointFromConfig;
  235. }
  236. else
  237. {
  238. if (address != null)
  239. {
  240. this.Endpoint.Address = address;
  241. }
  242. ApplyConfiguration(configurationName);
  243. }
  244. this.configurationName = configurationName;
  245. EnsureSecurityCredentialsManager(this.serviceEndpoint);
  246. }
  247. internal void InitializeEndpoint(string configurationName, EndpointAddress address, System.Configuration.Configuration configuration)
  248. {
  249. this.serviceEndpoint = this.CreateDescription();
  250. ServiceEndpoint serviceEndpointFromConfig = null;
  251. if (configurationName != null)
  252. {
  253. serviceEndpointFromConfig = ConfigLoader.LookupEndpoint(configurationName, address, this.serviceEndpoint.Contract, configuration.EvaluationContext);
  254. }
  255. if (serviceEndpointFromConfig != null)
  256. {
  257. this.serviceEndpoint = serviceEndpointFromConfig;
  258. }
  259. else
  260. {
  261. if (address != null)
  262. {
  263. this.Endpoint.Address = address;
  264. }
  265. ApplyConfiguration(configurationName, configuration);
  266. }
  267. this.configurationName = configurationName;
  268. EnsureSecurityCredentialsManager(this.serviceEndpoint);
  269. }
  270. protected void InitializeEndpoint(ServiceEndpoint endpoint)
  271. {
  272. if (endpoint == null)
  273. {
  274. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("endpoint");
  275. }
  276. this.serviceEndpoint = endpoint;
  277. ApplyConfiguration(null);
  278. EnsureSecurityCredentialsManager(this.serviceEndpoint);
  279. }
  280. protected void InitializeEndpoint(Binding binding, EndpointAddress address)
  281. {
  282. this.serviceEndpoint = this.CreateDescription();
  283. if (binding != null)
  284. {
  285. this.Endpoint.Binding = binding;
  286. }
  287. if (address != null)
  288. {
  289. this.Endpoint.Address = address;
  290. }
  291. ApplyConfiguration(null);
  292. EnsureSecurityCredentialsManager(this.serviceEndpoint);
  293. }
  294. protected override void OnOpened()
  295. {
  296. // if a client credentials has been configured cache a readonly snapshot of it
  297. if (this.Endpoint != null)
  298. {
  299. ClientCredentials credentials = this.Endpoint.Behaviors.Find<ClientCredentials>();
  300. if (credentials != null)
  301. {
  302. ClientCredentials credentialsCopy = credentials.Clone();
  303. credentialsCopy.MakeReadOnly();
  304. this.readOnlyClientCredentials = credentialsCopy;
  305. }
  306. }
  307. base.OnOpened();
  308. }
  309. protected override void OnAbort()
  310. {
  311. if (this.innerFactory != null)
  312. {
  313. this.innerFactory.Abort();
  314. }
  315. }
  316. protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
  317. {
  318. return new CloseAsyncResult(this.innerFactory, timeout, callback, state);
  319. }
  320. protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
  321. {
  322. return new OpenAsyncResult(this.innerFactory, timeout, callback, state);
  323. }
  324. protected override void OnClose(TimeSpan timeout)
  325. {
  326. if (this.innerFactory != null)
  327. this.innerFactory.Close(timeout);
  328. }
  329. protected override void OnEndClose(IAsyncResult result)
  330. {
  331. CloseAsyncResult.End(result);
  332. }
  333. protected override void OnEndOpen(IAsyncResult result)
  334. {
  335. OpenAsyncResult.End(result);
  336. }
  337. protected override void OnOpen(TimeSpan timeout)
  338. {
  339. this.innerFactory.Open(timeout);
  340. }
  341. protected override void OnOpening()
  342. {
  343. base.OnOpening();
  344. this.innerFactory = CreateFactory();
  345. if (TD.ChannelFactoryCreatedIsEnabled())
  346. {
  347. TD.ChannelFactoryCreated(this);
  348. }
  349. if (this.innerFactory == null)
  350. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.InnerChannelFactoryWasNotSet)));
  351. }
  352. class OpenAsyncResult : AsyncResult
  353. {
  354. ICommunicationObject communicationObject;
  355. static AsyncCallback onOpenComplete = Fx.ThunkCallback(new AsyncCallback(OnOpenComplete));
  356. public OpenAsyncResult(ICommunicationObject communicationObject, TimeSpan timeout, AsyncCallback callback, object state)
  357. : base(callback, state)
  358. {
  359. this.communicationObject = communicationObject;
  360. if (this.communicationObject == null)
  361. {
  362. this.Complete(true);
  363. return;
  364. }
  365. IAsyncResult result = this.communicationObject.BeginOpen(timeout, onOpenComplete, this);
  366. if (result.CompletedSynchronously)
  367. {
  368. this.communicationObject.EndOpen(result);
  369. this.Complete(true);
  370. }
  371. }
  372. static void OnOpenComplete(IAsyncResult result)
  373. {
  374. if (result.CompletedSynchronously)
  375. return;
  376. OpenAsyncResult thisPtr = (OpenAsyncResult)result.AsyncState;
  377. Exception exception = null;
  378. try
  379. {
  380. thisPtr.communicationObject.EndOpen(result);
  381. }
  382. catch (Exception e)
  383. {
  384. if (Fx.IsFatal(e))
  385. {
  386. throw;
  387. }
  388. exception = e;
  389. }
  390. thisPtr.Complete(false, exception);
  391. }
  392. public static void End(IAsyncResult result)
  393. {
  394. AsyncResult.End<OpenAsyncResult>(result);
  395. }
  396. }
  397. class CloseAsyncResult : AsyncResult
  398. {
  399. ICommunicationObject communicationObject;
  400. static AsyncCallback onCloseComplete = Fx.ThunkCallback(new AsyncCallback(OnCloseComplete));
  401. public CloseAsyncResult(ICommunicationObject communicationObject, TimeSpan timeout, AsyncCallback callback, object state)
  402. : base(callback, state)
  403. {
  404. this.communicationObject = communicationObject;
  405. if (this.communicationObject == null)
  406. {
  407. this.Complete(true);
  408. return;
  409. }
  410. IAsyncResult result = this.communicationObject.BeginClose(timeout, onCloseComplete, this);
  411. if (result.CompletedSynchronously)
  412. {
  413. this.communicationObject.EndClose(result);
  414. this.Complete(true);
  415. }
  416. }
  417. static void OnCloseComplete(IAsyncResult result)
  418. {
  419. if (result.CompletedSynchronously)
  420. return;
  421. CloseAsyncResult thisPtr = (CloseAsyncResult)result.AsyncState;
  422. Exception exception = null;
  423. try
  424. {
  425. thisPtr.communicationObject.EndClose(result);
  426. }
  427. catch (Exception e)
  428. {
  429. if (Fx.IsFatal(e))
  430. {
  431. throw;
  432. }
  433. exception = e;
  434. }
  435. thisPtr.Complete(false, exception);
  436. }
  437. public static void End(IAsyncResult result)
  438. {
  439. AsyncResult.End<CloseAsyncResult>(result);
  440. }
  441. }
  442. }
  443. public class ChannelFactory<TChannel> : ChannelFactory, IChannelFactory<TChannel>
  444. {
  445. InstanceContext callbackInstance;
  446. Type channelType;
  447. TypeLoader typeLoader;
  448. Type callbackType;
  449. //Overload for activation DuplexChannelFactory
  450. protected ChannelFactory(Type channelType)
  451. : base()
  452. {
  453. if (channelType == null)
  454. {
  455. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("channelType");
  456. }
  457. if (!channelType.IsInterface)
  458. {
  459. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxChannelFactoryTypeMustBeInterface)));
  460. }
  461. this.channelType = channelType;
  462. }
  463. // TChannel provides ContractDescription
  464. public ChannelFactory()
  465. : this(typeof(TChannel))
  466. {
  467. using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity() : null)
  468. {
  469. if (DiagnosticUtility.ShouldUseActivity)
  470. {
  471. ServiceModelActivity.Start(activity, SR.GetString(SR.ActivityConstructChannelFactory, typeof(TChannel).FullName), ActivityType.Construct);
  472. }
  473. this.InitializeEndpoint((string)null, null);
  474. }
  475. }
  476. // TChannel provides ContractDescription, attr/config [TChannel,name] provides Address,Binding
  477. public ChannelFactory(string endpointConfigurationName)
  478. : this(endpointConfigurationName, null)
  479. {
  480. }
  481. // TChannel provides ContractDescription, attr/config [TChannel,name] provides Binding, provide Address explicitly
  482. public ChannelFactory(string endpointConfigurationName, EndpointAddress remoteAddress)
  483. : this(typeof(TChannel))
  484. {
  485. using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity() : null)
  486. {
  487. if (DiagnosticUtility.ShouldUseActivity)
  488. {
  489. ServiceModelActivity.Start(activity, SR.GetString(SR.ActivityConstructChannelFactory, typeof(TChannel).FullName), ActivityType.Construct);
  490. }
  491. if (endpointConfigurationName == null)
  492. {
  493. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("endpointConfigurationName");
  494. }
  495. this.InitializeEndpoint(endpointConfigurationName, remoteAddress);
  496. }
  497. }
  498. // TChannel provides ContractDescription, attr/config [TChannel,name] provides Address,Binding
  499. public ChannelFactory(Binding binding)
  500. : this(binding, (EndpointAddress)null)
  501. {
  502. }
  503. public ChannelFactory(Binding binding, String remoteAddress)
  504. : this(binding, new EndpointAddress(remoteAddress))
  505. {
  506. }
  507. // TChannel provides ContractDescription, provide Address,Binding explicitly
  508. public ChannelFactory(Binding binding, EndpointAddress remoteAddress)
  509. : this(typeof(TChannel))
  510. {
  511. using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity() : null)
  512. {
  513. if (DiagnosticUtility.ShouldUseActivity)
  514. {
  515. ServiceModelActivity.Start(activity, SR.GetString(SR.ActivityConstructChannelFactory, typeof(TChannel).FullName), ActivityType.Construct);
  516. }
  517. if (binding == null)
  518. {
  519. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("binding");
  520. }
  521. this.InitializeEndpoint(binding, remoteAddress);
  522. }
  523. }
  524. // provide ContractDescription,Address,Binding explicitly
  525. public ChannelFactory(ServiceEndpoint endpoint)
  526. : this(typeof(TChannel))
  527. {
  528. using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity() : null)
  529. {
  530. if (DiagnosticUtility.ShouldUseActivity)
  531. {
  532. ServiceModelActivity.Start(activity, SR.GetString(SR.ActivityConstructChannelFactory, typeof(TChannel).FullName), ActivityType.Construct);
  533. }
  534. if (endpoint == null)
  535. {
  536. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("endpoint");
  537. }
  538. this.InitializeEndpoint(endpoint);
  539. }
  540. }
  541. internal InstanceContext CallbackInstance
  542. {
  543. get { return this.callbackInstance; }
  544. set { this.callbackInstance = value; }
  545. }
  546. internal Type CallbackType
  547. {
  548. get { return this.callbackType; }
  549. set { this.callbackType = value; }
  550. }
  551. internal ServiceChannelFactory ServiceChannelFactory
  552. {
  553. get { return (ServiceChannelFactory)InnerFactory; }
  554. }
  555. internal TypeLoader TypeLoader
  556. {
  557. get
  558. {
  559. if (this.typeLoader == null)
  560. {
  561. this.typeLoader = new TypeLoader();
  562. }
  563. return this.typeLoader;
  564. }
  565. }
  566. internal override string CloseActivityName
  567. {
  568. get { return SR.GetString(SR.ActivityCloseChannelFactory, typeof(TChannel).FullName); }
  569. }
  570. internal override string OpenActivityName
  571. {
  572. get { return SR.GetString(SR.ActivityOpenChannelFactory, typeof(TChannel).FullName); }
  573. }
  574. internal override ActivityType OpenActivityType
  575. {
  576. get { return ActivityType.OpenClient; }
  577. }
  578. public TChannel CreateChannel(EndpointAddress address)
  579. {
  580. if (address == null)
  581. {
  582. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("address");
  583. }
  584. return CreateChannel(address, address.Uri);
  585. }
  586. public virtual TChannel CreateChannel(EndpointAddress address, Uri via)
  587. {
  588. bool traceOpenAndClose = this.TraceOpenAndClose;
  589. try
  590. {
  591. using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity && this.TraceOpenAndClose ? ServiceModelActivity.CreateBoundedActivity() : null)
  592. {
  593. if (DiagnosticUtility.ShouldUseActivity)
  594. {
  595. ServiceModelActivity.Start(activity, this.OpenActivityName, this.OpenActivityType);
  596. // Turn open and close off for this open on contained objects.
  597. this.TraceOpenAndClose = false;
  598. }
  599. if (address == null)
  600. {
  601. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("address");
  602. }
  603. if (this.HasDuplexOperations())
  604. {
  605. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxCreateNonDuplexChannel1, this.Endpoint.Contract.Name)));
  606. }
  607. EnsureOpened();
  608. return (TChannel)this.ServiceChannelFactory.CreateChannel(typeof(TChannel), address, via);
  609. }
  610. }
  611. finally
  612. {
  613. this.TraceOpenAndClose = traceOpenAndClose;
  614. }
  615. }
  616. public TChannel CreateChannel()
  617. {
  618. return CreateChannel(this.CreateEndpointAddress(this.Endpoint), null);
  619. }
  620. public TChannel CreateChannelWithIssuedToken(SecurityToken issuedToken)
  621. {
  622. TChannel channel = this.CreateChannel();
  623. FederatedClientCredentialsParameters parameters = new FederatedClientCredentialsParameters();
  624. parameters.IssuedSecurityToken = issuedToken;
  625. ((IChannel)channel).GetProperty<ChannelParameterCollection>().Add(parameters);
  626. return channel;
  627. }
  628. public TChannel CreateChannelWithIssuedToken(SecurityToken issuedToken, EndpointAddress address)
  629. {
  630. TChannel channel = this.CreateChannel(address);
  631. FederatedClientCredentialsParameters parameters = new FederatedClientCredentialsParameters();
  632. parameters.IssuedSecurityToken = issuedToken;
  633. ((IChannel)channel).GetProperty<ChannelParameterCollection>().Add(parameters);
  634. return channel;
  635. }
  636. public TChannel CreateChannelWithIssuedToken(SecurityToken issuedToken, EndpointAddress address, Uri via)
  637. {
  638. TChannel channel = this.CreateChannel(address, via);
  639. FederatedClientCredentialsParameters parameters = new FederatedClientCredentialsParameters();
  640. parameters.IssuedSecurityToken = issuedToken;
  641. ((IChannel)channel).GetProperty<ChannelParameterCollection>().Add(parameters);
  642. return channel;
  643. }
  644. public TChannel CreateChannelWithActAsToken(SecurityToken actAsToken)
  645. {
  646. TChannel channel = this.CreateChannel();
  647. FederatedClientCredentialsParameters parameters = new FederatedClientCredentialsParameters();
  648. parameters.ActAs = actAsToken;
  649. ((IChannel)channel).GetProperty<ChannelParameterCollection>().Add(parameters);
  650. return channel;
  651. }
  652. public TChannel CreateChannelWithActAsToken(SecurityToken actAsToken, EndpointAddress address)
  653. {
  654. TChannel channel = this.CreateChannel(address);
  655. FederatedClientCredentialsParameters parameters = new FederatedClientCredentialsParameters();
  656. parameters.ActAs = actAsToken;
  657. ((IChannel)channel).GetProperty<ChannelParameterCollection>().Add(parameters);
  658. return channel;
  659. }
  660. public TChannel CreateChannelWithActAsToken(SecurityToken actAsToken, EndpointAddress address, Uri via)
  661. {
  662. TChannel channel = this.CreateChannel(address, via);
  663. FederatedClientCredentialsParameters parameters = new FederatedClientCredentialsParameters();
  664. parameters.ActAs = actAsToken;
  665. ((IChannel)channel).GetProperty<ChannelParameterCollection>().Add(parameters);
  666. return channel;
  667. }
  668. public TChannel CreateChannelWithOnBehalfOfToken(SecurityToken onBehalfOf)
  669. {
  670. TChannel channel = this.CreateChannel();
  671. FederatedClientCredentialsParameters parameters = new FederatedClientCredentialsParameters();
  672. parameters.OnBehalfOf = onBehalfOf;
  673. ((IChannel)channel).GetProperty<ChannelParameterCollection>().Add(parameters);
  674. return channel;
  675. }
  676. public TChannel CreateChannelWithOnBehalfOfToken(SecurityToken onBehalfOf, EndpointAddress address)
  677. {
  678. TChannel channel = this.CreateChannel(address);
  679. FederatedClientCredentialsParameters parameters = new FederatedClientCredentialsParameters();
  680. parameters.OnBehalfOf = onBehalfOf;
  681. ((IChannel)channel).GetProperty<ChannelParameterCollection>().Add(parameters);
  682. return channel;
  683. }
  684. public TChannel CreateChannelWithOnBehalfOfToken(SecurityToken onBehalfOf, EndpointAddress address, Uri via)
  685. {
  686. TChannel channel = this.CreateChannel(address, via);
  687. FederatedClientCredentialsParameters parameters = new FederatedClientCredentialsParameters();
  688. parameters.OnBehalfOf = onBehalfOf;
  689. ((IChannel)channel).GetProperty<ChannelParameterCollection>().Add(parameters);
  690. return channel;
  691. }
  692. internal UChannel CreateChannel<UChannel>(EndpointAddress address)
  693. {
  694. EnsureOpened();
  695. return this.ServiceChannelFactory.CreateChannel<UChannel>(address);
  696. }
  697. internal UChannel CreateChannel<UChannel>(EndpointAddress address, Uri via)
  698. {
  699. EnsureOpened();
  700. return this.ServiceChannelFactory.CreateChannel<UChannel>(address, via);
  701. }
  702. internal bool CanCreateChannel<UChannel>()
  703. {
  704. EnsureOpened();
  705. return this.ServiceChannelFactory.CanCreateChannel<UChannel>();
  706. }
  707. protected override ServiceEndpoint CreateDescription()
  708. {
  709. ContractDescription contractDescription = this.TypeLoader.LoadContractDescription(this.channelType);
  710. ServiceEndpoint endpoint = new ServiceEndpoint(contractDescription);
  711. ReflectOnCallbackInstance(endpoint);
  712. this.TypeLoader.AddBehaviorsSFx(endpoint, channelType);
  713. return endpoint;
  714. }
  715. void ReflectOnCallbackInstance(ServiceEndpoint endpoint)
  716. {
  717. if (callbackType != null)
  718. {
  719. if (endpoint.Contract.CallbackContractType == null)
  720. {
  721. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SfxCallbackTypeCannotBeNull, endpoint.Contract.ContractType.FullName)));
  722. }
  723. this.TypeLoader.AddBehaviorsFromImplementationType(endpoint, callbackType);
  724. }
  725. else if (this.CallbackInstance != null && this.CallbackInstance.UserObject != null)
  726. {
  727. if (endpoint.Contract.CallbackContractType == null)
  728. {
  729. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SfxCallbackTypeCannotBeNull, endpoint.Contract.ContractType.FullName)));
  730. }
  731. object implementation = this.CallbackInstance.UserObject;
  732. Type implementationType = implementation.GetType();
  733. this.TypeLoader.AddBehaviorsFromImplementationType(endpoint, implementationType);
  734. IEndpointBehavior channelBehavior = implementation as IEndpointBehavior;
  735. if (channelBehavior != null)
  736. {
  737. endpoint.Behaviors.Add(channelBehavior);
  738. }
  739. IContractBehavior contractBehavior = implementation as IContractBehavior;
  740. if (contractBehavior != null)
  741. {
  742. endpoint.Contract.Behaviors.Add(contractBehavior);
  743. }
  744. }
  745. }
  746. //Static funtions to create channels
  747. protected static TChannel CreateChannel(String endpointConfigurationName)
  748. {
  749. ChannelFactory<TChannel> channelFactory = new ChannelFactory<TChannel>(endpointConfigurationName);
  750. if (channelFactory.HasDuplexOperations())
  751. {
  752. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInvalidStaticOverloadCalledForDuplexChannelFactory1, channelFactory.channelType.Name)));
  753. }
  754. TChannel channel = channelFactory.CreateChannel();
  755. SetFactoryToAutoClose(channel);
  756. return channel;
  757. }
  758. public static TChannel CreateChannel(Binding binding, EndpointAddress endpointAddress)
  759. {
  760. ChannelFactory<TChannel> channelFactory = new ChannelFactory<TChannel>(binding, endpointAddress);
  761. if (channelFactory.HasDuplexOperations())
  762. {
  763. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInvalidStaticOverloadCalledForDuplexChannelFactory1, channelFactory.channelType.Name)));
  764. }
  765. TChannel channel = channelFactory.CreateChannel();
  766. SetFactoryToAutoClose(channel);
  767. return channel;
  768. }
  769. public static TChannel CreateChannel(Binding binding, EndpointAddress endpointAddress, Uri via)
  770. {
  771. ChannelFactory<TChannel> channelFactory = new ChannelFactory<TChannel>(binding);
  772. if (channelFactory.HasDuplexOperations())
  773. {
  774. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInvalidStaticOverloadCalledForDuplexChannelFactory1, channelFactory.channelType.Name)));
  775. }
  776. TChannel channel = channelFactory.CreateChannel(endpointAddress, via);
  777. SetFactoryToAutoClose(channel);
  778. return channel;
  779. }
  780. internal static void SetFactoryToAutoClose(TChannel channel)
  781. {
  782. //Set the Channel to auto close its ChannelFactory.
  783. ServiceChannel serviceChannel = ServiceChannelFactory.GetServiceChannel(channel);
  784. serviceChannel.CloseFactory = true;
  785. }
  786. }
  787. }