|
If the code is repeated in the same controller you should extract out to a single method. From your description it sounds like you have the same block of code in several controllers, in which case you extract out the method into a common base controller and inherit from it. e.g.
public abstract class MyBaseController
{
protected void CommonCodeMethod()
{
}
}
public class MyController1 : MyBaseController
{
public void Foo()
{
CommonCodeMethod()
}
}
public class MyController2 : MyBaseController
{
public void Foo()
{
CommonCodeMethod()
}
}
You should put the base in it's own file (as your controllers probably are). You might not need the abstract on the class, and you might want to make CommonMethod public or internal .
If the function is used outside of controllers, then the base class isn't suitable: create a separate class that contains the common method, try to give the class used a descriptive name (for example, if the common methods all create ViewModel called FooViewModel , consider FooViewModelCreator as the class name and Create as the methodname). If you have several such methods, try to keep ones that fit naturally together in the same class, so the class is only really responsible for one category of thing and you have [potentially] several classes.
[Edit]
Countered Univotes. Note sure why your question was downvoted, seemed to me to be a sensible newbie question - perhaps the downvoter could elucidate.
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed” “One of the greatest problems of our time is that many are schooled but few are educated”
Sir Thomas More (1478 – 1535)
modified 18-Jun-13 8:44am.
|
|
|
|
|
Just added my 5 to his vote.
|
|
|
|
|
I am trying to reference 2 identical assemblies with only their filename and version to be different. I need this in order to support several versions for backward compatibility.
I've created an alias name for one of them and the other stayed "global". Also added the "extern alias" declaration at the start of the source code.
I'm getting compiler error (my real module name was switched with [module name] here):
"An assembly with the same simple name '[module name], Version=1.0.0.0, Culture=neutral, PublicKeyToken=null has already been imported. Try removing one of the references or sign them to enable side-by-side."
One of these two assemblies does have a version 1.0.0.0, but the other has 1.5.0.0 and I can see that when I highlight them in the solution explorer in the properties window.
The assemblies are not signed, and I do not currently wish to make them as such. Why is this error? The versions are clearly different… is there a way to solve this?
i'm using VS2012.
|
|
|
|
|
Using different AppDomains would solve it.
|
|
|
|
|
It won't solve it since i need to load both assemblies in the same domain.
|
|
|
|
|
Try this[^]
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
i already found this before, but it does not offer a working solution:
i cannot change the assembly info since it is a compiled module and as i said - the assemblies are not signed, but they have a different version.
Any other way to make it work?
|
|
|
|
|
impeham wrote: Any other way to make it work? Remove the reference and load the assembly dynamic.
If you want to use the simple names, you'd have to abide by it's rules.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Pretty sure that your requirements, per your posts, means there is no solution.
So either relax your requirements or don't do it.
|
|
|
|
|
In an app where you think about "backward compatibility" and other things you should really use strong names for the assemblies - So I'd question why you are not using strong-names (signing). I can't think about a good reason for not doing it (and solving your problem) other than "too lazy to manage the certifcate"... Even if you don't worry about installing your assembly in the GAC or your manufacturer authenticity, there a good reasons for signing - you just found one!
|
|
|
|
|
Signing the assemblies would probably be the best solution - i agree.
At the moment it is not feasible since there are clients with this module in the field already that cannot be upgraded with new signed assemblies.
I am using a workaround for this in the meantime - I am changing the assembly name from the project properties (changing the version and the title in assembly information did not work). I still have a few tests to do before I'll know it is working for sure (without the need to upgrade any client modules).
|
|
|
|
|
I have created a website using C#.I need to implement barcode generator to certain pages.
I have tried with the below link.
http://www.codeproject.com/Articles/3888/C-Barcode-Generator-WebService
I am not familiar with the .resx and .asmx files.
Could any one help me explaining this..
Thanks in advance..
|
|
|
|
|
0. You have posted this question in Q&A - Do not repost here
1. There is a forum at the bottom of the article - the author will be better placed to help you.
2. "generate barcodes in c#" produces ~1,900,000 results in Google, please try those first
It is unlikely you'll want to use a web service if you just want barcodes. You can download barcode fonts[^] and use those directly, but you'll need to check compatibility.
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed” “One of the greatest problems of our time is that many are schooled but few are educated”
Sir Thomas More (1478 – 1535)
|
|
|
|
|
hello guys, I need help, I'll explain the problem ...
I am making a wpf project that pressing a buttom let me see the video of the IP camera, only to see that the string directly to the video does not work, so I use the string of the image that is updated every time, to do this use the thread, and to avoid exceptions, use the delegates, only that at some point I will generate this error:
An unhandled exception of type 'System.StackOverflowException' in WindowsBase.dll
The point at which I generate error is as follows:
Del Ert = new Del(setImage);
images.Dispatcher.Invoke (ert, new object [] {images, url});
the type
Del is my delegate:
public delegate void Del (Image images, string url);
I hope you can help me, if you need some additional information I can post it
|
|
|
|
|
The most common way for this to happen is for an infinite recursion or loop to happen, e.g. a method calling itself (or calling itself via other method(s)) in such a way that the loop never ends. There is a stack object used by the .net framework when it executes your code (called the call execution stack), this has a finite size. Normally you enter a method (the call is pushed on the stack) and when the method exits the method is removed (popped off) from the stack. When infinite looping happens the method (or methods if a calls b calls c calls a etc) is repeatedly popped onto the stack filling it, then it overflows. As the stack is large this normally takes a few seconds though it can take even longer depending on your intervening code.
One problem with this is that the stack overflow exception doesn't necessarily happen where the actual problem is, so you might not have included the recursive code needed so we can help you in your problem.
I suggest two options:
1) Run the code until the error occurs, then open the stack trace window[^]. You'll see the calls made by your code and by reading through this you'll see the loop and hopefully where you need to break out of it.
2) Put a breakpoint on the code where you are getting the failure, then step through. At some point you'll loop round (hopefully quickly!) again you'll need to decide where you want to break out.
I'd try these two first, the first option is quicker though if you can't spot your problem then the second option will be clearer. There is a possibility that images.Dispatcher.Invoke (ert, new object [] {images, url}); is causing the loop, if ert is either directly or indirectly recursive. The other thing that might be the case (though unlikely) is that you have an non call-stack which is filling, but even this is likely to be filled by an infinite loop.
[Edit]
Me use INglish good and tipe weLL
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed” “One of the greatest problems of our time is that many are schooled but few are educated”
Sir Thomas More (1478 – 1535)
|
|
|
|
|
hello I tried your solution, I put a break-point and I noticed that IF is the cycle that makes me run any code inifinite times.
public void setImage(Image images,string url)
{
if (images.Dispatcher.CheckAccess())
{
MessageBox.Show("ciao");
Del ert = new Del(setImage);
images.Dispatcher.Invoke(ert, new object[] { images, url });
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(url);
img.EndInit();
images.Source = img;
}
else
{
images.Dispatcher.BeginInvoke(new Del(setImage), DispatcherPriority.Normal,
new object[] { images, url });
}
}
public void StreamImg()
{
while (true)
{
var date = DateTime.Today.Hour;
setImage(image1, @"http://ipaddress/jpg/image.jpg" + "?" + date);
Thread.Sleep(1000);
}
}
|
|
|
|
|
public void setImage(Image images,string url)
{
if (images.Dispatcher.CheckAccess())
{
MessageBox.Show("ciao");
Del ert = new Del(setImage);
images.Dispatcher.Invoke(ert, new object[] { images, url });
}
else
{
images.Dispatcher.BeginInvoke(new Del(setImage), DispatcherPriority.Normal, new object[] { images, url });
}
}
See the comment parts I've made in the stripped down version of your code. This is the problem, in both branches of the if/else you recursively try to call the method.
I think you need to do the following:
public void setImage(Image images,string url)
{
if (images.Dispatcher.CheckAccess())
{
MessageBox.Show("ciao");
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(url);
img.EndInit();
images.Source = img;
}
else
{
images.Dispatcher.BeginInvoke(new Del(setImage), DispatcherPriority.Normal,
new object[] { images, url });
}
}
Was should happen is setImage is called, if it is on the Dispatcher, fine, the UI is updated in the if block and the method exits. If the call isn't on the dispatcher the else block is executed, setImage calls itself on the dispatcher asychronously. When this call is executed, the if block is executed, the UI updated and the method exits (also exiting the call to setImage on the non-dispacther thread).
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed” “One of the greatest problems of our time is that many are schooled but few are educated”
Sir Thomas More (1478 – 1535)
|
|
|
|
|
Keith, Thank you very much, with your solution the error disappears... thanks for your patience!!
|
|
|
|
|
No problem!
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed” “One of the greatest problems of our time is that many are schooled but few are educated”
Sir Thomas More (1478 – 1535)
|
|
|
|
|
Ehm sorry, but I tried again and I noticed that no longer enters into the cycle without those commands, how i do?
|
|
|
|
|
You going to have to put breakpoints on and check, the method should be entered at least once when streamImage calls it. You shouldn't be "cycling" as such, the code should be called once, go into the "else" block which calls the method once under the dispatcher thread (entering the if block and ending). If you need to loop then I don't understand the purpose of your code.
If you are entering the "else" block without going to the "if" It could be that you don't want the asynchronous BeginInvoke and you need a straight Invoke instead, but I don't know why you chose the async method in the first place. It may be the dispatcher is being destroyed before the async method is called, I really can't remember if this is possible, or if an exception is thrown if it does. It could also be that the Dispatcher is running something on a higher priority that is blocking your opertation, but I think this is less likely.
Can't be much more help than that I'm afraid.
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed” “One of the greatest problems of our time is that many are schooled but few are educated”
Sir Thomas More (1478 – 1535)
|
|
|
|
|
the complete code is the following:
public partial class MainWindow : Window
{
Thread newtrd;
public MainWindow()
{
InitializeComponent();
}
public delegate void Del(Image images, string url);
public void setImage(Image images,string url)
{
if (images.Dispatcher.CheckAccess())
{
BitmapImage img = new BitmapImage();
img.BeginInit();
var date = DateTime.Today.Hour;
img.UriSource = new Uri(url + "?" + date);
img.EndInit();
img.Freeze();
images.Source = img;
}
else
{
images.Dispatcher.BeginInvoke(new Del(setImage), DispatcherPriority.Normal,
new object[] { images, url });
}
}
public void StreamImg()
{
while (true)
{
try
{
setImage(image1, @"http://ipaddress/jpg/image.jpg");
}
catch (Exception x)
{
MessageBox.Show(x.Message.ToString() + "\n Chiudere il Programma dal pulsante Exit");
}
Thread.Sleep(500);
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
newtrd = new Thread(new ThreadStart(StreamImg));
newtrd.IsBackground = true;
newtrd.Start();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
if (newtrd.IsAlive)
{
newtrd.Abort();
}
this.Close();
}
}
}
I use delegates to overcome the problems of access to the thread
|
|
|
|
|
Here's a little trick for you - there's no need to do cross thread access on a BitmapImage . As this class inherits from Freezable , you can simply call Freeze on the BitmapImage object before you assign it to images.Source . Your code could end up like this:
public void StreamImage()
{
while (true)
{
var date = DateTime.Today.Hour;
string url = @"http://ipaddress/jpg/image.jpg?" + date;
BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(url);
image.EndInit();
image.Freeze();
images.Source = image;
}
}
|
|
|
|
|
i tried this solution, but I got another error: Unable to lock this Freezable.
|
|
|
|
|
Ah yes. You're getting this because your Uri is still downloading the stream. If you hook into the DownloadComplete event on the BitmapImage, you can freeze the image at that point.
|
|
|
|
|