European Windows 2012 Hosting BLOG

BLOG about Windows 2012 Hosting and SQL 2012 Hosting - Dedicated to European Windows Hosting Customer

WCF with Free ASP.NET Hosting - HostForLIFE.eu :: Creating a WCF Data Service

clock May 4, 2015 07:47 by author Rebecca

Here in this tutorial, you are going to follow a simple step by step approach to create a WCF Data Service. You need Visual Studio 2010 IDE & SQL Server in your machine as prerequisites for building WCF Data Service. It would be nice if you have the basic .NET knowledge. Let's get started!

Step 1

Open Visual studio 2010 and create a Web -> ASP.NET Web Application with a language of your choice, either C# or VB. I’ve used C# throughout the lesson. The reason why we are creating an ASP.NET Web application is because in order to host and consume the WCF Data Service we need a hosting and client application.

Step 2

Once the application is created, you need a DataModel where it is going to be served. So let’s create a SQL Server database. Right click on the App_Data -> Create SQL Server database and define the schema as below and let’s feed some values into our newly created database table.

Step 3

You have your Database and Table ready, now it’s time for you to create an Entity Data Model. Right click on the project and Add New item -> Select ADO.NET Entity Data Model, a Wizard pops up, proceed further with it. Below screenshots helps you building a Model.

Step 4

Now, you are done with Entity Model and for our Schema structure, this how the Model looks:

Step 5

You are good with our basic implementation in-order to adopt a WCF Data Service into our application. Ready to go. Let’s create a WCF Data Service. Right click on the Project -> Add New Item -> Select WCF Data Service.

Step 6

WCF creates us a basic template code for us to start with.

Step 7

Before moving forward, let us keep our Database Entity name handy. It can be found in Web.Config under Connection String.

Step 8

Here you have replaced the template code with EntityName and Set of rules which decides the rights of access.

Step 9

If you run the application or just hit F5 selecting the WCF Data Service file. It opens up a browser and navigates us to http://localhost:/WCFDataFilename.svc . You can see a XML document with title holds to the Entity Name we created.

Step 10

To get the full database values as a XML model, just postfix the Entity Name with URL. As below:

Step 11

Quite simple right! You do have some syntax approach in accessing the resource via OData. For example, if you need a Single record, access it like below:

 

 

HostForLIFE.eu WCF with Free ASP.NET Hosting
Try our WCF with Free ASP.NET Hosting today and your account will be setup soon! You can also take advantage of our Windows & ASP.NET Hosting support with Unlimited Domain, Unlimited Bandwidth, Unlimited Disk Space, etc. You will not be charged a cent for trying our service for the next 3 days. Once your trial period is complete, you decide whether you'd like to continue.



WCF with Free ASP.NET Hosting - HostForLIFE.eu :: How to Create and Host a WCF Service in a Console Application

clock April 20, 2015 06:52 by author Rebecca

In this tutorial, I will explain to Self Host your WCF Service in a Console Application step by step. In order to host a WCF Service, you  need a managed process, a ServiceHost instance and an endpoint configured forWCF Service. You can host aWCF Service in following different possible ways:

1. Hosting in a Managed Application/ Self Hosting

  • Console Application
  • Windows/WPF Application
  • Windows Service

2.  Hosting on Web Server

  • IIS 6.0 (ASP.NET Application supports only HTTP)
  • Windows Process Activation Service (WAS) i.e. IIS 7.0 supports HTTP, TCP, NamedPipes, MSMQ

Self Hosting a WCF Service in a console application is comparatively easy as well as flexible because we can achieve the purpose by writing few lines of code.

Step 1

Let’s first Create a Simple WCF Service i.e. a StudentService and then host in a Console application. StudentService having service operation GetStudentInfo that takes StudentId as parameter and returns student name. Open Visual Studio and Create a new Class Library Project, name it as “StudentService” and press “OK” button.

Then, right click on project and Add a new “WCF Service” to this Class Library Project.

It will add Service Contract (IStudentService) and it’s implementation class (StudentService) to class library project. Also, it will add a reference to System.ServiceModel.
Code for IStudentService interface will be as follows:

 [ServiceContract] public interface IStudentService
 {
     [OperationContract]
     string GetStudentInfo(int studentId);
 }
And following is the code for StudentService implementation class:
      public class StudentService : IStudentService
     {
         public string GetStudentInfo(int studentId)
         {
             string studentName = string.Empty;
             switch (studentId)
             {
                 case 1:
                     {
                         studentName = “Your Name”;
                         break;
                     }
                 case 2:
                     {
                         studentName = “Your Name”;
                         break;
                     }
                 default:
                     {
                         studentName = “No student found”;
                         break;
                     }
             }
             return studentName;
         }
     }

Step 2

In order to host this service in Console application, let’s add a new console application project “StudentHost” to this solution.

Your console application will have reference to:

  •     StudentService class library
  •     System.ServiceModel.

At the start of this WCF Tutorial, we discuss that hosting a WCF service requires a Managed Process (i.e. console application), Service Host (an instance of ServiceHost class) and one or more Service Endpoints. Detailed implementation of Student host application is as follows:

    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost studentServiceHost = null;
            try
            {
                //Base Address for StudentService
                Uri httpBaseAddress = new Uri(“http://localhost:4321/StudentService”);
               
                //Instantiate ServiceHost
                studentServiceHost = new ServiceHost(typeof(StudentService.StudentService),
httpBaseAddress);
 
               //Add Endpoint to Host
                studentServiceHost.AddServiceEndpoint(typeof(StudentService.IStudentService),
                                                        new WSHttpBinding(), “”);           
 
               //Metadata Exchange
                ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior();
                serviceBehavior.HttpGetEnabled = true;
                studentServiceHost.Description.Behaviors.Add(serviceBehavior);
 
                //Open
                studentServiceHost.Open();
                Console.WriteLine(“Service is live now at : {0}”, httpBaseAddress);
                Console.ReadKey();               
            }
            catch (Exception ex)
            {
                studentServiceHost = null;
                Console.WriteLine(“There is an issue with StudentService” + ex.Message);
            }
        }
    }

Now, simply build the console application and run it after setting as startup project. You will see the following screen that shows our self-hosted WCF Service is running.

HostForLIFE.eu WCF with Free ASP.NET Hosting
Try our
WCF with Free ASP.NET Hosting today and your account will be setup soon! You can also take advantage of our Windows & ASP.NET Hosting support with Unlimited Domain, Unlimited Bandwidth, Unlimited Disk Space, etc. You will not be charged a cent for trying our service for the next 3 days. Once your trial period is complete, you decide whether you'd like to continue.



About HostForLIFE.eu

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2016 Hosting, ASP.NET Core 2.2.1 Hosting, ASP.NET MVC 6 Hosting and SQL 2017 Hosting.


Tag cloud

Sign in