I create in my sharepoint visual web part custom properties, problem is that after a server restart, the value disappears.
public enum Organ { INST1, INST2 };
public static Organ OrganEnum;
[Category("Custom settings"),
Personalizable(PersonalizationScope.Shared),
WebPartStorage(Storage.Shared),
WebBrowsable(true),
WebDisplayName("Organ"),
WebDescription("Choice Organ")]
public Organ _OrganEnum
{
get { return OrganEnum; }
set { OrganEnum = value; }
}
I tried in sharepoint web.config edit this line, but it does not work
<SafeControl Assembly="WebPart, Version=1.0.0.1, Culture=neutral, PublicKeyToken=998d82b12e783432" Namespace="WebPart.Organ" TypeName="*" Safe="True" SafeAgainstScript="True" AllowRemoteDesigner="True" />
Your property OrganEnum shouldn't be static. That is probably what causes your trouble.
Try decaring you property like this:
public Organ OrganEnum{ get; set; }
and skip the
public static Organ OrganEnum;
altogether.
Related
I'm saving and loading various variables in my application settings file (Settings.settings). Saving/Loading variables such as strings, Uris and DataTables is working correctly.
When attempting to Save/Load a custom object List<IterationFilter>, the List is lost when the application is closed and reopened. List<IterationFilters> becomes null when the application is reloaded... even though an IterationFilter was added to the List and saved.
Saving a String (working correctly):
Properties.Settings.Default.ConnectionString = connectionString;
Properties.Settings.Default.Save();
Saving a Generic List:
Properties.Settings.Default.FilterList.Add(newFilter);
Properties.Settings.Default.Save();
I followed this answer, to create my List setting. My .settings file looks like this:
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public System.Collections.Generic.List<TFS_Extraction.IterationFilter> FilterList {
get{
return ((System.Collections.Generic.List<TFS_Extraction.IterationFilter>)(this["FilterList"]));
}
set{
this["FilterList"] = value;
}
}
My IterationFilter class:
namespace TFS_Extraction
{
[Serializable]
public class IterationFilter
{
public string Operator { get; set; }
public string Value { get; set; }
public IterationFilter(string _operator, string _value)
{
Operator = _operator;
Value = _value;
}
}
TFS_Extraction.IterationFilter has to be serializable. The class is required to have a public default constructor.
I've got a bit of a strange situation going on. I've created a very simple class as such.
[Serializable]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public class StockSyncCollection : ObservableCollection<StockSyncDatum>
{
}
public class StockSyncDatum
{
public StockSyncDatum() { }
public StockSyncDatum(int posStockId, int posStockCatalogueId, DateTime lastUpdated)
{
this.PosStockId = posStockId;
this.PosStockCatalogueId = posStockCatalogueId;
this.LastUpdated = lastUpdated;
}
public int PosStockId { get; set; }
public int PosStockCatalogueId { get; set; }
public DateTime LastUpdated { get; set; }
}
I have then created a custom setting through the designer for which the generated code looks like this
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::Sync.Config.StockSyncCollection StockSyncCollection {
get {
return ((global::Sync.Config.StockSyncCollection)(this["StockSyncCollection"]));
}
set {
this["StockSyncCollection"] = value;
}
}
However the problem I am running into is that later in my code (after adding some items to the collection) I will call
Settings.Default.Save();
But what happens is that only some subset of the items that have been added to the collection up until the point the save method is called is actually persisted. So for example, I might add 50 items but only 10 of them will be saved.
Now note that I am calling Settings.Default.Save() in a loop. My gut instinct is telling me that there is some sort of hash that is generated of the object that isn't being updated.
So what I am wondering is how the hell do you debug Application Settings in C# apps? I can see various events like SettingsChanging and SettingsSaving but I do not see an event for SettingsSaveError or similar.
Anyone have any idea what might be going on or how to debug this?
EDIT: The loop code basically looks like this.
foreach (var partition in stockTransfers.Partition(PartitionCount))
{
IList<StockTransferContract> stockTransferContracts = partition.ToStockTransferContracts();
//Do all the magic syncing stuff...
foreach (var item in stockTransferContracts)
_StockSyncCollection.Add(new StockSyncDatum(posStockId, posStockCatalogueId, DateTime.UtcNow));
Settings.Default.Save();
}
Keep in mind that I need to call save because I expect errors to occasionally arise and need to ensure that the previously "Sync'd" items are noted.
I have the class CaptureResolution representing the resolution for a camera capture:
[Serializable]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
[XmlRoot (ElementName = "CaptureResolution", IsNullable = false)]
public class CaptureResolution: ApplicationSettingsBase
{
[UserScopedSetting]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
[XmlAttribute (AttributeName = "Width")]
public int Width { get; set; }
[UserScopedSetting]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
[XmlAttribute(AttributeName = "Height")]
public int Height { get; set; }
public CaptureResolution(int width, int height)
{
Width = width;
Height = height;
}
public CaptureResolution(): this(1024, 720)
{
}
}
I added a setting into the settings designer with the given type:
However when calling this
Properties.Settings.Default.ResolutionSelection = new CaptureResolution(1920, 1080);
Properties.Settings.Default.Save();
The setting is not saved in the user settings file:
<setting name="ResolutionSelection" serializeAs="Xml">
<value />
</setting>
I can surely verify that the value is assigned to the setting's property. Otherwise the program wouldn't work at all. I also had a look with the debugger on this.
Also I already searched on the internet for suitable solutions but in the end it didn't really helped. Other settings are saved without any problems.
Afaik the settings designer needs a class that can be serialized to XML and a default parameterless constructor that is public accessible. I did both so I'm wondering why it is not working as intended.
Additional question
How can I assign a default value to this custom type setting?
Entering new FaceDetection.Model.CaptureResolution() ends up in an exception.
You are deriving CaptureResolution from ApplicationSettingsBase:
public class CaptureResolution: ApplicationSettingsBase
Don't do that. There is no need to do it, and doing so obviously doesn't work.
Default Value
The settings desinger will create a Settings.cs if you click on the "View Code" button on its top. In this Settings.cs file you can add your own code and you can manually create application settings, e.g. something like that:
[UserScopedSetting]
public CaptureResolution ResolutionSelection
{
get
{
var value = (CaptureResolution)this[nameof(ResolutionSelection)];
if (value == null)
{
value = new CaptureResolution(1, 2); // decent default value
this[nameof(ResolutionSelection)] = value;
}
return value;
}
set { this[nameof(ResolutionSelection)] = value; }
}
This will create a default value it the setting is null. You will have to remove the setting that you created using the desinger.
I have a sample c# application.
In the App.config file there is parameters such as add key="access" value="abcd" inside appSettings tag and in other class file there is a get property such as
public string Access { get; } from where I am getting the value abcd.Here there is no set property.But I am getting the value of access as abcd and its working fine.
My question is, where the value of access in App.config is set in the application.
A readonly property still can be set from within the class that defines the property. And that's what is happening here.
Pseudo code:
public class Settings
{
private string _access;
public Settings()
{
_access = read from config;
}
public string Access { get { return _access; } }
}
I've got a class which looks a little like this....
[DataContract]
public partial class Area : ModelBase
{
private String name;
private Guid floorId;
private Guid areaTypeId;
private int assetCount;
[DataMember]
public String Name
{
get { return name; }
set { name = value; }
}
[DataMember]
public Guid FloorId
{
get { return floorId; }
set { floorId = value; }
}
public Guid AreaTypeId
{
get { return areaTypeId; }
set { areaTypeId = value; }
}
}
....and I have a Wcf Service Library which has the following interface defined...
IEnumerable<Area> GetSomeStuff(IEnumerable<Area> uploadedAreas);
It's all working just fine, but in my client app (a compact framework application) the AreaTypeId property is exposed?
I thought that if I didn't add the [DataMember] attribute it wouldn't be seen by the client? What am not understanding???
Thanks,
ETFairfax
If you want to hide any property from client then just add [IgnoreDataMember] attribute to that property.
If you are sharing type assembly between client and server It will be visible unless you turn off reusing types from referenced assemblies (click "Configure Service Reference" on proxy).
If you are not sharing assembly it will not be visible because code for class is generated based on exposed contract (you can see it by turning on Show All Files in VS and then go to generated file Reference.cs under service proxy).
DataMember is attribute for DataContractSerializer so if you are sharing assembly the property will be not serialized on server and not initialized on client but it will be visible. Reference