Can't use public class in my web service - c#

Why i can't use the following public class :
namespace OrganizerUI.App_code
{
public class Employee
{
private string text;
public string Text
{
get { return text; }
set { text = value; }
}
}
}
in my web service :

Often (always?), code files that are added to the Visual Studio App_Code directory are not set by default to compile even if they are .Net code. They are set to "Content" which means they are included in the output only. If you right-click the file and choose "properties" you can see/change it to "compile" instead of "content".

Related

How to fix c# multilingual application, so that it can work once installed and not only in VS2018 environment

I am developing a multilingual application using ResourceManager and custom resource files. The application does not have to use the current PC culture, but one of the aforementioned files. Launching the program from Visual Studio 2017 environment everything works correctly. If I make the installation disk and install it, I no longer see the strings. How can I solve the problem?
I created two .resx files: Resources.en.resx and Resources.it.resx (under Properties) and a menu to choose the language.
I create class to manage the language:
public class LanguageConfiguration: INotifyPropertyChanged
{
public CultureInfo MyCulture { get; set; }
private string _langName= "en";
public string LangName
{
get => _langName;
private set
{
if (value == _langName) return;
_langName= value;
UpdateLanguage();
OnPropertyChanged();
}
}
private LanguageConfiguration ()
{
UpdateLanguage();
}
public void UpdateLanguage()
{
Cultura = CultureInfo.CreateSpecificCulture(_langName);
}
}
In the ViewModel of the Main Form:
public class ViewModelMain : INotifyPropertyChanged
{
public readonly LanguageConfiguration Language;
public ResourceManager Translation{ get; set; }
public ViewModelMain(Action<Action> invoker)
{
Language = new LanguageConfiguration();
var Translation= new GestoreTraduttore(TraduzioneHelper.Namespace, TraduzioneHelper.Assembly);
Translation = new ResourceManager($#"{typeof(ViewModelMain).Namespace}.Properties.Resources", typeof(ViewModelMain).Assembly);
}
I expect a label on the Main form to change as the selected language changes (LangName). And in fact this happens, launching the program from the "Play" button.
At this point I create an installation disk using the Visual Studio 2017 Setup Project, install and launch the application: the label remains white.
I have verified that also in the installed application the language is selected correctly, but in any case the strings are not displayed.

How to hide Methods and Properties that i wrote in Created Item Template after adding to project?

I have question. I created a custom user control. My CustomUserControl is inherited from UserControl. I add some custom methods and properties in my CustomUserControl. I wanted to add my CustomUserControl to "Add New Item" in Visual Studio for projects.
For this I used "Item Template" and created a template. After restarting Visual Studio every thing was fine and I could add my CustomUserControl by using "Add New Item" in my project.
Just i have a problem when I add CustomUserControl to my project, the methods and properties that I am added into template file appeare and i can change them. How can I Hide methods and property in template? I don't want to see methods and properties after add CustomUserControl to project.
Note : When i add my CustomUserControl project, "CustomUserControl1" is created and it inherit from the UserControl not my CustomUserControl.
My Template is :
public partial class CustomUserControl : UserControl
{
private string _Version;
[Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public string Version
{
get { return _Version; }
private set { _Version = value; }
}
private void InitRequirements()
{
try
{
// ... My Code
}
catch (Exception exp)
{
throw exp;
}
}
}
After adding to project :
public partial class CustomUserControl1 : UserControl
{
private string _Version;
[Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public string Version
{
get { return _Version; }
private set { _Version = value; }
}
private void InitRequirements()
{
try
{
// ... My Code
}
catch (Exception exp)
{
throw exp;
}
}
}
it should be like this :
public partial class CustomUserControl1 : CustomUserControl
{
// Without showing methods and properties
}
Thank you
Best regards,
You can try to hide the properties you don't want to display using the new keyword.
For example, if you want to hide the property Text, you can add the following:
[Bindable(false)]
[Browsable(false)]
public new string Text { get; set; }
Edit
If you want to reuse your control in other projects, instead of an "Item Template" you may want to create a library or assembly and reference it in your projects. Like this, you will be able to use it and inherit from it without seeing the code. When you use the Item Template it will just create a new UserControl based on the code saved on the template, but you are not reusing the UserControl itself. You could also create a NuGet package if you want to handle versioning, etc...

c# Custom Attribute with support for string resources does not load localized resources

I have a DLL containing this custom attibutes
[AttributeUsage(AttributeTargets.Class)]
public class MyAttribute: System.Attribute {
private ResourceManager resManager = null;
public MyAttribute() {
}
public Type ResourceType { get; set; }
private string caption = string.Empty;
public string Caption {
get { return getResourceString(caption); }
set { caption = value; }
} //Caption
private string getResourceString(string resourceKey) {
if (string.IsNullOrWhiteSpace(resourceKey))
return string.Empty;
if (ResourceType == default(Type))
return resourceKey;
if (resManager == null)
resManager = new ResourceManager(ResourceType);
return resManager.GetString(resourceKey) ?? string.Format("[[{0}]]", resourceKey);
} //getResourceString()
}
And a program (exe) that use the MyAttribute and also has the resources needed (resKeyHello) for both english and french in Resources.resx and Resources.fr.resx respectively
[MyAttribute(Caption="resKeyHello", ResourceType=typeof(Resources)]
public Foo() {
}
The problem is that it never use the french satellite assembly. I also tried to create the ResourceManager like this ResourceManager(ResourceType.FullName, ResourceType.Assembly), but without any effect. And also tried loading the resource like this resManager.GetString(resourceKey, Thread.CurrentThread.CurrentUICulture)
I can see in debugger that CurrentUICulture is french.
I finally found a workaround.
The problem seem to be about the way french resources file were generated. I used Project -> New -> Resources file with same name and .fr.resx extension.
The satellite DLL were generated during compilation in the ./bin/fr sub folder, but the runtime did not see it.
Finally I used "ResX Resource Manager" from CodePlex to generate my resource files and now everything is ok

c# property contains only get,where the value set from App.config

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; } }
}

Wcf - Hiding Properties?

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

Categories

Resources