NullReferenceException while calling WebMethod - c#

I have the following problem;
My console app is running on the server and all I want to do is control it over ASP.NET Web Service.
I added new ASP.NET Web Service project to my Solution where my main console app and added reference to it.
The problem is every time WebMethod calls function from console app, i get the nullreferenceexception. Even if I try to use static classes or singletons; every object is null, although my console app is running absolutely correctly.
Should I change some permisions setting or something?
Thank you for your time.

Based on your comments - your console app needs to expose API to administer it. Now this can be possible by using WCF Web Services where your console app needs to host the WCF Services. Controlling console app from ASP.NET web services would be difficult because ASP.NET web services would be hosted in different process/AppDomain (by IIS) - so they somehow need to talk console app process. This across process talk is possible in .NET using remoting or WCF. So its better to use directly WCF to provide web based API (instead of using ASP.NET web services).
Refer this article to start with creating simple WCF service and hosting it in console.

Related

Should a hosted service be used in an ASP.NET Core Web API project when that service has to run before requests are made?

I've added a hosted service to my ASP.NET Core Web API project:
services.AddSingleton<IHostedService, ScheduledProcessingService>();
However, Main() and Startup don't get hit until a request comes in. That means my hosted service doesn't start until then. When running locally, when I start the app, I have to make a call to the API controller (which I'd like not to have to do) in order for the hosted service to run.
If my hosted service has to process, regardless of whether a web request comes in, is it wrong to put that service in a Web API project? Should I create a separate project for it?
I could change some IIS settings, like in this post, but I want to make sure I'm doing what's right.
Unless someone provides more clarity, I think this is the answer:
It's hosted in IIS so it's just how it works. To overcome this, I think we'd have to set up some automatic ping, hangfire.io, or simply putting it in its own project that is just a service.
Another option would be to change these settings on the app pool:
.NET CLR version: v4.0 (instead of no managed code)
Start mode: always running
Idle time-out: 0
Another suggestion:
We use the app initialization feature in IIS to start and keep the services running.

API written in C# using nancyfx running on WampServer

I am still relatively new to writing API's and web services, so bear with me if I use incorrect terminology.
I want to know if it is possible to create an API in C# using nancyfx (or any other framework) or even just a simple web app and then set it up as a continually running web service using WampServer. I'm pretty new to Wamp, all I have done so far is create a few rudimentary pages with php; but I can't seem to find any information about running a web service with Wamp using a different back end language other than php.
WAMP is stand for Windows Apache MySQL PHP. As I can say, you can't run a C# web service with it base configuration. However, you actually don't need this. If you are on .net Core, there is a Kestrel web server. You can use it to serve your app. On a .net framework you have a HttpListener which can help you to implement self-hosted web service (for example, a windows service which hosts your application).
There are many other options, btw. Try google for some kind of "Hosting .net web application" or "Self-hosted .net web application"

Web Api and web ui in same application/service

For my AngularJS web application I use Azure Service Fabric as my backend. For that I created a stateless web api service.
Do I have to put the frontend /client-side AngularJS code in the same application type or the same stateless service? Is it useful to create another stateless service for the web ui?
As others have mentioned, you can host your Angular web app anywhere you like; there are no restrictions imposed by Service Fabric in this regard.
That said, in your case a simple solution is to have your front-end web app and your web api in the same stateless service. Basically you treat this as a regular old ASP.NET MVC application, and ASP.NET MVC makes it fairly easy to have an API and web UI in the same MVC project. Here are a couple examples of this:
ASP.NET 5 MVC application with an API and a UI: https://github.com/Azure-Samples/service-fabric-dotnet-getting-started/tree/master/Services/Chatter
Single-page jQuery app with an API self-hosted with Katana: https://github.com/Azure-Samples/service-fabric-dotnet-management-party-cluster/tree/master/PartyCluster
As to the question of why do you need Service Fabric to host a front-end file. There are trade-offs here. Assuming the front-end is a web application that's meant to be part of a larger Service Fabric application - as it is in your case - it's nice to have everything in one place using one set of tools on the same SDK with the same deployment process hosted on the same cluster, rather than having a completely separate process, tools, SDK, and hosting environment for one part of your application. You also get all the Service Fabric goodness like rolling upgrades, health monitoring, high availability and auto failover, etc. The downside is that you don't get the super easy-to-use tools for web applications that come with, say, Azure App Service.

.net web service hosted within my application

I'm migrating an old Delphi application that I wrote into C#. The application is a datalogger that exposes logged data requests via a SOAP web service interface.
The web service is contained with the delphi graphical windows application, i.e. no need to run a web server like IIS, etc I just run the application and it's up and running under the hood.
I'm looking to do the same in my c# Windows form application, I can find loads of resources on writing web services that are ultimately hosted within IIS but am struggling to find a solution for a self contained web service within my application.
Does anyone have any suggestions or can point me towards any resources on this?
The web service does not neceserily have to be SOAP, REST is fine (in fact probably prefered).
Look into WCF Services.
Hosting and Consuming WCF Services
Hosting WCF services in a Windows Forms Application
The System.Web.Hosting namespace allows you to host ASP.Net pages without using IIS within your applications. I have never used it to host web services but I found a tutorial that seems to provide a guide on doing this-
http://msdn.microsoft.com/en-us/magazine/cc163879.aspx
If you're wanting to host a service inside your application, it's possible with the System.ServiceModel.ServiceHost class. You need to learn WCF first, but that at least answers your question to get you started. If you have any further questions, leave me a comment or two and I'll update my answer to accommodate your inquiries.

Calling ASP.net Web Service from C# Application

I have a question. How can i invoke a web service and get the result from a C# desktop application. I am making a desktop app and I want it to be able to connect to my online ASP.net web services. How is this possible?
In Solution Explorer, right-click your project node and select Add Service Reference.
Enter the URL where your service WSDL is located. This is usually the URL of the service itself.
This generates a strongly-typed proxy class in a new Services References folder in your project.
Write code in your desktop app to instantiate the proxy class and invoke methods on it. The rest works like magic. :)
AB Kolan was also correct, but Add Web Reference uses the old-style web services framework whereas Add Service References uses the new WCF stack. Important note: It is not required that the service itself use WCF for you to use WCF on the client side. WCF on the client is typically the best choice for any service, provided you can take a dependency on .NET 3.0 and above.
Add a Web Reference to the webservice in your Desktop App project reference. Doing so would generate a Proxy for the Webservice called Reference.cs
You can access your webservice using the proxy.
This is possible the same way that you access web services from any other type of application, be it an ASP.NET page, a class library or windows service.
For an explanatory tutorial on the subject, see Accessing a Web Service from a Desktop Application.
Will get help how to create a webservice and consume that service:
http://www.c-sharpcorner.com/UploadFile/0c1bb2/consuming-web-service-in-Asp-Net-web-application/
Thanks

Categories

Resources