Here you will find .Net source code, for ASP.Net, WinForms, WCF, Unit Testing, Security and more...
If you find this code useful, please visit our sponsers who help keep this site running.
Tip: Turn off line numbers when using Copy and Paste.
1using System; 2using System.Net; 3using System.Security.Principal; 4using System.ServiceModel; 5using Enterprise.UI.ServiceProxy; 6 7namespace Enterprise.Service.Gateway 8{ 9 10 /// <!-- 11 /// Code Courtesy Of http://www.purecoding.net 12 /// Permissions : Free for general use 13 /// Keywords: WCF, c#, .net 14 /// Filename: ServiceAccess.cs 15 /// --> 16 /// <summary> 17 /// This is a wrapper around a WCF service proxy. 18 /// Intended as the single point for controlling the access to 19 /// the service. 20 /// Benefit is we can control credentials and configuration. 21 /// It also will avoid creating multiple Service Clients. 22 /// </summary> 23 public class ServiceAccess : IDisposable 24 { 25 26 #region Singleton 27 28 private static readonly object _ThreadLock = new object(); 29 private static ServiceAccess _ServiceAccess; 30 private static EnterpriseServiceClient _Service; //The generated proxy client name goes here. 31 32 private ServiceAccess() 33 { 34 } 35 36 public static ServiceAccess Instance 37 { 38 get 39 { 40 lock (_ThreadLock) 41 { 42 if (_ServiceAccess == null) 43 { 44 _ServiceAccess = new ServiceAccess(); 45 } 46 } 47 return _ServiceAccess; 48 } 49 } 50 51 #endregion 52 53 public EnterpriseServiceClient Client 54 { 55 get 56 { 57 lock (_ThreadLock) 58 { 59 if (_Service == null) 60 { 61 _Service = new EnterpriseServiceClient(); 62 } 63 } 64 Open(); 65 return _Service; 66 } 67 } 68 69 70 71 #region IDisposable Members 72 73 public void Dispose() 74 { 75 Close(); 76 _Service = null; 77 _ServiceAccess = null; 78 } 79 80 #endregion 81 82 #region public methods 83 84 public EnterpriseService Open() 85 { 86 if (_Service.State != CommunicationState.Opened) 87 { 88 _Service.ChannelFactory.Credentials.SupportInteractive = false; 89 _Service.ClientCredentials.SupportInteractive = false; 90 _Service.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials; 91 92 _Service.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Delegation; 93 _Service.ChannelFactory.Credentials.Windows.AllowedImpersonationLevel = 94 TokenImpersonationLevel.Delegation; 95 96 _Service.Open(); 97 } 98 return _Service; 99 } 100 101 public void Close() 102 { 103 if (_Service != null && _Service.State == CommunicationState.Opened) 104 { 105 _Service.Close(); 106 } 107 } 108 109 #endregion 110 } 111} 112