Hello Everyone, In this article we will talk about how can
we create a new WCF Service in few seconds.
Open a new instance of visual studio and choose a pre
installed WCF service library template.
This will create a default Service library project and this will contain these files inside that.
- IService1.cs
- Service1.cs
- App.Config
Open App.Config file from the solution and check whether it contains
contract and end point information. Like below -
<services>
<service name="RestServiceTest.Service1">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8733/Service1/" />
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="RestServiceTest.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
You can change the base
address, binding and contract if you are not aware about these then not a
problem you can visit this article - WCF
Basics
IService1.cs - This contains
and Service Contract and Data Contract. You can change it anytime. IService1 is
the contract of your service so name of this interface and in the app.config
file should be same.
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO:
Add your service operations here
}
In Above code Composite type is a data contract.
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello
";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
Service1.cs - It's
a implementer class of the service and this is the base address of the service
as well. Make sure you implement all the interface methods. like below-
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered:
{0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
Now we are done with the
service rebuild the project and click on the run button. This will host the
service. Visual studio will take care of this. When you run this service a WCF
test client will be launched. Like below -
If you do not see your service projects it means that there
is some problem with your port. IF you are getting below error -
HTTP could not register
URL http://localhost:8733/Service1. Your process does not have access rights to
this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for
details).
It means that you do not
have administrator rights to this port. There are couple of solutions for this
-
·
Either you give yourself administrator rights to
this port
·
You can open a command line with administrator
rights and run this command -netsh http add urlacl url=http://+:8733/Service1
user=mylocaluser
If you get any error running this command like Create SDDL
failed then try to change the user from mylocaluser to Everyone like below -
netsh http add urlacl
url=http://+:8733/Service1 user=Everyone
If it shows url reservation successfully added then it
means we are done. Now run the visual studio
project then you will be able to see the service projects inside WCF test
client.
Now hit the browser with the base address mentioned in the
app.config. It will show the service
methods like below -