Link/files/callwangxiang/simplewcf.rar
To simplify the development of WCF, we can generate a proxy class by adding service reference in the project (or svcutil.
Later, P & P provided the Service Factory, which can help us design the WCF and flexibly choose WS-* or WCF implementation as needed,CodeAutomatic Generation and configuration.
The client proxies generated in both methods include a duplicate service contract and data contract (defined by messagecontract and faultcontact ),
However, just as we will not directly drag the connection \ command space to assist data access in some larger projects, the code and configuration files generated using these tools include a lot of "spam ",
In addition, compiling the same entity twice is not conducive to our deployment.
Return to the original WCF code. We adopt a remoting-like method. The following is an example after slimming:
Advantages of this method:
1. entity consistency: when implementing a global WCF project in an enterprise (or industry), the standard business entity is repeatedly defined and can directly serve the industry xml dm (Data Model) or MDM (Master Data Management), which can distribute standard business entities to the service and client of multiple project groups.
2. Clean, no "dirty" repeated code and Configuration
3. compliant with the Service hierarchy,
1. Common. dll [Datacontract]
Public Class Complex
{
[Datamember]
Public Int X;
[Datamember]
Public Int Y;
Public Complex ( Int X, Int Y)
{
This. X=X;
This. Y=Y;
}
Public Complex ()
{
}
Public Override String Tostring ()
{
Return String. Format ("({0}, {1 })", X, y );
}
}
[Servicecontract]
Public Interface Icalculator
{
[Operationcontract]
IntAdd (IntX,IntY );
[Operationcontract]
IntSubstract (IntX,IntY );
[Operationcontract]
Complex addcomplex (complex A, complex B );
}
22.16host.exe
Public Class Calculatorservice: icalculator
{
Icalculator members # Region Icalculator members
Public Int Add ( Int X, Int Y)
{
Console. writeline ("Add");
ReturnX+Y;
}
Public Int Substract ( Int X, Int Y)
{
Console. writeline ("Substract");
ReturnX-Y;
}
Public Complex addcomplex (complex A, complex B)
{
Console. writeline ( " Addcomplex " );
Complex result = New Complex ();
Result. x = A. x + B. X;
Result. Y = A. Y + B. Y;
Return Result;
}
# Endregion
}
<? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
< System. servicemodel >
< Services >
< Service Name = "Host. calculatorservice" >
< Endpoint
Address = "Http: // localhost: 8000/derivatives/calculator"
Binding = "Wshttpbinding"
Contract = "Common. icalculator" />
</ Service >
</ Services >
</ System. servicemodel >
</ Configuration >
Static Void Main ( String [] ARGs)
{
Using (Servicehost host = New Servicehost ( Typeof (Calculatorservice )))
{
Host. open ();
Console. writeline ("Service is available.");
Console. readkey ();
}
}
32.16client.exe <? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
< System. servicemodel >
< Client >
< Endpoint Name = "Calculatorservice"
Address = "Http: // localhost: 8000/derivatives/calculator"
Binding = "Wshttpbinding"
Contract = "Common. icalculator" />
</ Client >
</ System. servicemodel >
</ Configuration >
Static Void Main ( String [] ARGs)
{
Console. writeline ( " Press any key when the service is available. " );
Icalculator proxy = New Channelfactory < Icalculator > ( " Calculatorservice " ). Createchannel ();
Console. writeline (proxy. Add ( 1 , 2 ));
Complex = New Complex ();
A. x = 3 ;
A. Y = 4 ;
Complex B = New Complex ();
B. x = 3 ;
B. Y = 1 ;
Complex result = Proxy. addcomplex (A, B );
Console. writeline (result );
Console. readkey ();
}