Programming WCF services translation notes (4)

Source: Internet
Author: User

Chapter 1 of this book mainly describes the service contract. Content: "This chapter first discusses how to establish associations for two very different programming models through Operation overloading and contract levels. Next, this chapter will introduce some simple and powerful technical and guiding principles for designing and separating service contracts. At the end of this chapter, we also demonstrate how to interact with the contract metadata at runtime through programming ."

Operation overload

Both C ++ and C # support operation overloading, but they are not supported in the WCF programming model. Frankly speaking, in the WCF programming model, object-oriented support is relatively weak, including the inheritance system and Polymorphism to be introduced later, there are many problems. Therefore, we cannot define such a service contract on the server:
[Servicecontract]
Interface icalculator
{
[Operationcontract]
Int add (INT arg1, int arg2 );

[Operationcontract]
Double add (double arg1, double arg2 );
}

Although it can be passed during compilation, once the host is loaded, an invalidoperationexception will be thrown. Taking the icalculator contract as an example, WCF considers it as zero operation.

The solution is to use the name attribute of the operationcontract feature, for example:
[Servicecontract]
Interface icalculator
{
[Operationcontract (name = "addint")]
Int add (INT arg1, int arg2 );

[Operationcontract (name = "adddouble")]
Double add (double arg1, double arg2 );
}

However, if this method is used, the generated proxy uses the name specified by the name attribute as the method name for the proxy operation. This is not a good method for programmers. Fortunately, we can manually modify the generated proxy to the same operation name as the service contract. Because the operation alias is specified through name, the exception thrown by loading the host is avoided.

Contract inheritance

Even if the parent interface is marked with [servicecontract], the subinterface still needs to mark [servicecontract] Because servicecontractattribute cannot be inherited. The implementation of service contracts by services is no different from the traditional C # programming. For example:
[Servicecontract]
Interface isimplecalculator
{
[Operationcontract]
Int add (INT arg1, int arg2 );
}
[Servicecontract]
Interface iscientificcalculator: isimplecalculator
{
[Operationcontract]
Int multiply (INT arg1, int arg2 );
}
Class mycalculator: iscientificcalculator
{
Public int add (INT arg1, int arg2)
{
Return arg1 + arg2;
}
Public int multiply (INT arg1, int arg2)
{
Return arg1 * arg2;
}
}

when you publish an endpoint, you can publish a separate endpoint to the underlying contract interface:






when the client imports the preceding service contract, it will cancel the inheritance level of the service contract and use the action and replyaction attributes in the operationcontract feature to retain the contract name originally defined for each operation. However, to make client programming consistent with Service programming, it is best to restore the client's contract level. There is nothing too mysterious about the method. It is simply to manually modify the client contract according to the service contract level. The modified client contract and its proxy are defined as follows:
[servicecontract]
Public interface isimplecalculator
{
[operationcontract]
int add (INT arg1, int arg2 );
}< br> Public partial class simplecalculatorclient: clientbase ,
isimplecalculator
{< br> Public int add (INT arg1, int arg2)
{< br> return channel. add (arg1, arg2);
}< br> // rest of the proxy
}

[servicecontract]
Public interface iscientificcalculator: isimplecalculator
{< br> [operationcontract]
int multiply (INT arg1, int arg2 );
}< br> Public partial class scientificcalculatorclient:
clientbase , iscientificcalculator
{< br> Public int add (INT arg1, int arg2)
{< br> return channel. add (arg1, arg2);
}< br> Public int multiply (INT arg1, int arg2)
{< br> return channel. multiply (arg1, arg2);
}< br> // rest of the proxy
}

in the book, the author also proposed the so-called proxy chaining technology. In essence, it is to make the proxy classes that implement different levels of interfaces to form an inheritance relationship of IS-A. In the preceding definition, scientificcalculatorclient can be inherited from simplecalculatorclient, instead of clientbase :< br> Public partial class simplecalculatorclient: clientbase ,
isimplecalculator
{< br> Public int add (INT arg1, int arg2)
{< br> return channel. add (arg1, arg2);
}< br> // rest of the proxy
}

Public partial class scientificcalculatorclient: simplecalculatorclient,
Iscientificcalculator
{
Public int multiply (INT arg1, int arg2)
{
Return channel. Multiply (arg1, arg2 );
}
// Rest of the proxy
}

Only in this way, as shown belowCodeIs correct:
Simplecalculatorclient proxy1 = new simplecalculatorclient ();
Simplecalculatorclient proxy2 = new scientificcalculatorclient ();
Scientificcalculatorclient proxy3 = new scientificcalculatorclient ();

Decomposition and Design of service contracts

The principle of contract separation and interface isolation is the same as that of interface segregation principle. In the ISP principle, we recommend that you use multiple special interfaces instead of a single interface. This prevents interface contamination and facilitates interface reuse. The same is true for contract decomposition, but it is subject to the cost of implementing the contract.

The book provides the criteria for breaking down service contracts. "Reasonable contract decomposition can achieve in-depth specialization, loose coupling, fine adjustment, and contract reuse. These advantages help improve the entire system. In general, the purpose of contract decomposition is to minimize the number of operations contained in the contract ."

When designing a service-oriented system, you need to balance the two factors that affect the system (see Figure 2-1 ). One is the cost of implementing the service contract, and the other is to merge or integrate the service contract into a highly cohesive application.Program.
 
Figure 2-1 number and scale of balancing services

When defining a service contract, you also need to pay attention to the use of the so-called property-like operation in the book. To put it simply, if the object state management (which is generally embodied in C #) is involved, such operations should not be published as service operations. The reason is: "The client is only responsible for calling operations, and the Service manages the status of service objects ."

Contract Query

To query a contract, you must first understand the metadata. WCF provides the following helper classes in the system. servicemodel. Description namespace:
Public Enum metadataexchangeclientmode
{
Metadataexchange,
Httpget
}
Class metadataset :...
{...}
Public class serviceendpointcollection: collection <serviceendpoint>
{...}

Public class metadataexchangeclient
{
Public metadataexchangeclient ();
Public metadataexchangeclient (binding mexbinding );
Public metadataexchangeclient (URI address, metadataexchangeclientmode mode );

Public metadataset getmetadata ();
Public metadataset getmetadata (endpointaddress address );
Public metadataset getmetadata (URI address, metadataexchangeclientmode mode );
// More Members
}

public abstract class metadataimporter
{< br> public abstract serviceendpointcollection importallendpoints ();
// more Members
}< br> public class wsdlimporter: metadataimporter
{< br> Public wsdlimporter (metadataset metadata );
// more Members
}< br> public class serviceendpoint
{< br> Public endpointaddress
{Get; set ;}
Public binding
{Get; Set ;}< br> Public contractdescription contract
{Get ;}
// more Members
}< br> public class contractdescription
{< br> Public string name
{ get; set ;}
Public String namespace
{Get; Set ;}< br> // more Members
}

The book provides Metadata Query methods, and also implements a metadatahelper class specifically used to operate metadata.
Public static class metadatahelper
{
Public static bool querycontract (string mexaddress, type contracttype );
Public static bool querycontract (string mexaddress, string contractnamespace, string contractname );
// More Members
}

You can provide the contract type we want to query for the metadatahelper class, or provide the contract name and namespace:

String address = "...";
Bool contractsupported = metadatahelper. querycontract (address, typeof (imycontract ));

The specific implementation can see the description in the book, the complete implementation code can be downloaded to the author's website (http://www.idesign.net.

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.