Reinitialize service in ASP.NET Web API - c#

I deployed my service within a self hosted (OWIN) ASP.NET Web API:
config.Services.Replace(typeof(IHttpControllerTypeResolver), new MyHttpControllerTypeResolver());
Because of some changes in the underlying system I would like to reinitialize the service (singleton; instance of MyHttpControllerTypeResolver). Is this possible?

Without seeing how your application is structured, I'll assume the line of code you posted is in a method called Configuration belonging to a class called Startup (or similar), and that somewhere something is calling WebApp.Start<Startup>(...).
Configuration is sort of a "magic" method name that WebApp.Start will look for and call. WebApp.Start returns an IDisposible. In order to re-initialize things, you'll need to capture that IDisposible in some variable (let's call it app), call app.Dispose(), and then call WebApp.Start again.

Related

How to hide some declared Methods in ASMX file in Web Service C#

i have one doubt in Web Service / WCF
i'm creating the service and it's having 10 methods respectively
test1() , Program1(int age),Describe1(), DisplayAge(string name),,SimilarInterest(),ServiceCall(), Hide(), Difference(), WebService() and Help()
now after hosting this service in asmx only the below methods should display. others should not need to display.
DisplayAge(string name),,SimilarInterest(),ServiceCall() only these three should display when i call the http://URL.asmx?wsdl
the other 7 methods should not need to display in asmx wsdl file .how to do that?
As far as I know, in XML Web Service(ASMX), this should not work if you want the service to be both invoked and not shown in the WSDL. Either using private decorated methods or removing the [WebMethod] attribute causes the method to no longer be invoked. If in WCF we can implement authentication, authorization that individual methods cannot be invoked, or simply not expose metadata. But we cannot hide the specified method (but can be called by the outside world).
https://social.msdn.microsoft.com/Forums/en-US/533e0361-e9e0-400b-a7b2-f098a9ef3e75/how-to-prevent-web-method-from-showing-on-service-description-page?forum=asmxandxml
Feel free to let me know if there is anything I can help with.

Calling an async web service repeatedly but never concurrently using C#

I need to call a 3rd party web service from within a console application using the asynchronous method provided by their API. I'm using the old ASMX web reference way to generate a proxy.
I have a class that does the following operations and I want to create an instance of it, make the call to the service with it, but then I want to wait for the completion of the callback and only then repeat with a new instance, new call, new callback etc.
I do not want to have more than 1 call active at a time.
i.e. only 1 instance of the class will exist at a time.
The web service calling code looks like this:
using(ABCWebService service = new ABCWebService())
{
...
service.ExecuteCallAndWaitResultCompleted += service_ExecuteCallAndWaitResultCompleted;
service.ExecuteCallAndWaitResultAsync(parm1, parm2, .., stateObj);
}
...
The callback looks like this:
void service_ExecuteCallAndWaitResultCompleted(object sender, ExecuteCallAndWaitResultCompletedEventArgs e)
{
// collect data and then move onto next sequential call
}
I highly doubt there is an "asynchronous method provided by [the] API". A web method is a web method. It's your call which is asynchronous, that is to say by calling an async function on your client proxy class you tell the Framework to (in basic terms) fire off a request to this resource on another thread and call you back when it has a result. Its your client call which is asynchronous.
In Visual Studio (2012), when you Add Service Reference and then click Advanced and then Add Web Reference to create a service reference based on what VS calls "code based on .NET Framework 2.0 Web Services technology", which I gather is how you've done it, the resultant client class has not just asynchronous methods for each web method but synchronous methods too - one for each web method in the service. These synchronous methods will not return anything or yield control back to the caller until they have the result.
If I have your usage case correct, you want your console app to call the web method, wait for a response, do something once the responses arrives, and then start again. In that case, instead of calling the async method (for example GetPeopleAsync) call the synchronous method (GetPeople) - possibly inside a loop.

Where is the instance of a WCF object stored?

I'm going to be creating a service that needs to make a call to a hosted WCF service halfway around the world. This isn't that big of a deal since the number of transactions that will be made is relatively low. However, I need to pass in an instance of a class that will possibly be defined in the WCF to the necessary WCF function.
So my question is, will that instance of the class exist on my server? Or will I be contacting the host server every time I attempt to set a variable in the object?
EXAMPLE:`
public class Dog
{
public string noise;
public int numLegs;
}
public class doSomething
{
public string makeNoise(Dog x)
{
return x.noise;
}
}
`
All of those are defined in the WCF. So when I create an instance of class Dog locally, will that instance exist on my side or the server hosting the WCF service? If I'm setting 1000 instances of Dog, the latency will definitely build up. Whereas if I DON'T have to contact the server every time I make a change to my instance of Dog, then the only time I have to worry about latency is when I pass it into doSomething.makeNoise.
The host creates a new instance of the service class for each request, if you're using the default per-call instantiation method (which is the recommended way).
So either this is the IIS server which hosting your WCF service that creates an instance of your service class, or it is the ServiceHost instance that you've created inside your own self-hosting setup (a console app, a Windows service etc.).
The service class instance is used to handle your request - execute the appropriate method on the service class, send back any results - and then it's disposed again.
There's also the per-session mode in which case (assuming the binding you've chosen support sessions) your first call will create a service-class instance, and then your subsequent calls will go to the same, already created instance (until timeouts come into play etc.).
And there's also the singleton mode, where you have a single instance of the service class that handles all requests - this is however rather tricky to get right in terms of programming, and "challenged" in terms of scalability and performance
You will need to host your WCF service on a public available server (for example IIS). Successful hosting will provide you with a link for the svc file. Clicking on that will give you a link ending in singleWsdl. You need to copy that link. On your client side, the one that requires a reference to the WCF, you will need to Add Service Reference and pass that link. This will generate proxy code with Client objects that you can use to access your WCF ServiceOperation methods.
At a minimum you should have three projects. A website project to host the actual site. A WCF project to host your services. And finally a shared project, which should contain the classes you are concerned with (the models).
Both the website and wcf projects should reference the shared project, this way they both know how the models look.
The wcf project should return serialzed models as json objects, which I usually do by referencing Newtonsoft.Json.
Your website project should expect this json, and deserialize them, also using Newtonsoft.Json. This is why your class (model) should exist in the shared project, so you can use the same class on both sides of your service call.

Synchronicity of web service calls

Are web service calls synchronous or asynchronous by default? How is synchronicity determined, by the service or by the client?
I have code similar to the following:
try
{
string result = MakeWebServiceCall_1(); // this is a third party webservice
MakeWebServiceCall_2(result); // another webservice which must happen *after* the first one is complete
}
catch()
{
SetStatus(Status.Error); // this calls my own stored procedure
throw;
}
SetStatus(Status.Sucess);
In the above, SetStatus is writing to the same tables that the third party web services read from. If I change the status before both web service calls have completed, it's going to make a big mess and I'm going to get fired. How do I know/ensure that the webservice calls are synchronous?
According to MSDN when you add a reference to a Web Service it will implement methods to call the Web Service both synchronously and asynchronously in the proxy class. You just need to make sure you call the right one.
After you have located an XML Web service for your application to access by using the Add Web Reference dialog box, clicking the Add Reference button will instruct Visual Studio to download the service description to the local machine and then generate a proxy class for the chosen XML Web service. The proxy class will contain methods for calling each exposed XML Web service method both synchronously and asynchronously. Source

ASMX Web Reference not Updating After Service Return Type Updated

I have a webservice - called MyCompany.WebService1
I reference this using a web reference in my ASP.net web application.
Theres a method called "GetDeal" in this web service, that returns a "Deal" Object.
The deal object currently looks (for example) like this:
public class Deal
{
Public string Name {get;set;}
Public string Description {get;set;}
}
This class is in a different assembly: MyCompany.Model
The web service references this assembly.
In my web app, I can call the GetDeal method.
This returns Service1.Deal (service1 is just the name of the web reference)
I can access both properties above.
I have now changed the Deal class, and added a couple more properties.
However, I can't see these new properties in my web application.
I've updated the web service in the web application.
I rebuilt the web service several times, tried removing the MyCompany.Model reference and re-addding it etc...
I can't figure out what has changed... This was working - I have changed the model before, and it's updated the reference correctly...
Anything I've missed?
As long as the following points are fulfilled, this should work:
the new property is marked as Public and must be read/write (must have a getter and a setter)
you have compiled the host web application (the web app which exposes the web service).
(You can try calling the web service in a web browser to check whether the new property is visible).
you have updated the web reference the client application (and rebuilt the app)
In addition to what Martin suggests, you have to actually run the updated service.
I recommend you look at the WSDL to see if the changes took effect. Add "?wsdl" to the web service URL in the browser, and look to see if your new properties appear in the XML Schema at the top.

Categories

Resources