Write a simple console-initiated MCV service

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.