Small WCF instance

Source: Internet
Author: User

Server:

Namespace wcfservicefist
{
[Servicecontract]
Public interface iservicefirst
{
[Operationcontract]
Double getsum (int x, Double Y );

[Operationcontract]
Int getx ();
}

[Datacontract]
Public class compositetype
{
[Datamember]
Public int X {Get; set ;}
[Datamember]
Public Double Y {Get; set ;}

Public double sum ()
{
Return X + Y;
}
}
}

 

 

Namespace wcfservicefist
{
Public class servicefirst: iservicefirst
{
Compositetype COM = new compositetype ();

Public double getsum (int x, Double Y)
{
Com. x = X;
Com. Y = y;
Return com. sum ();
}
Public int getx ()
{
Return com. X;
}
}
}
Create a hostProgramFor example, winform

Create a config

 

<Configuration>
<System. servicemodel>
<Services>
<Service name = "wcfservicefist. servicefirst" behaviorconfiguration = "wcfservicefist. servicefirstbehavior">
<Host>
<Baseaddresses>
<! -- Add a service call address -->
<Add baseaddress = "http: // 192.168.0.0.10: 8200/">
</Baseaddresses>
</Host>

<! -- Service endpoints -->
<Endpoint address = "" binding = "wshttpbinding" Contract = "wcfservicefist. iservicefirst">
<! --
During deployment, the following identification elements should be deleted or replaced to reflect
The identifier used to run the deployed service. After the deletion
Automatically deduce the corresponding ID.
-->
<Identity>
<DNS value = "192.168.0.10"/>
</Identity>
</Endpoint>
<Endpoint address = "mex" binding = "mexhttpbinding" Contract = "imetadataexchange"/>
</Service>
</Services>
<Behaviors>
<Servicebehaviors>
<Behavior name = "wcfservicefist. servicefirstbehavior">
<! -- To avoid metadata leakage, set the following value to false before deployment and delete the above metadata endpoint -->
<Servicemetadata httpgetenabled = "true"/>
<! -- To receive fault exception details for debugging, set the following value to true. Set it to false before deployment to avoid leakage of exception information -->
<Servicedebug includeexceptiondetailinfaults = "true"/>
</Behavior>
</Servicebehaviors>
</Behaviors>
</System. servicemodel>

</Configuration>

 

Configuration file:

<Service name = "wcfservicefist. servicefirst" behaviorconfiguration = "wcfservicefist. servicefirstbehavior">

<Endpoint address = "" binding = "wshttpbinding" Contract = "wcfservicefist. iservicefirst">

<Behavior name = "wcfservicefist. servicefirstbehavior">

 

Add the function of enabling or disabling a service in the Host Program.

 

Private servicehost _ servicehost = new servicehost (typeof (wcfservicefist. servicefirst ));
Private void btnopen_click (Object sender, routedeventargs E)
{
If (_ servicehost. State! = Communicationstate. Opened)
{
Try
{
_ Servicehost. open ();
MessageBox. Show ("opened successfully ");
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
}
}

Private void btnclose_click (Object sender, routedeventargs E)
{
If (_ servicehost. State! = Communicationstate. Closed)
{
Try
{
_ Servicehost. Close ();
MessageBox. Show ("disabled successfully ");
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
}
}

After opening the service, create a client and

Http: // 192.168.0.10: 8200/as the service reference address (the server must be enabled and enabled) to add a service reference.

In this way, it will be consistent with the WebService usage method.

 

private void button#click (Object sender, eventargs e)
{< br> servicefirstclient SFC = new servicefirstclient ();
MessageBox. show (SFC. getsum (10, 10.9 ). tostring (); // result 20.9
MessageBox. show (SFC. getx (). tostring (); // result 10
}

Related Article

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.