I have a class library (c#) with many methods that call the same web service (asmx).
What is the best practice for instantiating the web service.
Instantiate the web service once and pass it as a parameter to each method
Or instantiate and dispose the web service in each method.
What you are instantiating is a local proxy class that calls the service, so it isn't as costly as you may think.
As web services are supposed to be stateless, either method would work. I doubt you will see much of a difference in performance.
It seams like a bad practice to make a new instance of the service, hook up complete events each time you have to call a service method
usually i make an instance variable
and then instantiate the service in the constructor and hook up all complete events there
and only call the methods when needed
this approach works well, except if you do it in an User-control it breaks the Visual Studio Designer
Related
I have a WCF-based, C# CRUD REST api that works on Schedule objects. There are methods for creating, updating, deleting as you'd expect.
My problem is that the Schedule object contains a TriggerInfo subobject. If you just call the constructor, you're really calling the constructor of a proxy, and the real constructor is never called, so the subobjects are not initialized.
The proxy that WCF emits has TriggerInfo as a field but it's always going to be null because the constructor logic in the "real" class is never called.
In other words, when the client creates a C# 'Schedule' object, it's really creating a proxy of the real Schedule class, and the proxy knows nothing about having to init anything!
So in this chicken-and-egg situation, who creates the C# 'Schedule' object that the client can "fill out"?
I thought the C# client could create a Schedule object, fill out all the properties and pass it to the CreateSchedule() api and it'd work. Not so easy!
It'd work if I made a big, flat monolithic class where all of the TriggerInfo properties were properties on the Schedule object instead, but it's not very tidy, especially if you have multiple subclasses.
I could have a ScheduleFactory object exposed on my API that knows how to create one, but I don't know if that's a valid approach!
Don't create Schedule object client-side if it needs any nontrivial initialization - just add a New or Create method to your WCF service and do it server-side. Alternatively, you can use new Schedule() client-side, get a new proxy instance with a lot of null properties and fill in these properties with sensible default values server-side in Save method.
I have an API project created in C#. There's a desire to "simplify" something that it does which would mean creating a new API endpoint, which in the background would call several existing endpoints within the same API.
I'm concerned that this sort of recursion, an API calling itself, is bad practice and it'd be a better solution to have applications that make use of the API call the existing endpoints individually and manage the returned data within their own separate application logic. Am I right to be concerned?
Thanks
There is a whole lot of "it depends" in the answer.
If you moved logic out of your controllers and into shared libraries would it make sense for these two libraries to call one another directly, or would they be in the same library?
If they're in the same library and the data they access should all be in that same service, then I would call within the library.
I have changed a former WCF one-way service to a duplex service so I can implement callbacks.
My actual problem is since that change, every piece of code that instanciates the service proxy needs to be modified to pass an instance context as parameter into the constructor.
There are many, many places in a few different projects that make use of that service. Everyone of them is now broken.
Will I be forced to go back in every proxy instanciation and pass an instance context into the constructor ? Can I avoid this ?
Simply speaking - no you cannot avoid this.. Presumably you need the duplex service for one WCF call to be able to callback... I would probably just create a separate method on the interface rather than changing the existing one so that you don't break the contract between service and client.
I've got a situation where I have several web services that I need to consume. I need the ability to perform custom actions in the constructor of the proxy before any calls are made (assigning the configured URL, assigning the SOAP header, etc.).
My first solution is to create a child class that derives from the generated proxy, then make those actions in the constructor of the child class. That way, app code can call the constructor of the child, and get a valid proxy that has the stuff I need.
I'm trying to prevent the app code from calling the constructor of the generated proxy, so people don't accidentally instantiate the proxy without doing my custom stuff. My first thought is to move the generated code into a separate assembly from the child, and make sure the app code only has a reference to the child assembly. This works for the most part, but...
The services contain complex types, defined in the proxy. I need the app code to reference these classes, which means the app code needs a reference to the base assembly anyway, which means they now have access to the generated constructor.
I've tried an overly-complex solution of wrapping each of the generated complex types in an interface, and then hiding the real calls and replacing them with copies of the object as the interface type. This worked once or twice, but it gets ugly really quick.
It seems that the only way I can have everything I want is to remove the public constructor of the generated proxy, and replace it with a protected constructor, then allow a reference to this assembly - they'll be able to work with the complex types, but won't be able to call the constructor. My problem is that the only way I can think of to do this is to manipulate the generated code to change the constructor.
Any ideas? I'm using WSDL.exe to generate the proxies, and there's no option there to hide the constructor. Is there another way that I'm just missing? I suppose I can write a tool to automatically modify the proxy immediately after it's generated, but that just feels ugly to me.
Thanks
Are you stuck using .NET 2.0? If not, then you shouldn't be using WSDL.EXE. You should be using SVCUTIL.EXE or "Add Service Reference".
Instead of creating a derived class, you should create your own wrapper classes, which use the proxy classes. One would use something like MyWrapper.CreateProxy(), which would return a properly-configured instance of the proxy class.
BTW, WSDL.EXE creates proxies using the legacy "ASMX" technology, which has no ability to use the types from the service.
I ended up going with modifying the proxy generated code to make the constructor protected instead of public. The call to WSDL.exe was handled in an automated project already, so it wasn't that big of a deal. This was really the only way I could get everything I wanted.
Instead of doing that, why can't you override the GetWebRequest method? It will be called before the service method call anyways.
If you have added a service reference, implementing message inspector will do same thing.
I just wanna learn why I can't static web methods in web services ? Why is it restricted ?
Can some body give me concise explanation of this.
The answer is: because you can't.
It's not designed that way. The design is that an instance of the web service class will be created, and then an instance method will be called.
I can only guess why Microsoft designed it that way. To know for sure, you'd have to ask them. Consider:
There's no particular benefit to permitting static methods. Anything you can do with a static method, you can also do with an instance method.
A [WebService] class is not meant to be some arbitrary class that happens to be used as a web service. It's meant to be a class that you created for the purpose of exposing web service operations. As such, there is no need to support classes that already exist and already have static methods.
The SOAP Header implementation permits your class to contain an instance field of a type deriving from the SoapHeader class. This field will be filled with an incoming SOAP header and/or will contain the SOAP Header to be returned. You could not do this with a static field, as it would be overwritten with each request.
As I said, these are all guesses. The correct answer to the question is, "you can't because that's how Microsoft designed it. If you want to know why they designed it that way, you need to ask them".
FWIW, I just checked, and it does not appear that WCF permits static methods to be operations either.
When a client creates an object for your web service, what they are really creating is a proxy object to that web service. This proxy object handles things like opening and closing your connections for you as well as all the overhead of actually working with the web service. A static method call would be difficult to manage. The "static proxy" for lack of a better word would have to do all of things that the instance of the proxy object is doing each and every time a client called one of the static methods, thus adding massive overhead.