1. Add a WCF service to project engineering, two files IService.cs and Service.cs will appear in the project.
IService.cs defines the interface in the service, the interface must add an attribute [OperationContract] in order to discover the function in the instantiated object after the client adds the service.
Namespace ConsoleApplication1
{
[ServiceContract]
public interface Imyservice
{
[OperationContract]
void DoWork ();
[OperationContract]
void MyFunction ();
}
}
implementing Interfaces in Service.cs
2. After adding a service , the App. Config Configuration service information.
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<startup>
<supportedruntime version= "v4.0" sku= ". netframework,version=v4.5 "/>
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name= "" >
<!--set the following values to false before deployment to avoid leaking metadata information-
<!---This information is used when the user adds the service, or the client cannot find the service by adding a service reference-
<servicemetadata httpgetenabled= "true" httpsgetenabled= "true" />
<!--to receive the fault exception details for debugging, set the following value to True. Set to false before deployment to avoid leaking exception information--
<servicedebug includeexceptiondetailinfaults= "false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name= "Consoleapplication1.myservice" >
<endpoint address= "" binding= "BasicHttpBinding" contract= "Consoleapplication1.imyservice" >
<identity>
<dns value= "localhost"/>
</identity>
</endpoint>
<endpoint address= "Mex" binding= "mexHttpBinding" contract= "IMetadataExchange"/>
<baseAddresses>
<!- User Client adds the address of the service reference ---
<add baseaddress= "http://192.168.1.109:8733//MyService/"/>
</baseAddresses>
</service>
</services>
</system.serviceModel>
</configuration>
3. Start the service.
Add the following code in main
ServiceHost host = new ServiceHost (typeof (MyService)); Put the WCF service host here
Host. Open ();
Console.WriteLine ("Service has been started");
Console.read ();
So the service has been started.
4. Reference Services
Add a service reference to any project, add http://192.168.1.109:8733//MyService/in the Address bar, and you can find the service to add.
5. Use of the service
Add the following statement to the project code that references the service.
myservicereference.myserviceclient mc = new Myservicereference.myserviceclient ();
The functions in the service can be invoked using the MC.
Write a simple console-initiated MCV service