Tuesday 24 October 2023

Hosting ASP.NET Web API REST Service On IIS 10

 To make any application accessible to the end-user, we need to host it on a server. So, in this article, we will learn how to host ASP.NET Web API REST Service in IIS 10. The following steps are required to host any application. 

I hope, you have learned how to create and publish ASP.NET Web API REST Service. Now, let's start hosting Web API REST Service in IIS 10.

Step 1 Install IIS
 
Before hosting any application, make sure that IIS (Internet Information Services) is installed on your Hosting Server. If it is not installed or enabled, then follow the below steps.
  • Click on Windows icon at the bottom left corner and find the "Control Panel" menu and click on it.

  • In the "Control Panel", find the "Program and Features" option from the displayed list.

  • This shows the list of installed programs. Find and click the "Turn Windows feature on or off" option from the left pane, as shown in the following image.

     
  • Now, in the next popup, find Internet Information Services (IIS) and check on it.
  • Click "Next" button and then "Finish". It takes a few minutes to install IIS.
  • After installing IIS, restart your machine.
Note - I have Windows 10 OS and the above process is for Windows 10 PC. The steps might differ depending on your Operating System version.
 
Step 2 Install .NET Framework
 
Although Visual Studio is not required on your hosting Server, it must have .NET Framework installed, as per the .NET framework used to develop the application. If it is not installed, then please install the appropriate .NET Framework available on Microsoft official website.
 
I assume, you have installed the .NET framework on your Hosting Server.
 
Step 3 Move the published code on Hosting Server
 
Copy the "Published files" and paste those on respective Servers where you want to host the Web API REST Service. In our last article, we published Web API REST Service in the E drive of my Server, as shown in the following image .
 
Step 4 Open IIS Manager
 
Now, open the IIS Manager from Windows menu or in any other ways you have known.

 
The above image is of IIS 10 Manager of my Windows 10 machine. The view as well as options might be different on your machine depending on the OS version.

Step 5 Add Website
 
Right click on "Site" in IIS and click on add new website, as shown in the following screenshot.
 

After clicking on "Add Website" option, it displays the following configuration window.
 
 
 
I hope you understood the preceding configuration by highlighted text.
 
Step 6 Define Site Name & Application Pool
 
Define the site name which will be useful to uniquely identify the site within the IIS Server. After specifying the site name, choose the Application Pool from available pools. You can even create a custom application pool with any desired name. Currently, our IIS Manager has the following Application Pools.

 
Choose the application pool depending on your application configuration. In this article, we are going to choose DefaultAppPool.

Step 7 Browse and select Published Folder path
 
Now, choose the physical location of published code files by clicking on "Browse" button, as shown in the following image. 
 
 
 
Now, click on "OK" button.
 
Step 8 Define IP address & Port
 
Choose one IP address from the list of available IP addresses and define the unique port number for the application, which will be unique within the defined IP address.

Step 9 Choose Protocol & Host name (optional )
 
Choose the protocol for your application i.e HTTP or HTTPS which requires port 443 to be open and choose the Host name which will be used publicly to access the application. After defining all the configurations, the web site configuration window will look like this.
 
 
Now, click on OK button. It will create and add the application in IIS.
 


Step 10 Browse the URL with REST Client
 
Browse the hosted application using REST client with base URL along with API, Controller name, and Action method name, with pattern defined in routing template inside the webapiconfig.cs file.
 
We can browse applications using the browser but then, we can only test the GET method. So, to test all types of Action Methods, we are using  advanced REST client which is a developer extension tool in Firefox browser.
 
Our hosted Web API REST Service includes these two methods, as given below.
  • GetAllEmployees (GET )
  • GetEmployeeById  (POST ) which takes id as input parameter
Browse GetAllEmployees method using Advanced REST client which has used HTTP GET verb.
 


In the preceding screenshot, the GetAllEmployees hosted Web API REST method is returning all the employee lists.
 
Now, browse GetEmployeeById method using Advanced REST client which uses HTTP POST verb and takes id as input parameter.
 
 
 
From the above desired output, it's clear that our Web API REST Service is hosted on IIS successfully.

Note
  • Download the Zip file of the Published code to learn and start quickly.
  • This article is just guideline to show how to host Web API REST Service on IIS .
  • Optimize the speed by setting debug as false etc., from web.config file as per your skills. 
  • In this article, the optimization is not covered in depth.
  • Configure the authentication in IIS as per your REST Service .

For more information: https://www.c-sharpcorner.com/article/hosting-asp-net-web-api-rest-service-on-iis-10/

Wednesday 18 October 2023

Pinging a Reader with C#

 Here's a handy way to determine if a reader is connected to the network or not. It uses the .NET Ping and PingOptions classes, so make sure to add the following namespace to your project.

 

using System.Net.NetworkInformation;

//Then, you can write a method like this, that returns true if the reader is connected and false if it is not.

private bool ReaderIsAvailable(string address)
{
    Ping pingSender = new Ping();
    PingOptions options = new PingOptions();
    options.DontFragment = true;
    string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    byte[] buffer = Encoding.ASCII.GetBytes(data);
    int timeout = 120;
    PingReply reply = pingSender.Send(address, timeout, buffer, options);
    if (reply.Status == IPStatus.Success)
        return true;
    else
        return false;
}

Finally, this is how you would call the method.

if (ReaderIsAvailable("SpeedwayR-10-25-32"))
{
    // Reader is connected to the network.
    // Start reading some tags!
}
else
{
    // Reader is not connected to the network.
    // Handle it here.
}

Sunday 8 October 2023

Android – Can’t make spinner’s scrollbar always visible (Android)

 I have such a problem – I want to make spinner's scrollbar always visible.

But for spinner function

setScrollbarFadingEnabled(false);

causes crash with a NullExceptionPointer during drawing the GUI.

XML tags can't solve this problem too – it seems that spinner just ignore them.

Maybe there are another ways to move? For example, using of the custom scrollbar? If yes, how can I do this?

---------------------------------------------------------------------------------------------------------------

As I've been asked, here is LogCat error message for simple project with just a spinner in it:

AndroidRuntime(2252): FATAL EXCEPTION: main
AndroidRuntime(2252): java.lang.NullPointerException
AndroidRuntime(2252): at android.view.View.onDrawScrollBars(View.java:5836)
AndroidRuntime(2252): at android.view.View.draw(View.java:6799)
AndroidRuntime(2252): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
AndroidRuntime(2252): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
AndroidRuntime(2252): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
AndroidRuntime(2252): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
AndroidRuntime(2252): at android.view.View.draw(View.java:6796)
AndroidRuntime(2252): at android.widget.FrameLayout.draw(FrameLayout.java:352)
AndroidRuntime(2252): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
AndroidRuntime(2252): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
AndroidRuntime(2252): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
AndroidRuntime(2252): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
AndroidRuntime(2252): at android.view.View.draw(View.java:6796)
AndroidRuntime(2252): at android.widget.FrameLayout.draw(FrameLayout.java:352)
AndroidRuntime(2252): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2078)
AndroidRuntime(2252): at android.view.ViewRoot.draw(ViewRoot.java:1433)
AndroidRuntime(2252): at android.view.ViewRoot.performTraversals(ViewRoot.java:1175)
AndroidRuntime(2252): at android.view.ViewRoot.handleMessage(ViewRoot.java:1753)
AndroidRuntime(2252): at android.os.Handler.dispatchMessage(Handler.java:99)
AndroidRuntime(2252): at android.os.Looper.loop(Looper.java:123)
AndroidRuntime(2252): at android.app.ActivityThread.main(ActivityThread.java:4632)
AndroidRuntime(2252): at java.lang.reflect.Method.invokeNative(Native Method)
AndroidRuntime(2252): at java.lang.reflect.Method.invoke(Method.java:521)
AndroidRuntime(2252): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
AndroidRuntime(2252): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
AndroidRuntime(2252): at dalvik.system.NativeStart.main(Native Method)

Best Solution

Spinner does not have a scrollbar, Hence you are getting NullPointerException.

The popup shown by spinner has a scrollbar. So you need to change the properties of the ListView shown by spinner. But ListView itself is not exposed by Spinner by any public methods.

Even if you get the ListPopupWindow by reflection, a further problem arises, that ListPopupWindow's ListView is only created after you click the Spinner.

But OnClickListener of the Spinner cannot be registered , to set the ListView Properties after the show.

You could create a Custom Spinner with performClick overridden and then get mPopup by reflection. and use mPopup.getListView().setScrollbarFadingEnabled(false)

But If you are going to create a custom Spinner, i believe it is easier to implement the whole popup to

Friday 6 October 2023

How to always display vertical scrollbar in spinner when open it? Explain with example

 The vertical scrollbar in a Spinner widget in Android is typically displayed automatically when there are enough items in the Spinner to require scrolling. Android does not provide a built-in option to always display the vertical scrollbar when the Spinner is open. However, you can create a custom Spinner-like view that always shows the scrollbar when opened.

Here's an example of how you can achieve this by creating a custom Spinner-like view using a PopupWindow and a ListView:

  1. Create a Custom Spinner Layout:

    Create a custom layout for your Spinner-like view (e.g., custom_spinner_layout.xml). In this layout, you can include a ListView with vertical scrollbars. Here's a basic example:

-------------------------------------------------------------------------------------------------------

<!-- custom_spinner_layout.xml -->

<ListView

    android:id="@+id/customSpinnerListView"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:scrollbars="vertical" />


---------------------------------------------------------------------------------------------------------------

Create a Custom Spinner Class:

Create a custom class (e.g., CustomSpinner) to manage the behavior of your custom Spinner-like view. In this class, you can create a PopupWindow to display the custom view:

-------------------------------------------------------------------------------------------------------------

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.PopupWindow;


import java.util.List;


public class CustomSpinner {

    private final Context context;

    private final PopupWindow popupWindow;

    private final ListView listView;


    public CustomSpinner(Context context, List<String> itemList) {

        this.context = context;

        View customView = LayoutInflater.from(context).inflate(R.layout.custom_spinner_layout, null);


        popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        popupWindow.setFocusable(true);

        listView = customView.findViewById(R.id.customSpinnerListView);

        ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, itemList);

        listView.setAdapter(adapter);

    }

    public void show(View anchor) {

        popupWindow.showAsDropDown(anchor);

    }

    public void dismiss() {

        popupWindow.dismiss();

    }

}

-------------------------------------------------------------------------------------------------------------


Using the Custom Spinner:

In your activity, you can create an instance of the custom spinner and display it when needed. For example:

-------------------------------------------------------------------------------------------------------------

List<String> items = new ArrayList<>();

items.add("Item 1");

items.add("Item 2");

items.add("Item 3");


CustomSpinner customSpinner = new CustomSpinner(this, items);


// Display the custom spinner when a button is clicked

Button openSpinnerButton = findViewById(R.id.openSpinnerButton); // Replace with your button

openSpinnerButton.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

        customSpinner.show(v);

    }

});


-------------------------------------------------------------------------------------------------------------

  1. In this example, when the button is clicked, the custom spinner is displayed, and it will always show vertical scrollbars when open.

By creating a custom Spinner-like view with vertical scrollbars, you can control the scrollbar's visibility and display it whenever the custom Spinner is opened.


Monday 2 October 2023

Android spinner increment number

 public void addItemsOnSpinner() {


        spinner = (Spinner) findViewById(R.id.spinner);
        List<String> list = new ArrayList<String>();
        for (int i=0; i<=255; i++){
            list.add("list " + i);
        }
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);
    }

What is Formal Education? Explain with example.

 Formal education refers to structured and organized learning experiences that take place within an established institution, typically following a predefined curriculum, syllabus, and a set of rules and regulations. This type of education is characterized by its systematic approach, assessment methods, and the issuance of recognized certificates or degrees upon successful completion of the program.

Key features of formal education include:

  1. Institutionalized Setting: Formal education occurs in institutions such as schools, colleges, universities, and vocational training centers. These institutions have specific guidelines, schedules, and facilities dedicated to education.

  2. Structured Curriculum: Formal education follows a planned and structured curriculum that outlines what subjects or topics students should learn and in what order. This curriculum is often designed by education experts and approved by relevant authorities.

  3. Qualified Instructors: Trained and qualified teachers or professors lead formal education programs. They are responsible for imparting knowledge, providing guidance, and assessing students' progress.

  4. Assessment and Evaluation: Formal education involves regular assessments, examinations, and grading systems to measure students' understanding and performance. Grades and transcripts are used to evaluate and record their academic progress.

  5. Standardized Certification: Successful completion of formal education programs typically leads to the award of certificates, diplomas, or degrees, which are recognized and accepted by employers, other educational institutions, and society at large.

  6. Duration: Formal education programs have a specific duration or duration range, which can vary depending on the level of education (e.g., primary, secondary, tertiary) and the specific course or degree pursued.

Example of Formal Education:

Let's consider an example of formal education in the context of a university degree program:

Bachelor of Science in Computer Science

  • Institution: A university, such as XYZ University, offers a Bachelor of Science (B.Sc.) program in Computer Science.

  • Curriculum: The university has a predefined curriculum for this program, which includes courses in programming languages, algorithms, database management, and computer hardware, among others. Students are required to complete a certain number of credits in these courses.

  • Instructors: Qualified professors with expertise in computer science teach these courses, providing lectures, assignments, and lab sessions to students.

  • Assessment: Throughout the program, students are assessed through quizzes, assignments, mid-term exams, and final exams. Their performance is measured, and grades are assigned.

  • Certification: Upon successful completion of all required courses and meeting the university's graduation requirements, students are awarded a Bachelor of Science degree in Computer Science. This degree is recognized by employers and can be used as a qualification for various job opportunities in the field.

In summary, formal education is a structured and standardized form of learning that occurs within educational institutions, offering recognized qualifications upon completion. It plays a crucial role in providing individuals with the knowledge and skills needed to pursue careers and contribute to society.