ASP.NET Core Web app, How can I access HttpContext in startup class for localhost - asp.net-core-3.1

How can I get the HttpContext in the Startup.cs public void Configure?
this:
bool IsLocal = HttpContext.Request.Host.Value.StartsWith("localhost");
Gives me the error: CS0120: An object reference is required for the non-static field, method, or property 'HttpContext.Request'.
I need to know if the app is running local or on the server.

Related

rest webservice not receiving FromBody parameter when forwarded

I have a rest web service built with the Visual Studio wizard with a method like this:
public string Post([FromBody] string value)
{
...
}
and another parameterless one (provided by the wizard) like this:
public IEnumerable Get()
{
...
}
I have installed this in two separate locations on the web.
I can call the parameterless Get method and the FromBody Post method directly on each location from a .net console app and from a smartphone app with no problem. I may sometimes need to forward calls made to one location on to the other and this is where the problem comes. I find if I call the Post(FromBody) method on the service being forwarded that it is received by the location to which it has been forwarded without the parameter and so the second wizard generated parameterless method receives it by default.
I am using the RestSharp NuGet package.
The service being fowarded is being managed by Google domains and in the forwarding process I have specified the path to be included in the forwarding.
Is there some additional setting I need to specify in the calling code that will ensure that the FromBody parameter is passed on to be picked up by the ultimate destination rather than being discarded?
Any thoughts anyone has would be very welcome.
I was expecting the FromBody parameter to be passed on as part of the forwarding process

Autofac OWIN web api - load dependency based on request

How I can load a service dependency based on the route parameter?
My requirement is different, but I'll try to use a simple example.
A user can select the shipping provider (UPS, Fedex...) and the information is as part of the request model or route. Based on the route, I need to load the service class.
How it can be done in Autofac OWIN? Help on this will be appreciated
When you use the Autofac's OWIN integration, each request creates a new lifetime scope in which the current IOwinContext is registered, as you can see here.
You could then delegate the creation of your service to a factory that would take a dependency on IOwinContext.
public class MyServiceFactory
{
private readonly IOwinContext _context;
public MyServiceFactory(IOwinContext context)
{
_context = context;
}
public IService Create()
{
// inspect the context and determine which service you need
// you could return, dependending on, let's say, the URL
// - UpsService()
// - FedexService()
}
}
One thing you'll need to make sure is that you register your factory as InstancePerLifetimeScope since the IOwinContext will be different for each request.
Do you need to work at the OWIN layer, though? It will make things harder, and possibly a bit hacky, since OWIN is really just the HTTP layer, so there's no such thing as route data.
If you use ASP.NET Web API, you could base the factory on the current HttpRequestMessage if you use the RegisterHttpRequestMessage extension method.
You can then access route data through request.GetRequestContext().RouteData. Note that GetRequestContext is an extension method in the System.Net.Http namespace.
If you use ASP.NET MVC, you can register the AutofacWebTypesModule in the container, itself registering quite a few types in the container.
One of those is HttpRequestContext which has a RouteData property, so you can inject this one in the factory and apply your logic.

GWT save data in TextBox by clicking a Button

I have a simple application containing a TextBox and Button by using GWT, the point is that when I write something in the TextBox and click the Button the data in the TextBox would be saved in database, here is what I wrote, but I don't know how to save it by clicking:
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
//method to save the data
}
});
You need to implement a remote service to be used by your application.
GWT uses RemoteService to do server tasks. A remote service is an interface defining your remote methods, it must be placed in the shared classpath of your application. The remote service interface is implemented by the servercode to execute the actual action you want it to perform.
The client uses a so called Async interface, that is in the client class path and ususally generated for you by the IDE or a special compiler target. In mvn projects, you will find them in the target/generated-sources folder.
Those async interfaces that are implemented by the compiler and instantiated by your code using GWT.create().
The async interface has the same methods as the remote service interface, but all methods have a changed signature. Instead of a return value, they expose an AsyncCallback<T> handler, that is used to call your code after the remote call on the server returned. <T> is the return type of your method in the RemoteService interface on the server.
The proper way to save your data, is to have a presenter class or activity class that will have an instance of the async service. The activity/presenter will take the value from the view and send it to the remote service for storing into database.
In your click handler, you use the activity to save the data, or the presenter attaches himself as the click handler to the view.
There is plenty of code samples showing the principle in gwtproject.org tutorials section.
The call in the presenter/activity will use an async callback class like so:
backendSvc.saveData( textString, new AsyncCallback<Response>() {
void onSuccess(Response r) {
view.showMessage("Response saved");
}
void onError(Throwable caught) {
view.showError("Ouch", caught);
}
}
The view should not call the backend directly, this will lead to a mess, if your application grows.
I suggest, you check tutorials on the gwtproject website and really try to understand the flow of events. The concept is fundamental to understand the GWT way.
Please elaborate your question..I understood like how to save value in database from text box if it so then ,
Get the Value from Text Box
Send the Value to Server either by using any one of the following approach
RPC
RequestBuilder
Push the Data(value) to database
Example:
services.saveData(textValue, new AsyncCallback<Response>() {
public void onSuccess(Response response) {
showMessage("Saved Successfully");
}
public void onError(Throwable error) {
showError("Failed to Save Data");
}
};
If you are asking how to connect GWT to your server database, this link might be helpful.
XML: The bridge between GWT and PHP
Simply take the input value from textbox and post it to your server

AutoCompleteExtender not working in DotNetNuke

I am using DNN 6.0 and VS2008 AjaxControl Tool kit 3.5
I am trying to add ASP.NET AutoCompleteExtender to a text box in one of my modules in DNN.
As far as I know the AutoCompleteExtender works only through a webservice.
I've added a webservice, but I can not get the AutoCompleteExtender to work, I get no errors but the webservice never gets invoked, What am I missing or how can I get the ASP.NET AutoCompleteExtender to work?
Thank You
In my case it was the following problem:
ERROR MSG: Only Web services with a [ScriptService] attribute on the class definition can be called from script.
Try to put [ScriptService] as a definition of your web service class.
Example:
[WebService(Namespace = "http://dnndev/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class BusinessDataProvider : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod]
public string[] GetSpecificListOfContributors(string prefixText, int count)
{
return ContributorController.GetSpecificListOfContributors(prefixText, count);
}
}
I hope this one will help you.
Best,

Zend_Application issue with extending Frontcontroller and using extended Frontcontroller application resource

I extended the Zend Frontcontroller with a personal one and also extended the frontcontroller application resource to use my personal front controller. All it basically does for the moment is assign the front variable within the application resource method getFrontController to my personal front controller. Lastly, I added the pluginpaths variable within application.ini to use my personal Application Resources. In any case, I'm getting the Zend Frontcontroller returned to me instead of my personal one. Anybody know why my personal application frontcontroller resource
isnt being used?
`
Since Zend_Controller_Front is a singleton, you will also need to override the getInstance() method to ensure it creates an instance of your class instead of the base class. You can just cut and paste the method to do this:
public static function getInstance()
{
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}