If
you haven't created the WCF Service go to following link to create
the Service.
If
you created the WCF Service follow the following Step to Self Host.
Step
1:- Open Visual Studio 2010
Step
2:- Go to File menu => New => Project...
Step
3:- Left Pane Templates select Visual C# => Select Console
Application from Middle pane => Type SelfHost in the Name box =>
Click OK.
Step
4:- Right click SelfHost in Solution Explorer => Select Add
Reference.... => Select System.ServiceModel from the .NET tab =>
Click OK.
Step
5:- Right click SelfHost in Solution Explorer => Select Add
Reference.... => Select Browse tab => Give Service File path
"D:\DEMO\WCF\EmployeeWcfService\EmployeeWcfService\bin" =>
Click OK => Select EmployeeWcfService.dll => Click OK.
Step
5:- Add the following namespace
using
System.ServiceModel;
using
System.ServiceModel.Description;
using
EmployeeWcfService;
Step
6:- Create an instance of the Uri class with the base address for the
service.
Uri
baseAddress = new
Uri("http://localhost:52923/EmployeeService.svc");
Step
7:- Create an instance of the ServiceHost class, passing a Type that
represents the service type and the base address Uniform Resource
Identifier (URI) to the ServiceHost(Type, Uri[]). Enable metadata
publishing, and then call the Open method on the ServiceHost to
initialize the service and prepare it to receive messages.
// Create the ServiceHost.
using
(ServiceHost
host = new
ServiceHost(typeof(EmployeeService),
baseAddress))
{
//
Enable metadata publishing.
ServiceMetadataBehavior
smb = new
ServiceMetadataBehavior();
smb.HttpGetEnabled =
true;
smb.MetadataExporter.PolicyVersion
= PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
//
Open the ServiceHost to start listening for messages. Since
//
no endpoints are explicitly configured, the runtime will create
//
one endpoint per base address for each service contract implemented
//
by the service.
host.Open();
Console.WriteLine("The
service is ready at {0}",
baseAddress);
Console.WriteLine("Press
<Enter> to stop the service.");
Console.ReadLine();
//
Close the ServiceHost.
host.Close();
}
Complete
Code
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ServiceModel;
using
System.ServiceModel.Description;
using
EmployeeWcfService;
namespace
SelfHost
{
class
Program
{
static
void
Main(string[]
args)
{
Uri
baseAddress = new
Uri("http://localhost:52923/EmployeeService.svc");
//
Create the ServiceHost.
using
(ServiceHost
host = new
ServiceHost(typeof(EmployeeService),
baseAddress))
{
//
Enable metadata publishing.
ServiceMetadataBehavior
smb = new
ServiceMetadataBehavior();
smb.HttpGetEnabled =
true;
smb.MetadataExporter.PolicyVersion
= PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
//
Open the ServiceHost to start listening for messages. Since
//
no endpoints are explicitly configured, the runtime will create
//
one endpoint per base address for each service contract implemented
//
by the service.
host.Open();
Console.WriteLine("The
service is ready at {0}",
baseAddress);
Console.WriteLine("Press
<Enter> to stop the service.");
Console.ReadLine();
//
Close the ServiceHost.
host.Close();
}
}
}
}
Step
7:- Press Ctrl + F5 Or Use Visual Studio Start Debugging(F5) icon to
run the service.
If
you get the following error.
1. Check
you have administrative rights.
2. Run
Visual Studio instance as an administrator.
Step
:- Right Click Visual Studio Icon => Click "Run as
administrator"
Step
8:- If following screen appear, Your WCF service is running and
SelfHosted Successfully => Use following URL to Consume the
Service.
URL
:-
http://localhost:52923/EmployeeService.svc
Now
your WCF Service running, to check it follow the following link to
consume it.
For
More Info Visit microsoft.com
No comments:
Post a Comment