First I will start off by creating class library project and I will name it "WCFClassLibrary". In that project, I will create a class named "MyWcfService" and I will also create an Interface which is "IMyWcfService". Now in my "WCFClassLibrary", I will add a reference to the "System.ServiceModel.dll" and I am now going to modify my "IMyWcfService" interface.
I will be using "System.ServiceModel.dll" in my interface and since interfaces that represents WCF contracts needs to have [ServiceContract] attribute, I am modifying the interface as follows.
using System.ServiceModel; namespace WCFClassLibrary { [ServiceContract] interface IMyWcfService { [OperationContract] string MyTestMethod(string sHelloWorld); } }Then in my "MyWcfService" class, I am going to implement the IMyWcfService interface.
namespace WCFClassLibrary { public class MyWcfService : IMyWcfService { public string MyTestMethod(string sHelloWorld) { return sHelloWorld; } } }Now I am done with implementing the service. Next what I have to do is implement the host application to host my service. For that I will create a console application. For that I will add a new Console Application project and I will name it "MyWcfHostConsoleApplication". I will add a reference to the "System.ServiceModel.dll" and "WCFClassLibrary.dll" project. Then I will add a Application configuration file and it's name is "App.config". So it's time modify the "App.config" file. Within,
<configuration></configuration>I will start writing following code.
<system.serviceModel> <services> <service name="WCFClassLibrary.MyWcfService"> <endpoint address="" binding="basicHttpBinding" contract="WCFClassLibrary.IMyWcfService"> </endpoint> <host> <baseAddresses> <add baseAddress="http://localhost:8080/MyWcfService"/> </baseAddresses> </host> </service> </services> </system.serviceModel>Now, I have to write code for hosting my service from inside my Main method. For that I will modify my Main method as follows.
using System; using System.ServiceModel; using WCFClassLibrary; namespace MyWcfHostConsoleApplication { class Program { static void Main(string[] args) { using (ServiceHost serviceHost = new ServiceHost(typeof(MyWcfService))) { serviceHost.Open(); Console.ReadLine(); } } } }Now when I type "http://localhost:8080/MyWcfService" and hit enter in by browser, what I will see is this,
Metadata publising for this service is currently disabled. |
You will see that "Metadata publishing for this service us currently disabled.". To enable Metadata publishing what you will have to do is again modify the "App.config " as follows.
<system.serviceModel> <services> <service name="WCFClassLibrary.MyWcfService" behaviorConfiguration="MyWcfServiceMEXBehavior"> <endpoint address="" binding="basicHttpBinding" contract="WCFClassLibrary.IMyWcfService"> </endpoint> <!--MEX endpoint--> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"> </endpoint> <host> <baseAddresses> <add baseAddress="http://localhost:8080/MyWcfService"/> </baseAddresses> </host> </service> </services> <!--A behavior definition for MEX--> <behaviors> <serviceBehaviors> <behavior name="MyWcfServiceMEXBehavior"> <serviceMetadata httpGetEnabled="True"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>Now again when I host and I type "http://localhost:8080/MyWcfService" and hit enter in by browser, I can see this.