[UPDATE: A demonstration of this code can be found in a follow-up post.]
My presentation this morning at CRMUG’s 10@10of10 went pretty well, I thought. There weren’t too many attendees, though. I can’t say I blame anyone for that. In my experience, very few jump with glee for a chance to look at code.
Since the format was limited to 10 minutes, I obviously had to truncate a great deal of the information I wanted to provide. It’s funny, because at first I wondered what I’d fill 10 minutes with. Those who attended will likely visit this space to retrieve what I promised: the code. So, without further ado:
The Code
<heavenly chorus>
using System; using System.Collections.Generic; using System.ComponentModel; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Description; using System.ServiceModel.Dispatcher; using System.Runtime.Serialization; using System.Xml; namespace CrmSDK { public partial class DynamicEntity { private Dictionary<string, Property> namedProperties; [System.Xml.Serialization.XmlIgnore] public Dictionary<string, Property> NamedProperties { get { return namedProperties; } set { namedProperties = value; this.RaisePropertyChanged("NamedProperties"); } } public DynamicEntity() { this.PropertyChanged += new PropertyChangedEventHandler(dynamicEntity_PropertyChanged); } private void dynamicEntity_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "Properties") { NamedProperties = convertPropertyArray(this.Properties); } } private static Dictionary<string, Property> convertPropertyArray(Property[] properties) { Dictionary<string, Property> propertyDictionary = new Dictionary<string, Property>(); for (int propertyIndex = 0; propertyIndex < properties.Length; propertyIndex++) propertyDictionary.Add(properties[propertyIndex].Name, properties[propertyIndex]); return propertyDictionary; } } public class CrmServiceConnectionParams { public String Scheme { get; set; } public String Url { get; set; } public CrmAuthenticationToken AuthenticationToken { get; set; } public BasicHttpSecurityMode SecurityMode { get; set; } private void setSecurityModeFromScheme() { switch (Scheme) { case "https": SecurityMode = BasicHttpSecurityMode.Transport; break; default: SecurityMode = BasicHttpSecurityMode.None; break; } } public CrmServiceConnectionParams(String url, CrmAuthenticationToken token) { if (url.Contains("://")) { string[] urlSplit = url.Split(new string[] { "://" }, StringSplitOptions.None); Scheme = urlSplit[0]; Url = urlSplit[1]; } else throw new ArgumentException("Failure creating CrmServiceConnectionParams instance. Invalid or missing URL scheme (e.g. 'http://')."); AuthenticationToken = token; setSecurityModeFromScheme(); } public CrmServiceConnectionParams(String scheme, String url, CrmAuthenticationToken token) { Scheme = scheme; Url = url; AuthenticationToken = token; setSecurityModeFromScheme(); } public CrmServiceConnectionParams(String scheme, String url, CrmAuthenticationToken token, BasicHttpSecurityMode securityMode) { Scheme = scheme; Url = url; AuthenticationToken = token; SecurityMode = securityMode; } } public class CrmServiceInstance { private CrmServiceConnectionParams connectionParams; public CrmServiceConnectionParams ConnectionParams { get { return connectionParams; } set { connectionParams = value; spawnCrmService(); } } private CrmServiceSoapClient crmService; public CrmServiceSoapClient CrmService { get { return crmService; } set { crmService = value; isCrmServiceReady = true; if (OnCrmServiceReady != null) OnCrmServiceReady(this, new EventArgs()); } } #region OnCrmServiceReady Event public event EventHandler<EventArgs> OnCrmServiceReady; private bool isCrmServiceReady; public bool IsCrmServiceReady { get { return isCrmServiceReady; } }
No comments:
Post a Comment