Following the previous section of WCF distributed development step for win series (4): WCF Service Reliability transport configuration and programming development, this section we continue to learn the WCF distributed development step for Win section (5): Service contract and Operation overloading. Here we first explain OOP object-oriented programming in the method overload, the meaning of overloading, WCF service programming development How to implement the operation overload, followed by the Code analysis part, give the service contract definition and implementation of operational overload of attention to the problem and implementation process, and then detailed the client implementation of the operation of the overloaded mode. The final part is the summary of this article. The structure of this section is: "1" Overload concept "2" Operation Overload "3" Code Implementation Analysis "4" Run Result "5" Summary
"1" Overload concept:
"1.1" What is an overload (overload):
Overloading refers to the realization that the same method name can correspond to multiple methods. The names of these methods are the same, but the methods have different types of parameters. This is the concept of method overloading. The application of function method classes and objects is especially important.
Method overloading requires that the compiler be able to uniquely determine which method code should be executed when invoking a method, that is, which method is implemented. When determining the implementation of a method, it is required to differentiate from the number and type of method parameters. This means that when you do a method overload, the same name method is required to be different in the number of arguments, or different on the parameter type. Otherwise, the overload cannot be implemented.
About overloading It is important to note that the parameter type and number of parameters of the overloaded method must be different (that is, either the type of the parameter is different, or the number of arguments is different, or the type and number of arguments are different), otherwise, the compiler will not know that the method is called.
The benefit of method overloading is the same method, which brings different results and implementations, where we can determine the calling fly method based on the difference in the passing parameters. This is a compile-time polymorphism of a implementation mechanism.
Example of "1.2" C # class method overloading:
Here we give a simple C # language implementation of the method overload of the sample, here for the Sayhellooverloading method, the same class is given the number of parameters of the three methods. Internal implementations are also different. The specific code is as follows:
3. Object-oriented classes, how to implement the Operation overload, and the WCF service class in the operation of the overloaded comparison
public class Classoverloading
{
Public classoverloading ()
{
}
Masking method overloading, respectively implementing three methods, C # and other object-oriented language provides method overloading mechanism support.
public string sayhellooverloading ()
{
Writing code
Return "Hello,this an C # class overloading demo";
}
Method overloads in a class do not require aliases
public string sayhellooverloading (string name)
{
Writing code
Return "Hello:" + name + "This a C # class overloading demo";
}
public string sayhellooverloading (string firstName, String lastName)
{
Writing code
Return "Hello:" + firstName + lastName + "This a C # class overloading demo";
}
}
"2" Action Overload:
"2.1" Action Overload:
The WCF service supports the core Web service protocol, and also its metadata exchange is based on the XML language description, the client through the WSDL file to understand the service method related information, including the number of parameters, type, return value, call order and other important information. Because WSDL does not support overloading of methods, our WCF service operation overloads are not exposed to the client through WSDL. If we define the overloads of the methods in the service contract, the compilation will pass normally, but starting the service host throws a System.InvalidOperationException exception, such as:
Therefore we cannot define and implement method overloads in the WCF service class, otherwise it cannot be exposed as a service operation.
"2.2" Workaround:
WCF provides us with a workaround that allows us to use the overloads of the service operation in the WCF service class. WCF defines a mechanism operationcontract, using the Name property of the OperationContract attribute, to specify an alias for the operation:
[AttributeUsage (AttributeTargets.Method)]
public sealed class Operationcontractattribute:attribute
{
public string Name
{Get;set;}
More members
}
"3" Code Implementation analysis:
Let's give a specific WCF service implementation operation overload, including the complete process of service definition, host configuration, client reference, and testing.
"3.1" Service contract:
The service contract Iwcfoverloadingservice is defined, and the 3 different definitions of sayhellooverloading operation contract and the implementation in Wcfservice service class are given respectively. The specific code is as follows:
1. Service contract, Operation contract overload
[ServiceContract (Namespace = "http://www.cnblogs.com/frank_xl/")]
public interface Iwcfoverloadingservice
{
Operation contract
[OperationContract (Name = "SayHelloOverLoading1")]
String sayhellooverloading ();
Operation contract
[OperationContract (Name = "SayHelloOverLoading2")]
String sayhellooverloading (string name);
Operation contract
[OperationContract (Name = "SayHelloOverLoading3")]
String sayhellooverloading (String firstName, string lastName);
}
2. Service class, integrated interface. Implementing a contract
public class Wcfservice:iwcfoverloadingservice
{
Ways to implement an interface definition
public string sayhellooverloading ()
{
Console.WriteLine ("Hello!, this a overloading demo for WCF Service");
Return "hello! This a overloading demo for WCF Service ";
}
Ways to implement an interface definition
public string sayhellooverloading (string name)
{
Console.WriteLine ("Hello! {0},this an overloading demo WCF Service ", name);
Return "hello!" + name + ", this a overloading demo for WCF Service";
}
Ways to implement an interface definition
public string sayhellooverloading (string firstName, String lastName)
{
Console.WriteLine ("Hello! {0} {1},this an overloading demo WCF Service ", FirstName, LastName);
Return "hello!" + FirstName + "" + LastName + ", this a overloading demo for WCF Service";;
}
}
"3.2" Managed Host:
The custom managed host uses the configuration file to define the service's endpoint and metadata exchange nodes, and other properties such as the exchange behavior of the service are also given in the configuration file, and we have configured three different methods of data service communication, namely HTTP, TCP, IPC. The configuration information is as follows:
<services>
<service behaviorconfiguration= "Wcfservice.wcfservicebehavior" name= "Wcfservice.wcfservice" >
<endpoint
Address= "Http://localhost:9001/WCFService"
Binding= "Wshttpbinding"
contract= "Wcfservice.iwcfoverloadingservice" >
</endpoint>
<endpoint
Address= "Net.tcp://localhost:9002/wcfservice"
Binding= "NetTcpBinding"
contract= "Wcfservice.iwcfoverloadingservice" >
</endpoint>
<endpoint
Address= "Net.pipe://localhost/wcfservice"
Binding= "NetNamedPipeBinding"
contract= "Wcfservice.iwcfoverloadingservice" >
</endpoint>
<endpoint address= "Mex" binding= "mexHttpBinding" contract= "IMetadataExchange"/>
<endpoint address= "Mex" binding= "mextcpbinding" contract= "IMetadataExchange"/>
<endpoint address= "Mex" binding= "mexnamedpipebinding" contract= "IMetadataExchange"/>
<baseAddresses>
<add baseaddress= "http://localhost:9001/"/>
<add baseaddress= "net.tcp://localhost:9002/"/>
<add baseaddress= "net.pipe://localhost/"/>
</baseAddresses>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name= "Wcfservice.wcfservicebehavior" >
<servicemetadata httpgetenabled= "true"/>
<servicedebug includeexceptiondetailinfaults= "false"/>
</behavior>
</serviceBehaviors>
</behaviors>
"3.3" Client Service reference:
Let's add a reference to the server separately and start the managed hosting program first. Then add the service reference directly using the Visual Studio2008 tool, or you can use the SvcUtil.exe tool:
The client enters the base address of the service, finds the service, succeeds enough to see the service contract information, the 3 operation names shown here are different, and actually gives the three different names of the service methods. Enter the namespace and make sure you are done.
"3.4" Proxy code:
The client deserializes the generated service contract information, and we look at the client method name and the change of the operation contract, so that the client does not implement the corresponding method overloads, nor can it use the advantages of overloading, which is the feature of compile-time polymorphism. Manually change the client service proxy class and the service contract code to support the action method overloads, as follows:
Public interface Iwcfoverloadingservice {
[System.ServiceModel.OperationContractAttribute (Name = "SayHelloOverLoading1", action= "http://www.cnblogs.com/ Frank_xl/iwcfoverloadingservice/sayhellooverloading1 ", replyaction=" http://www.cnblogs.com/frank_xl/ Iwcfoverloadingservice/sayhellooverloading1respon "+
"Se")]
String sayhellooverloading ();
[System.ServiceModel.OperationContractAttribute (Name = "SayHelloOverLoading2", Action = "http://www.cnblogs.com/ Frank_xl/iwcfoverloadingservice/sayhellooverloading2 ", replyaction =" http://www.cnblogs.com/frank_xl/ Iwcfoverloadingservice/sayhellooverloading2respon "+
"Se")]
String sayhellooverloading (string name);
[System.ServiceModel.OperationContractAttribute (Name = "SayHelloOverLoading3", Action = "http://www.cnblogs.com/ Frank_xl/iwcfoverloadingservice/sayhellooverloading3 ", replyaction =" http://www.cnblogs.com/frank_xl/ Iwcfoverloadingservice/sayhellooverloading3respon "+
"Se")]
String sayhellooverloading (String firstName, string lastName);
}
This way our client-side approach also supports the overloaded nature of the action method.
"3.5" Client test code:
To test the operation contract, we added some test code to the client application, here in order to test the different operations defined by the service side. We grouped the code that was tested according to the protocol:
Instantiating the Client service proxy TCP
Serviceoverloadingtcp.wcfoverloadingserviceclient wcfserviceproxytcp =
New Serviceoverloadingtcp.wcfoverloadingserviceclient ("Wshttpbinding_iwcfoverloadingservice1");
Console.WriteLine ("Test Call service Using TCP--------------------.");
Call the Sayhellooverloading service by proxy, pass different parameters, and test
Console.WriteLine (Wcfserviceproxytcp.sayhellooverloading ());
Console.WriteLine (wcfserviceproxytcp.sayhellooverloading ("Frank Xu Lei");
Console.WriteLine (wcfserviceproxytcp.sayhellooverloading ("Lei", "Xu"));
Instantiating the Client Service proxy HTTP
Serviceoverloadinghttp.wcfoverloadingserviceclient wcfserviceproxyhttp =
New Serviceoverloadinghttp.wcfoverloadingserviceclient ("Nettcpbinding_iwcfoverloadingservice");
Console.WriteLine ("Test Call service Using Http-------------------");
Call the Sayhellooverloading service by proxy, pass different parameters, and test
Console.WriteLine (Wcfserviceproxyhttp.sayhellooverloading ());
Console.WriteLine (wcfserviceproxyhttp.sayhellooverloading ("Frank Xu Lei");
Console.WriteLine (wcfserviceproxyhttp.sayhellooverloading ("Lei", "Xu"));
Debuging
Console.WriteLine ("Press any key to continue");
Console.read ();
"4" Run Result:
Here, we call three service operations, which are tested separately. Results of the operation:
"5" Summary:
The above is an introduction to the WCF Service operation overloads in this section, including the basic definition of generic overloads and the implementation of simple method overloading in the C # language. Then introduced the implementation mechanism, limitations and solutions of WCF operation Overload, the service contract does not support the operation method overloading by default, we can use the existing mechanism of WCF to give the method alias to solve this problem. Then we give a complete test solution including the client, the client anti-sequence session generation service class does not support the service operation method overloading by default, and the generation is also the alias method of the service operation. We need to re-modify the client service proxy code when the client wants to enable the Service proxy class to support overloading to take advantage of the overloads. The example code for this section is also given for your reference:
/files/frank_xl/wcfserviceoverloadfrankxulei.rar.
Resources:
1. "Function overloading": http://baike.baidu.com/view/534068.htm
WCF distributed development step for Win (5) service contract and Operation overloading