|
Not sure, but I believe you have to turn on this feature in the AssemblyInfo page. Then, you have to put it on the Type you want to make visible.
Ben Scharbach
Temporalwars.Com
YouTube:Ben Scharbach
|
|
|
|
|
Hi,
I was looking for some guidance on best practices regarding error handling.
public class DemoController : Controller
{
private readonly ILogger _logger;
public DemoController(ILogger logger)
{
_logger = logger;
}
public ActionResult Index()
{
try
{
}
catch (Exception ex)
{
_logger.Error(ex.ToString());
}
return View();
}
}
I was therefore wondering if anyone would have any suggestions to improve this code?
Thanks
The Dark Knight
modified 26-Jul-17 16:34pm.
|
|
|
|
|
Ask the person who told you it was sub-standard what their issues are. They could be referring to the fact that catching just "Exception" is too generic, that you should aim for specific exceptions (that rarely happens in the real world though). Or it could be the fact that you just log the exception and do nothing with it, you go on to return the View as normal so the user has no real knowledge that the code failed.
|
|
|
|
|
Thanks, makes more sense to me now.
Paul McGann
|
|
|
|
|
It's usually good practice to inform the user that an error occurred, and therefore the output might not be trustworthy or the input was not accepted by the system.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
Thanks Benjamin, i'll keep this in mind for future.
Paul McGann
|
|
|
|
|
The "best wisdom" I have found re error-handling is in the writings of Eric Lippert, a key .NET architect: his "classic" piece often cited by other language gurus: [^].
Also: [^], [^].
A summary based on Eric's writings: [^].
There's a 3 part series on "Dr. Spock Engineer" blog I think is worth studying that starts here: [^].
I favor always throwing an Exception except in very special cases like within an enumeration of data where partial results are acceptable and usable.
«Differences between Big-Endians, who broke eggs at the larger end, and Little-Endians gave rise to six rebellions: one Emperor lost his life, another his crown. The Lilliputian religion says an egg should be broken on the convenient end, which is now interpreted by the Lilliputians as the smaller end. Big-Endians gained favor in Blefuscu.» J. Swift, 'Gulliver's Travels,' 1726CE
|
|
|
|
|
Thanks Bill, great articles and advice appreciate everyone's feedback.
Paul McGann
|
|
|
|
|
Logging the exception is useful, but I would recommend also throwing (not rethrowing) it. What I mean is:
try {
DoSomething();
}
catch (Exception ex) {
this._logger.Log(ex);
throw;
}
/ravi
|
|
|
|
|
Yes, instead of writing the same old boiler-code (as they call it), I would use PostSharp to create code-injection attributes which would automatically wrap the method with your try-catch{} exception-logger. This reduces the redundant code and improves the visibility of your class code.
Ben Scharbach
Temporalwars.Com
YouTube:Ben Scharbach
|
|
|
|
|
Greetings,
I feel like stupid when asking this question: Is it possible to create an API (or REST API) from a C# winform so other application can consume it? However, I have seen lots of posts about how to consume API from a winform.
What I have done is to create an command-line in winform so other app calls, it passes the values in the parameter.
For example:
myapp.exe -LastName:[name value] -DOB:[value]
I have run Google search and could not find anything related to this question.
Your guidance/suggestions/recommendations are truly appreciated.
JPL
|
|
|
|
|
It rather depends on what myapp is doing. You need to explain more about exactly what problem you are trying to solve.
|
|
|
|
|
Though it may be possible, it is generally not recommended: some one might run the program in a non-interactive session (e.g. by calling it from a windows service, or IIS). Then any dependency on a UI will cause trouble. Note that there is no user on the server where IIS is running, so nobody will be able to click OK on a message box showing an error. And even when you got such things right: even the link to System.Windows.Forms anywhere in a dll/exe causes trouble in such circumstances, you might have to copy System.Windows.Forms.dll into the folder of your application.
Better separate the logic completely from any UI. The UI of your "normal" app will then reference that logic and use it. And a command line app will reference the same logic. Or others may directly reference the dll(s) with the logic and use it directly. That's the way to go.
|
|
|
|
|
Consider three scenarios:
1. you want to provide a library of methods and data ... without visual UI ... to external clients: you create a Class Library which compiles to a 'dll.
2. you want to provide a re-usable Control which integrates methods and data with a visual UI to external clients: you create a WinForm Control (Component) which compiles to a 'dll.
3. you want to provide a Win application which integrates methods and data with a visual UI to external clients, which can be started with the System.Diagnostics resources (Process.Start), to which you can pass command line arguments etc. : you create an 'exe.
In all three cases, I'd follow Bernard's advice, and separate out logic, algorithms, data constant values, etc., into a Class Library that had no dependencies on the UI.
However, you can add a reference to a WinForm '.exe to another WinForm Project, and then access methods and data in that '.exe without starting it with Process.Start; without running it by invoking its Main Method. If you have static Classes defined: their contents can be used immediately; other non-static Classes will, of course, need to be instantiated with 'new to use their contents.
But, while .NET let's you "get away with this," you're, imho, creating a "monster"
«Differences between Big-Endians, who broke eggs at the larger end, and Little-Endians gave rise to six rebellions: one Emperor lost his life, another his crown. The Lilliputian religion says an egg should be broken on the convenient end, which is now interpreted by the Lilliputians as the smaller end. Big-Endians gained favor in Blefuscu.» J. Swift, 'Gulliver's Travels,' 1726CE
|
|
|
|
|
You can use WCF to host an API in your app. This link is for hosting in a Console app but the principal is the same
Self Hosted WCF REST Service or Hosting WCF REST Service in Console Application[^]
Obviously your winform app needs to be running for it to be listening for requests though. If you don't want to use WCF you can create your own lightweight protocol just by listening on a given port and having the client connect to that port and send basic commands in a format that you devise.
|
|
|
|
|
Hi,
I am working on ASP.NET MVC5.
I have a two class that has many fields. In a different view, I want to insert two fields from on table and a list of one field from the other class.
Table A
CentreId
AdminId (UserId)
CentreName
Address
Phone
Table B
UserId
FirstName
LastName
Email
Password
The view will have fields like below
First Name
Last Name
Email
Centres
-------List the centres based on the UserId-------
Button Save
When the save button is clicked, I wish to add the details to another table like below
Table C
TeacherId
AdminId
CenterId
How can I accomplish this? Please help
Thanks
|
|
|
|
|
And? What have you tried? Where are you stuck? What help do you need?
And where does TeacherId come from?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Teacher is a new table and hence TeacherId.
I added code in the Controller as below
Public ActionResult Educator()
{
int curId = (int)Session["UserId"];
var centres = from c in db.Centres where c.AdminId == curId select c;
return View();
}
And in View, I have
@html.Label("First Name")
@Html.EditorFor()
@html.Label("Last Name")
@Html.EditorFor()
@html.Label("Email")
@Html.EditorFor()
Next, I want a collection of Checkboxes from the Centres result.
When I click submit, this record goes to the Users table and also the Teachers table.
Thanks
|
|
|
|
|
sunsher wrote: Next, I want a collection of Checkboxes from the Centres result. I'd recommend reviewing the ng-loop directive. Most books will show how to do this in one of the first chapters.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
i have a server which send packet in the following format continuously after 60 second
u2u 325 16.6 6.5 330 329 | u2u 15 17 20 22 19 30 | u2u n s e w r s ||
i want to show this packet in the given format
label1 : 325 label2: 16.6 label3: 6.5 and so on in c sharp window form application
plz help me i am begginner in c sharp
|
|
|
|
|
Assuming you have received the packet - and if you haven't that's a whole new ball of wax - use string.Split:
string packet = "u2u 325 16.6 6.5 330 329 | u2u 15 17 20 22 19 30 | u2u n s e w r s ||";
string[] parts = packet.Split(' ', '|');
if (parts.Length == 27)
{
label1.Text = parts[1];
label2.Text = parts[2];
...
}
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
It appears that "u2u" is a section delimiter here; using:
string[] sections = packet.Split(new string[]{"u2u"," | ","||"},StringSplitOptions.RemoveEmptyEntries); Will get you a 3 element string array where each element contains only the data in that section.
You can easily take each section, use string.Split again to get the section itrms, and then set your Label values accordingly.
I note that your first section has #5 elements; the other sections have #6.
The code I would write for something like this would depend on how reliable I thought the data was: the extent it had regular structure. And, I'd always assume that there would be errors, eventually
«Differences between Big-Endians, who broke eggs at the larger end, and Little-Endians gave rise to six rebellions: one Emperor lost his life, another his crown. The Lilliputian religion says an egg should be broken on the convenient end, which is now interpreted by the Lilliputians as the smaller end. Big-Endians gained favor in Blefuscu.» J. Swift, 'Gulliver's Travels,' 1726CE
|
|
|
|
|
Hello
I am working on a recent project and I have been learning how to code in C# using OpenGL libraries for some graphics. I have achieved some quite interesting things using TAO Framework writing in Console Applications, creating a GLUT Window. But my problem now is that I need to incorporate the Graphics in a Windows Form so I can relate the objects that I render with some .NET Controls.
To deal with this problem, I have seen in some forums that it's better to use OpenTK instead of TAO Framework, so I can use the glControl that OpenTK libraries offer. However, I haven't found complete articles, tutorials or source codes that help using the glControl or that may insert me into de OpenTK functions. Would somebody please share in this forum some links or files where I can find good documentation about this topic? Or may I use another library different of OpenTK?
Thanks!
|
|
|
|
|
|
Good Morning Richard.
Thank you for commenting, but the search results are all based on GLUT independent Windows. What I need is to attach an OpenGL View inside a Windows Form. The most related article that I have found is this: An Analog Clock Design Using OpenTK in C#[^]. However, the source code isn't available and if I follow the steps described, the program doesn't work.
|
|
|
|