uri class not shown in system namespace - c#

I'm trying to compile the following code using the .NET 4.5 Framework:
using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Discussion.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
class CodeReview
{
public List<CodeReviewComment> GetCodeReviewComments(int workItemId)
{
List<CodeReviewComment> comments = new List<CodeReviewComment>();
Uri tfsuri = new Uri(MYURL);
TeamFoundationDiscussionService service = new TeamFoundationDiscussionService();
service.Initialize(new Microsoft.TeamFoundation.Client.TfsTeamProjectCollection(tfsuri));
IDiscussionManager discussionManager = service.CreateDiscussionManager();
IAsyncResult result = discussionManager.BeginQueryByCodeReviewRequest(workItemId, QueryStoreOptions.ServerAndLocal, new AsyncCallback(CallCompletedCallback), null);
var output = discussionManager.EndQueryByCodeReviewRequest(result);
foreach (DiscussionThread thread in output)
{
if (thread.RootComment != null)
{
CodeReviewComment comment = new CodeReviewComment();
comment.Author = thread.RootComment.Author.DisplayName;
comment.Comment = thread.RootComment.Content;
comment.PublishDate = thread.RootComment.PublishedDate.ToShortDateString();
comment.ItemName = thread.ItemPath;
comments.Add(comment);
}
}
return comments;
}
static void CallCompletedCallback(IAsyncResult result)
{
// Handle error conditions here
}
public class CodeReviewComment
{
public string Author { get; set; }
public string Comment { get; set; }
public string PublishDate { get; set; }
public string ItemName { get; set; }
}
}
The Visual Studio compiler is complaining that "the type or namespace 'Uri' could not be found" and when I expand the System namespace in the Object Browser it doesnt list any of the Uri classes. I've tried a few other versions of .NET and still have the same problem although it does appear if I select the .NET 4.0 Client Profile but then none of the TeamFoundation classes are present in the Microsoft namespace.

I solved my own problem but I don't understand why it worked. In desperation I created another new Project using .NET 4.5 and copied and pasted the code over. It compiled and Uri shows up as a Class in the Object Browser. I have no idea what the difference between the 2 projects is.

Related

Azure Functions binding redirect

Is it possible to include a web.config or app.config file in the azure functions folder structure to allow assembly binding redirects?
Assuming you are using the latest (June'17) Visual Studio 2017 Function Tooling, I derived a somewhat-reasonable config-based solution for this following a snippet of code posted by npiasecki over on Issue #992.
It would be ideal if this were managed through the framework, but at least being configuration-driven you have a bit more change isolation. I suppose you could also use some pre-build steps or T4 templating that reconciles the versions of the nugets in the project (and their dependencies) before writing out this config or generating code.
So the downside..
.. becomes having to remember to update the BindingRedirects config when you update the NuGet package (this is often a problem in app.configs anyway). You may also have an issue with the config-driven solution if you need to redirect Newtonsoft.
In our case, we were using the new Azure Fluent NuGet that had a dependency on an older version of Microsoft.IdentityModel.Clients.ActiveDirectory than the version of the normal ARM management libraries which are used side-by-side in a particular Function.
local.settings.json
{
"IsEncrypted": false,
"Values": {
"BindingRedirects": "[ { \"ShortName\": \"Microsoft.IdentityModel.Clients.ActiveDirectory\", \"RedirectToVersion\": \"3.13.9.1126\", \"PublicKeyToken\": \"31bf3856ad364e35\" } ]"
}
}
FunctionUtilities.cs
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Globalization;
using System.Linq;
using System.Reflection;
namespace Utilities.AzureFunctions
{
public static class FunctionUtilities
{
public class BindingRedirect
{
public string ShortName { get; set; }
public string PublicKeyToken { get; set; }
public string RedirectToVersion { get; set; }
}
public static void ConfigureBindingRedirects()
{
var config = Environment.GetEnvironmentVariable("BindingRedirects");
var redirects = JsonConvert.DeserializeObject<List<BindingRedirect>>(config);
redirects.ForEach(RedirectAssembly);
}
public static void RedirectAssembly(BindingRedirect bindingRedirect)
{
ResolveEventHandler handler = null;
handler = (sender, args) =>
{
var requestedAssembly = new AssemblyName(args.Name);
if (requestedAssembly.Name != bindingRedirect.ShortName)
{
return null;
}
var targetPublicKeyToken = new AssemblyName("x, PublicKeyToken=" + bindingRedirect.PublicKeyToken)
.GetPublicKeyToken();
requestedAssembly.Version = new Version(bindingRedirect.RedirectToVersion);
requestedAssembly.SetPublicKeyToken(targetPublicKeyToken);
requestedAssembly.CultureInfo = CultureInfo.InvariantCulture;
AppDomain.CurrentDomain.AssemblyResolve -= handler;
return Assembly.Load(requestedAssembly);
};
AppDomain.CurrentDomain.AssemblyResolve += handler;
}
}
}
Just posted a new blog post explaining how to fix the problem, have a look:
https://codopia.wordpress.com/2017/07/21/how-to-fix-the-assembly-binding-redirect-problem-in-azure-functions/
It's actually a tweaked version of the JoeBrockhaus's code, that works well even for Newtonsoft.Json.dll
Inspired by the accepted answer I figured I'd do a more generic one which takes into account upgrades as well.
It fetches all assemblies, orders them descending to get the newest version on top, then returns the newest version on resolve. I call this in a static constructor myself.
public static void RedirectAssembly()
{
var list = AppDomain.CurrentDomain.GetAssemblies()
.Select(a => a.GetName())
.OrderByDescending(a => a.Name)
.ThenByDescending(a => a.Version)
.Select(a => a.FullName)
.ToList();
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
var requestedAssembly = new AssemblyName(args.Name);
foreach (string asmName in list)
{
if (asmName.StartsWith(requestedAssembly.Name + ","))
{
return Assembly.Load(asmName);
}
}
return null;
};
}
It is not directly possible today, but we are thinking about ways to achieve this. Can you please open an issue on https://github.com/Azure/azure-webjobs-sdk-script/issues to make sure your specific scenario is looked at? Thanks!
First SO post, so apologies if formatting's a bit off.
We've hit this issue a couple of times and managed to find a better way of getting the required redirects by forcing MSBUILD to generate a binding redirects file and then parsing that to be used with the previously suggested answer.
Modify the project settings and add in a couple of targets:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
...
<AutoGenerateBindingRedirects>True</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
...
</PropertyGroup>
</Project>
These classes apply the binding redirects using the same idea that was posted earlier (link) except instead of using the host.json file it reads from the generated binding redirects file. The filename to use is from reflection using the ExecutingAssembly.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;
public static class AssemblyBindingRedirectHelper
{
private static FunctionRedirectBindings _redirects;
public static void ConfigureBindingRedirects()
{
// Only load the binding redirects once
if (_redirects != null)
return;
_redirects = new FunctionRedirectBindings();
foreach (var redirect in _redirects.BindingRedirects)
{
RedirectAssembly(redirect);
}
}
public static void RedirectAssembly(BindingRedirect bindingRedirect)
{
ResolveEventHandler handler = null;
handler = (sender, args) =>
{
var requestedAssembly = new AssemblyName(args.Name);
if (requestedAssembly.Name != bindingRedirect.ShortName)
{
return null;
}
var targetPublicKeyToken = new AssemblyName("x, PublicKeyToken=" + bindingRedirect.PublicKeyToken).GetPublicKeyToken();
requestedAssembly.Version = new Version(bindingRedirect.RedirectToVersion);
requestedAssembly.SetPublicKeyToken(targetPublicKeyToken);
requestedAssembly.CultureInfo = CultureInfo.InvariantCulture;
AppDomain.CurrentDomain.AssemblyResolve -= handler;
return Assembly.Load(requestedAssembly);
};
AppDomain.CurrentDomain.AssemblyResolve += handler;
}
}
public class FunctionRedirectBindings
{
public HashSet<BindingRedirect> BindingRedirects { get; } = new HashSet<BindingRedirect>();
public FunctionRedirectBindings()
{
var assm = Assembly.GetExecutingAssembly();
var bindingRedirectFileName = $"{assm.GetName().Name}.dll.config";
var dir = Path.Combine(Environment.GetEnvironmentVariable("HOME"), #"site\wwwroot");
var fullPath = Path.Combine(dir, bindingRedirectFileName);
if(!File.Exists(fullPath))
throw new ArgumentException($"Could not find binding redirect file. Path:{fullPath}");
var xml = ReadFile<configuration>(fullPath);
TransformData(xml);
}
private T ReadFile<T>(string path)
{
using (StreamReader reader = new StreamReader(path))
{
var serializer = new XmlSerializer(typeof(T));
var obj = (T)serializer.Deserialize(reader);
reader.Close();
return obj;
}
}
private void TransformData(configuration xml)
{
foreach(var item in xml.runtime)
{
var br = new BindingRedirect
{
ShortName = item.dependentAssembly.assemblyIdentity.name,
PublicKeyToken = item.dependentAssembly.assemblyIdentity.publicKeyToken,
RedirectToVersion = item.dependentAssembly.bindingRedirect.newVersion
};
BindingRedirects.Add(br);
}
}
}
public class BindingRedirect
{
public string ShortName { get; set; }
public string PublicKeyToken { get; set; }
public string RedirectToVersion { get; set; }
}
Xml classes to use to deserialise the generated binding redirect file into something easier to use. These were generated from the binding redirects file by using VS2017 "paste special -> paste xml as classes" so feel free to roll your own if needed.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class configuration
{
[System.Xml.Serialization.XmlArrayItemAttribute("assemblyBinding", Namespace = "urn:schemas-microsoft-com:asm.v1", IsNullable = false)]
public assemblyBinding[] runtime { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:schemas-microsoft-com:asm.v1")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:schemas-microsoft-com:asm.v1", IsNullable = false)]
public partial class assemblyBinding
{
public assemblyBindingDependentAssembly dependentAssembly { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:schemas-microsoft-com:asm.v1")]
public partial class assemblyBindingDependentAssembly
{
public assemblyBindingDependentAssemblyAssemblyIdentity assemblyIdentity { get; set; }
public assemblyBindingDependentAssemblyBindingRedirect bindingRedirect { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:schemas-microsoft-com:asm.v1")]
public partial class assemblyBindingDependentAssemblyAssemblyIdentity
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute()]
public string publicKeyToken { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute()]
public string culture { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:schemas-microsoft-com:asm.v1")]
public partial class assemblyBindingDependentAssemblyBindingRedirect
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string oldVersion { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute()]
public string newVersion { get; set; }
}
Here's an alternate solution for when you want the exact version of a particular assembly. With this code, you can easily deploy the assemblies that are missing:
public static class AssemblyHelper
{
//--------------------------------------------------------------------------------
/// <summary>
/// Redirection hack because Azure functions don't support it.
/// How to use:
/// If you get an error that a certain version of a dll can't be found:
/// 1) deploy that particular dll in any project subfolder
/// 2) In your azure function static constructor, Call
/// AssemblyHelper.IncludeSupplementalDllsWhenBinding()
///
/// This will hook the binding calls and look for a matching dll anywhere
/// in the $HOME folder tree.
/// </summary>
//--------------------------------------------------------------------------------
public static void IncludeSupplementalDllsWhenBinding()
{
var searching = false;
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
// This prevents a stack overflow
if(searching) return null;
var requestedAssembly = new AssemblyName(args.Name);
searching = true;
Assembly foundAssembly = null;
try
{
foundAssembly = Assembly.Load(requestedAssembly);
}
catch(Exception e)
{
Debug.WriteLine($"Could not load assembly: {args.Name} because {e.Message}");
}
searching = false;
if(foundAssembly == null)
{
var home = Environment.GetEnvironmentVariable("HOME") ?? ".";
var possibleFiles = Directory.GetFiles(home, requestedAssembly.Name + ".dll", SearchOption.AllDirectories);
foreach (var file in possibleFiles)
{
var possibleAssembly = AssemblyName.GetAssemblyName(file);
if (possibleAssembly.Version == requestedAssembly.Version)
{
foundAssembly = Assembly.Load(possibleAssembly);
break;
}
}
}
return foundAssembly;
};
}
}

Loading Assemblies into a separate AppDomain in a WindowsAzure WorkerRole

I'm developing an Azure Worker Role that will need to periodically perform actions based on work pulled from a WCF service. Due to the nature of Azure deployments, a decision was made to implement a plugin framework in Azure to allow quick updates to our product while minimizing the downtime by removing the need for full azure deployments, as well as supporting multiple versions of various assemblies.
A plugin is downloaded from Cloud storage and loaded into a class that contains all relevant info needed to load the assembly. (See PluginAssemblyInfo below)
In other development work I've developed the below ReflectionHelper class. This class works in other applications and I've verified it works in a console application.
However when running in the Azure emulator I get the following error: (I've added a comment on the line the exception is being thrown on.)
Type is not resolved for member 'MyCompanyName.ReflectionHelper,MyCompanyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
The ReflectionHelper class is added as a source file to the same assembly that is calling it. (I did edit the name of the assembly/namespaces for anonymity reasons)
Given the knowledge that this class works in other cases, my only guess is that it may be a trust issue, but I've not found much info online relating to this. Any help would be greatly appreciated.
This is the code for the ReflectionHelper class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Serialization;
using System.Web;
namespace MyCompanyName
{
[Serializable]
public class ReflectionHelper
{
public AppDomain appDomain { get; set; }
public byte[] PluginBytes { get; set; }
public string PluginClassName { get; set; }
public string PluginAssemblyName { get; set; }
public Dictionary<string, byte[]> AssemblyArchive { get; set; }
public object GetInstance(AppDomain NewDomain, byte[] PluginBytes, string PluginClassName, string PluginAssemblyName, Dictionary<string, byte[]> AssemblyArchive)
{
try
{
this.appDomain = NewDomain;
this.PluginBytes = PluginBytes;
this.PluginClassName = PluginClassName;
this.AssemblyArchive = AssemblyArchive;
this.PluginAssemblyName = PluginAssemblyName;
//this is the line that throws the serializationexception
appDomain.AssemblyResolve += new ResolveEventHandler((sender, args) =>
{
AssemblyName x = new AssemblyName(args.Name);
string Name = x.Name;
if (System.IO.Path.GetExtension(x.Name) != ".dll")
Name += ".dll";
Assembly Ret = appDomain.Load(this.AssemblyArchive[Name]);
return Ret;
});
appDomain.DoCallBack(LoaderCallBack);
return appDomain.GetData("Plugin");
}
catch (Exception ex)
{
throw ex;
}
}
public void LoaderCallBack()
{
ObjectHandle PluginObject = appDomain.CreateInstance(string.Format(this.PluginAssemblyName), PluginClassName);
appDomain.SetData("Plugin", PluginObject.Unwrap());
}
}
}
This is the code snippet that invokes it
AppDomain currentDomain = AppDomain.CurrentDomain;
AppDomainSetup domainSetup = new AppDomainSetup() { };
AppDomain BrokerJobDomain = AppDomain.CreateDomain("WorkerPluginDomain" + Guid.NewGuid().ToString());//, null, new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase });
ReflectionHelper helper = new ReflectionHelper();
object Instance = helper.GetInstance(BrokerJobDomain, pluginAssembly.PluginBytes, pluginAssembly.ClassName, pluginAssembly.AssemblyName, pluginAssembly.AssemblyArchive);
IWorkPlugin executor = (IWorkPlugin)Instance;
This is the assembly info class that is populated when the plugin is downloaded from cloud storage
public class PluginAssemblyInfo
{
public string ClassName { get; set; }
public byte[] PluginBytes { get; set; }
public Dictionary<string, byte[]> AssemblyArchive { get; set; }
public string AssemblyName { get; set; }
public DateTime LoadDate { get; set; }
}
The issue seems to be related to the RelativeSearchPath not being set up on the child domain. Ensuring the child domain had the same setup credentials seemed to fix this.
AppDomain currentDomain = AppDomain.CurrentDomain;
AppDomain BrokerJobDomain = AppDomain.CreateDomain("WorkerPluginDomain" + Guid.NewGuid().ToString(), null, AppDomain.CurrentDomain.SetupInformation );

Mono does not honor System.Runtime.Serialization.DataMemberAttribute EmitDefaultValue setting

Tried the code in Visual Studio on Windows to be sure.
Mono framework does not appear to honor the EmitDefaultValue argument of the DataMemberAttribute. Using the following code:
using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
namespace MyApp
{
class MainClass
{
public static void Main (string[] args)
{
Cereal specialK = new Cereal();
specialK.TheValue="This is a what?";
var ser = new DataContractJsonSerializer(typeof(Cereal));
MemoryStream stm = new MemoryStream();
ser.WriteObject(stm, specialK);
string json = System.Text.Encoding.UTF8.GetString(stm.ToArray());
Console.WriteLine(json);
Console.ReadLine();
}
}
[DataContract]
class Cereal
{
[DataMember(Name="set_on_serialize")]
private string _setOnSerialize = string.Empty;
[DataMember(Name = "default_export", EmitDefaultValue = false)]
private string _default_null;
public Cereal() { }
[DataMember(Name = "out_value")]
public string TheValue
{
get;
set;
}
[OnSerializing]
void OnSerializing(StreamingContext content)
{
this._setOnSerialize = "A brick!";
}
}
}
The output in Mono results in:
{"default_export":null,"out_value":"This is a what?","set_on_serialize":""}
The default_export property is being exported as null but should not be output as it is the default value of type string.
Correct output from VS on Windows is:
{"out_value":"This is a what?","set_on_serialize":"A brick!"}
Is this a bug in Mono or am I missing something?
Apparently this feature is not yet implemented in mono. See the FIXME comment in the mono source code (line 197).

How to read signed request using c# 4.0

I am using c# 4.0 and I am integrating facebook registration plugin
Can somebody please tell me what I need to do to read signed request.
I have downloaded Facebook.dll and Newtonsoft.Json.dll
I also have valid
App ID: ***
API Key: **********
App Secret: *************
If possible please give me a sample code how should I pass these keyes and how I can collect the decoded data sent in form of signed request.
Thanks:
There must be more easy ways to read signed request following is what I am using. There are few steps involved to read facebook signed request in c#
Dot net 2010 is required to follow these steps. It is suggested if you make a new web based project named “fb” when you done then you may import this code to your real project.
Download source from http://facebooksdk.codeplex.com/SourceControl/changeset/view/f8109846cba5#Source%2fFacebook%2fFacebookApp.cs
After unzip you will get “facebooksdk-f8109846cba5” inside it you will find a folder facebooksdk-f8109846cba5\Source\Facebook in this folder look for “Facebook.csproj”
Open “Facebook.csproj” in vs 2010 Look for file “FacebookApp.cs” open this file and search “internal protected FacebookSignedRequest ParseSignedRequest(string signedRequestValue)
Change “internal protected” to “public”
Then build the project by right click on project. Now it’s complied file (“Facebook.dll”) is ready to use. Copy it to your project bin directory and add its reference.
Now download Json.Net 3.5 release 8 from http://json.codeplex.com/releases/view/50552 and add to your project bin folder and also add its reference.
Now you are ready to read signed request. It is time to write code to read signed request.
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Facebook;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Collections.Generic;
namespace fb
{
public partial class test3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FacebookApp fap = new FacebookApp();
fap.AppId = "************";
fap.AppSecret = "********************";
string requested_Data = Request.Form["signed_request"];
FacebookSignedRequest fsr = fap.ParseSignedRequest(requested_Data);
// string json = JsonConvert.SerializeObject(fsr.Dictionary, Formatting.Indented);
UserData ud = new UserData(fsr);
Response.Write(ud.name + "");
Response.Write(ud.birthday + "");
Response.Write(ud.country + "");
Response.Write(ud.email + "");
Response.Write(ud.gender + "");
Response.Write(ud.location + "");
Response.Write(ud.userId + "");
}
}
public class UserData
{
public UserData(FacebookSignedRequest fsr)
{
string value = string.Empty;
JObject o;
foreach (string key in fsr.Dictionary.Keys)
{
value = fsr.Dictionary[key];
switch (key)
{
case "user_id":
userId = value;
break;
case "registration":
o = JObject.Parse(value);
name = GetValue(o, "name");
birthday = GetValue(o, "birthday");
email = GetValue(o, "email");
gender = GetValue(o, "gender");
location = GetValue(o, "location.name");
break;
case "user":
o = JObject.Parse(value);
country = GetValue(o, "country");
break;
}
}
}
private string GetValue(JObject o, string token)
{
string ret = string.Empty;
try
{
ret = (string)o.SelectToken(token);
}
catch (Exception ex)
{
throw ex;
}
return ret;
}
public string name { get; set; }
public string birthday { get; set; }
public string gender { get; set; }
public string location { get; set; }
public string country { get; set; }
public string email { get; set; }
public string userId { get; set; }
}
}
This is what I am using and its working fine for me.
Here is using the FB C# SDK v.5.2.1.0
var signed_request = Request.Form["signed_request"];
var fap = Facebook.FacebookApplication.Current;
var signed_request_obj = Facebook.FacebookSignedRequest.Parse(fap, signed_request);
if (signed_request_obj != null)
{
JObject o = JObject.Parse(signed_request_obj.Data.ToString());
}

How do you upgrade Settings.settings when the stored data type changes?

I have an application that stores a collection of objects in the user settings, and is deployed via ClickOnce. The next version of the applications has a modified type for the objects stored. For example, the previous version's type was:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
And the new version's type is:
public class Person
{
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
}
Obviously, ApplicationSettingsBase.Upgrade wouldn't know how to perform an upgrade, since Age needs to be converted using (age) => DateTime.Now.AddYears(-age), so only the Name property would be upgraded, and DateOfBirth would just have the value of Default(DateTime).
So I'd like to provide an upgrade routine, by overriding ApplicationSettingsBase.Upgrade, that would convert the values as needed. But I've ran into three problems:
When trying to access the previous version's value using ApplicationSettingsBase.GetPreviousVersion, the returned value would be an object of the current version, which doesn't have the Age property and has an empty DateOfBirth property (since it can't deserialize Age into DateOfBirth).
I couldn't find a way to find out from which version of the application I'm upgrading. If there is an upgrade procedure from v1 to v2 and a procedure from v2 to v3, if a user is upgrading from v1 to v3, I need to run both upgrade procedures in order, but if the user is upgrading from v2, I only need to run the second upgrade procedure.
Even if I knew what the previous version of the application is, and I could access the user settings in their former structure (say by just getting a raw XML node), if I wanted to chain upgrade procedures (as described in issue 2), where would I store the intermediate values? If upgrading from v2 to v3, the upgrade procedure would read the old values from v2 and write them directly to the strongly-typed settings wrapper class in v3. But if upgrading from v1, where would I put the results of the v1 to v2 upgrade procedure, since the application only has a wrapper class for v3?
I thought I could avoid all these issues if the upgrade code would perform the conversion directly on the user.config file, but I found no easy way to get the location of the user.config of the previous version, since LocalFileSettingsProvider.GetPreviousConfigFileName(bool) is a private method.
Does anyone have a ClickOnce-compatible solution for upgrading user settings that change type between application versions, preferably a solution that can support skipping versions (e.g. upgrading from v1 to v3 without requiring the user to in install v2)?
I ended up using a more complex way to do upgrades, by reading the raw XML from the user settings file, then run a series of upgrade routines that refactor the data to the way it's supposed to be in the new next version. Also, due to a bug I found in ClickOnce's ApplicationDeployment.CurrentDeployment.IsFirstRun property (you can see the Microsoft Connect feedback here), I had to use my own IsFirstRun setting to know when to perform the upgrade. The whole system works very well for me (but it was made with blood and sweat due to a few very stubborn snags). Ignore comments mark what is specific to my application and is not part of the upgrade system.
using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Xml;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using System.Text;
using MyApp.Forms;
using MyApp.Entities;
namespace MyApp.Properties
{
public sealed partial class Settings
{
private static readonly Version CurrentVersion = Assembly.GetExecutingAssembly().GetName().Version;
private Settings()
{
InitCollections(); // ignore
}
public override void Upgrade()
{
UpgradeFromPreviousVersion();
BadDataFiles = new StringCollection(); // ignore
UpgradePerformed = true; // this is a boolean value in the settings file that is initialized to false to indicate that settings file is brand new and requires upgrading
InitCollections(); // ignore
Save();
}
// ignore
private void InitCollections()
{
if (BadDataFiles == null)
BadDataFiles = new StringCollection();
if (UploadedGames == null)
UploadedGames = new StringDictionary();
if (SavedSearches == null)
SavedSearches = SavedSearchesCollection.Default;
}
private void UpgradeFromPreviousVersion()
{
try
{
// This works for both ClickOnce and non-ClickOnce applications, whereas
// ApplicationDeployment.CurrentDeployment.DataDirectory only works for ClickOnce applications
DirectoryInfo currentSettingsDir = new FileInfo(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath).Directory;
if (currentSettingsDir == null)
throw new Exception("Failed to determine the location of the settings file.");
if (!currentSettingsDir.Exists)
currentSettingsDir.Create();
// LINQ to Objects for .NET 2.0 courtesy of LINQBridge (linqbridge.googlecode.com)
var previousSettings = (from dir in currentSettingsDir.Parent.GetDirectories()
let dirVer = new { Dir = dir, Ver = new Version(dir.Name) }
where dirVer.Ver < CurrentVersion
orderby dirVer.Ver descending
select dirVer).FirstOrDefault();
if (previousSettings == null)
return;
XmlElement userSettings = ReadUserSettings(previousSettings.Dir.GetFiles("user.config").Single().FullName);
userSettings = SettingsUpgrader.Upgrade(userSettings, previousSettings.Ver);
WriteUserSettings(userSettings, currentSettingsDir.FullName + #"\user.config", true);
Reload();
}
catch (Exception ex)
{
MessageBoxes.Alert(MessageBoxIcon.Error, "There was an error upgrading the the user settings from the previous version. The user settings will be reset.\n\n" + ex.Message);
Default.Reset();
}
}
private static XmlElement ReadUserSettings(string configFile)
{
// PreserveWhitespace required for unencrypted files due to https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=352591
var doc = new XmlDocument { PreserveWhitespace = true };
doc.Load(configFile);
XmlNode settingsNode = doc.SelectSingleNode("configuration/userSettings/MyApp.Properties.Settings");
XmlNode encryptedDataNode = settingsNode["EncryptedData"];
if (encryptedDataNode != null)
{
var provider = new RsaProtectedConfigurationProvider();
provider.Initialize("userSettings", new NameValueCollection());
return (XmlElement)provider.Decrypt(encryptedDataNode);
}
else
{
return (XmlElement)settingsNode;
}
}
private static void WriteUserSettings(XmlElement settingsNode, string configFile, bool encrypt)
{
XmlDocument doc;
XmlNode MyAppSettings;
if (encrypt)
{
var provider = new RsaProtectedConfigurationProvider();
provider.Initialize("userSettings", new NameValueCollection());
XmlNode encryptedSettings = provider.Encrypt(settingsNode);
doc = encryptedSettings.OwnerDocument;
MyAppSettings = doc.CreateElement("MyApp.Properties.Settings").AppendNewAttribute("configProtectionProvider", provider.GetType().Name);
MyAppSettings.AppendChild(encryptedSettings);
}
else
{
doc = settingsNode.OwnerDocument;
MyAppSettings = settingsNode;
}
doc.RemoveAll();
doc.AppendNewElement("configuration")
.AppendNewElement("userSettings")
.AppendChild(MyAppSettings);
using (var writer = new XmlTextWriter(configFile, Encoding.UTF8) { Formatting = Formatting.Indented, Indentation = 4 })
doc.Save(writer);
}
private static class SettingsUpgrader
{
private static readonly Version MinimumVersion = new Version(0, 2, 1, 0);
public static XmlElement Upgrade(XmlElement userSettings, Version oldSettingsVersion)
{
if (oldSettingsVersion < MinimumVersion)
throw new Exception("The minimum required version for upgrade is " + MinimumVersion);
var upgradeMethods = from method in typeof(SettingsUpgrader).GetMethods(BindingFlags.Static | BindingFlags.NonPublic)
where method.Name.StartsWith("UpgradeFrom_")
let methodVer = new { Version = new Version(method.Name.Substring(12).Replace('_', '.')), Method = method }
where methodVer.Version >= oldSettingsVersion && methodVer.Version < CurrentVersion
orderby methodVer.Version ascending
select methodVer;
foreach (var methodVer in upgradeMethods)
{
try
{
methodVer.Method.Invoke(null, new object[] { userSettings });
}
catch (TargetInvocationException ex)
{
throw new Exception(string.Format("Failed to upgrade user setting from version {0}: {1}",
methodVer.Version, ex.InnerException.Message), ex.InnerException);
}
}
return userSettings;
}
private static void UpgradeFrom_0_2_1_0(XmlElement userSettings)
{
// ignore method body - put your own upgrade code here
var savedSearches = userSettings.SelectNodes("//SavedSearch");
foreach (XmlElement savedSearch in savedSearches)
{
string xml = savedSearch.InnerXml;
xml = xml.Replace("IRuleOfGame", "RuleOfGame");
xml = xml.Replace("Field>", "FieldName>");
xml = xml.Replace("Type>", "Comparison>");
savedSearch.InnerXml = xml;
if (savedSearch["Name"].GetTextValue() == "Tournament")
savedSearch.AppendNewElement("ShowTournamentColumn", "true");
else
savedSearch.AppendNewElement("ShowTournamentColumn", "false");
}
}
}
}
}
The following custom extention methods and helper classes were used:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Xml;
namespace MyApp
{
public static class ExtensionMethods
{
public static XmlNode AppendNewElement(this XmlNode element, string name)
{
return AppendNewElement(element, name, null);
}
public static XmlNode AppendNewElement(this XmlNode element, string name, string value)
{
return AppendNewElement(element, name, value, null);
}
public static XmlNode AppendNewElement(this XmlNode element, string name, string value, params KeyValuePair<string, string>[] attributes)
{
XmlDocument doc = element.OwnerDocument ?? (XmlDocument)element;
XmlElement addedElement = doc.CreateElement(name);
if (value != null)
addedElement.SetTextValue(value);
if (attributes != null)
foreach (var attribute in attributes)
addedElement.AppendNewAttribute(attribute.Key, attribute.Value);
element.AppendChild(addedElement);
return addedElement;
}
public static XmlNode AppendNewAttribute(this XmlNode element, string name, string value)
{
XmlAttribute attr = element.OwnerDocument.CreateAttribute(name);
attr.Value = value;
element.Attributes.Append(attr);
return element;
}
}
}
namespace MyApp.Forms
{
public static class MessageBoxes
{
private static readonly string Caption = "MyApp v" + Application.ProductVersion;
public static void Alert(MessageBoxIcon icon, params object[] args)
{
MessageBox.Show(GetMessage(args), Caption, MessageBoxButtons.OK, icon);
}
public static bool YesNo(MessageBoxIcon icon, params object[] args)
{
return MessageBox.Show(GetMessage(args), Caption, MessageBoxButtons.YesNo, icon) == DialogResult.Yes;
}
private static string GetMessage(object[] args)
{
if (args.Length == 1)
{
return args[0].ToString();
}
else
{
var messegeArgs = new object[args.Length - 1];
Array.Copy(args, 1, messegeArgs, 0, messegeArgs.Length);
return string.Format(args[0] as string, messegeArgs);
}
}
}
}
The following Main method was used to allow the system to work:
[STAThread]
static void Main()
{
// Ensures that the user setting's configuration system starts in an encrypted mode, otherwise an application restart is required to change modes.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
SectionInformation sectionInfo = config.SectionGroups["userSettings"].Sections["MyApp.Properties.Settings"].SectionInformation;
if (!sectionInfo.IsProtected)
{
sectionInfo.ProtectSection(null);
config.Save();
}
if (Settings.Default.UpgradePerformed == false)
Settings.Default.Upgrade();
Application.Run(new frmMain());
}
I welcome any input, critique, suggestions or improvements. I hope this helps someone somewhere.
This may not really be the answer you are looking for but it sounds like you are overcomplicating the problem by trying to manage this as an upgrade where you aren't going to continue to support the old version.
The problem isn't simply that the data type of a field is changing, the problem is that you are totally changing the business logic behind the object and need to support objects that have data relating to both old and new business logic.
Why not just continue to have a person class which has all 3 properties on it.
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public DateTime DateOfBirth { get; set; }
}
When the user upgrades to the new version, the age is still stored, so when you access the DateOfBirth field you just check if a DateOfBirth exists, and if it doesn't you calculate it from the age and save it so when you next access it, it already has a date of birth and the age field can be ignored.
You could mark the age field as obsolete so you remember not to use it in future.
If necessary you could add some kind of private version field to the person class so internally it knows how to handle itself depending on what version it considers itself to be.
Sometimes you do have to have objects that aren't perfect in design because you still have to support data from old versions.
I know this has already been answered but I have been toying with this and wanted to add a way I handled a similar (not the same) situation with Custom Types:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
private DateTime _dob;
public DateTime DateOfBirth
{
get
{
if (_dob is null)
{ _dob = DateTime.Today.AddYears(Age * -1); }
else { return _dob; }
}
set { _dob = value; }
}
}
If both the private _dob and public Age is null or 0, you have another issue all together. You could always set DateofBirth to DateTime.Today by default in that case. Also, if all you have is an individual's age, how will you tell their DateOfBirth down to the day?

Categories