is it better to declare Webservice class object instances as static as the .asmx webservice classes have only static methods.
what i want is that i declare and instantiate webservice asmx class as static in aspx Page Behind Class.
and on every event call on that page i could perform operation against webservice methods.
is it beneficial in terms of performance?
Thanks
Usama
Actually there are no "static classes" .. there are static members (variables,methods). static class is sth of .net2.0 that forces every member to be static. and that's about it. In addition to the fact:
"static" and "instance" are opposites - you can't instantiate a static class. – John Saunders Mar 11 at 3:18
Related
I see defaut template use ServiceProvder.GetService<ApplicationDbCotnext>() to initialize a DbContext,
But when you inside a Static Method, I have no idea how to get a DbContext, because there is no ServiceProvider.
Is there a way to get the ServiceProvider ?
Well, first of all, this has nothing to do with asp.net-core per se. This has more to do with how Dependency Injection works. You have to ask yourself why your method is static. Is that really necessary?
If you can't get rid of your static method, you might as well go all the way and introduce another anti-pattern, the Service Locator Pattern. In short: In the Startup class you put a reference to the ServiceProvider in a static property (call it for instance "ServiceProviderSingleton") of a static class (for instance "ServiceProviderProvider"). This way you can just call "ServiceProviderProvider.ServiceProviderSingleton.GetService()".
Again, i suggest giving your overal design a critical look. But if this is what you need/want then I hope it helped.
If we have a look at Microsoft's static methods (extension) - they seem not to use logging there - just throw appropriate Exception, for example in UseMvc method (for StartUp class):
https://github.com/aspnet/Mvc/blob/760c8f38678118734399c58c2dac981ea6e47046/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcApplicationBuilderExtensions.cs
When a class that contains code is instantiated, is the code is the class shared automatically by other instantiations of that same class? Eg. The data in the class that is instantiated may be minimal; however the code could be very significant. If the code is not "automatically" shared, is there a way to achieve that other than separating the code from the class data?
Sure.
Classes have the state and the behavior.
The state is encoded in member variables of the class. Each instance has its own copy of variables, thus its own state.
The behavior is specified by the methods implemented in the class ('methods' here stand for all static, non-static methods, setters and getters). Implementation is shared by all instances of the class, so all instances behave the same, but actual results and side-effects depend on instance state, obviously.
I understand the instance variables on an action class are thread-safe since action classes are instantiated per request. But I have this need for extending action classes and I'm concerned about the thread-safety.
Say, for example I have some common attributes and a few methods handling those attributes among several action classes. I prefer to put them in a single action class and make that extend the ActionSupport. And all action classes will then extend the base action class that I just created. My question is, are the instance variables on the base action class thread-safe? Does S2 manage the base action class at all?
Also what makes an action class an action class to be managed by the S2 and instantiated per request? Getting declared in the struts.xml? Extending ActionSupport class?
I think you're a bit confused about how Java works.
If you have a class A and a class B extends A, when you instantiate B there is a B. It's not like there's a single instance of A backing all instances of B. There's no "management of base classes".
Classes declared as actions either via XML, annotations, or convention are instantiated by the Struts action instantiation mechanism. Extending ActionSupport has (almost) nothing to do with it, the only time it might have something to do with it is because ActionSupport implements the Action interface.
Also what makes an action class an action class to be managed by the S2 and instantiated per request? Getting declared in the struts.xml? Extending ActionSupport class?
Getting declared in the struts.xml: yes, is that that turns a Java class into an Action.
And every Action class is thread-safe because it's ThreadLocal, no matter what it extends nor implements. Every request of every user will have its own copy of each Action class.
P.S: The other classes (not declared in struts.xml) are not "(action classes) not managed by S2 and instantiated per request", they're simply NOT Actions.
I have two classes implemented the same static methods like the following,
Class A
{
static OpB();
}
Class B
{
static OpB();
}
Can I use Ioc pattern to switch ClassA and ClassB using the configuration?
Or any workaround or suggestions?
The IoC pattern pretty much prohibits you from using static methods because it focuses on creating instances of a type (service). Static methods are not instance methods, so an IoC container would give you an instance but you can't call a static method on an instance.
If it's possible to drop the static qualifier on the OpB() method you'll be able to do what you want using IoC.
Would like to know if methods need to be static in a C# assemble to be access from SSRS?
No, you can use both public methods and static methods in a c# class library and reference them from your SSRS report.
You do need to add static methods in a different way than your public instance methods though. You should check out this MS article on custom code use in SSRS. Here is the gist of how to add a static method:
The Classes section is only for
instance-based members. It is not for
static members. Static (also referred
to as "shared" in some of our
Reporting Services documentation)
means that the member is available to
every instance of the class and every
instance uses the same storage
location. Static members are declared
by using the shared keyword in
Microsoft Visual Basic and the static
keyword in C#. This can be a bit
confusing. What this means is, if your
custom assembly contains instance
members that you need to access, you
will have to specify the class name
and instance name in the Classes
section. Because the method I will be
calling from Reporting Services was
defined as static by using the shared
keyword in Visual Basic, I'll use the
References section instead of the
Classes section.
So, if you want to do an instance method, make sure to add the refrence, but also specify a "Class" and "Instance name" in the Classes section of Report Properties for every method you need. Then call them using an expression of =Code. Like so:
=Code.InstanceName.Method
Hope that will help you out.