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
}