WCF distributed development step for Win (1): WCF Distributed Framework Foundation concept

Source: Internet
Author: User
Tags msmq sap netweaver

As we all know, the low coupling between systems has always been the goal of the enterprise Application system integration, the emergence of SOA service-oriented architecture provides us with important reference principles for how to use the existing enterprise system resources to design and implement ERP system. SOA is so hot that manufacturers have launched their own middleware products, such as Oracle Fusion and SAP NETWEAVER,IBM, BEA and other enterprises have launched their own SOA-based solutions. An important concept in SOA architecture design based on the Java EE platform is the EJB Enterprise service Bus, which realizes the data interaction of each system. On the. NET platform, WCF is Microsoft's communication infrastructure for the data interactions of each system. As a result of the thesis project, I have contacted the SOA (service-oriented architecture, service Oriented architecture) and generated a strong interest. Therefore, yes. NET platform based on SOA enterprise Application system integration and implementation of the technology to learn, summarize experience and experience, finishing out a series of cloth-type development of articles, share with you.    

Here's a look at the structure of the "WCF distributed development step to win" article series. The first is the introduction of the basic concept of WCF distributed development, followed by the design of the detailed introduction and application of each knowledge point, the middle will be interspersed with the previous techniques of comparative analysis, involving the service contract, data contract, Operation contract, things, security, message queue and other important concepts. same as Lao Xu consistent style, I will try to do the article's conditioning clear, detailed system, easy to understand! will also release the detailed code of the label for everyone to refer to the study. The structure of this section of the article is: 1. WCF Basic Concepts Introduction 2.WCF new features 3.WCF framework Model 4. Implementing Code Analysis 5. Summary.

Introduction to "1" WCF Basic concepts

To learn WCF, first of all we need to understand the basic concept of WCF, then what is WCF?

The WCF full name Windows communication Foundation, the distributed Communication programming framework that Microsoft provides for building service-oriented applications, is an important part of the. NET Framework 3.5. Using this framework, developers can build enterprise-class, connected application solutions that are cross-platform, secure, reliable, and support transactional processing.
According to Microsoft's official explanation, WCF (previously known as "Indigo") is a unified framework for building and running service-oriented services (oriented) applications using managed code. It enables developers to build a secure, trustworthy, transactional solution across platforms that can be compatible with existing systems. WCF is the integrator of Microsoft's distributed application development, which integrates. NET platform, all technologies related to distributed systems, such as Enterprise Sevices (COM +). NET Remoting, Web Service (ASMX), WSE3.0, and MSMQ Message Queuing. In the context of communication (communiation), it can span processes, across machines, across subnets, corporate networks and the Internet, and in the host program, you can host Asp.net,exe,wpf,windows forms,nt service,com+ (Host). WCF can support protocols including tcp,http, cross-process and custom, and security mode includes SAML, kerberos,x509, user/password, custom and many other standards and patterns. That is, under the WCF framework, it is easy to develop a distributed system based on SOA, and Microsoft has included all the technical elements associated with it, mastering WCF as the key to opening the door to SOA. (Refer to MSDN)

The SOA concept here is to make it clear that SOA (service-oriented architecture, Service Oriented architecture) is an architectural pattern and design principle. interpreted as an SOA (service-oriented applicatoin) service-oriented application in <programming WCF services >by Juval Lowy, the two should not conflict, The latter should be an application designed and implemented using SOA service-oriented architecture (service-oriented applicatoin).

The power of WCF is due to the integration of the Enterprise Sevices (COM +). NET Remoting, Web Service (ASMX), which is related to distributed systems under the. NET Platform. WSE3.0 and MSMQ Message Queuing technology, I have in the previous WCF Distributed development Prerequisite knowledge (4) and WSE3.0 Build Web Services Security (4) series have given the detailed introduction, here is not detailed, interested friends can check their own. I'll explain what I need to know about the WCF-related knowledge points later on.

New features of "2" WCF

Indigo has three outstanding features: Consistency with multiple existing Microsoft technologies, support for cross-vendor interoperability, and explicit service-oriented features.

"2.1" consistency with Microsoft distributed computing Technology

Microsoft's previous distributed computing technology features and application areas are different. The best choice for building a basic interoperable Web service is the ASP. NET Web Service (ASMX). To connect two. NET Framework-based applications, select. NET Remoting. If your application requires distributed transactions and other higher-level services, its creators typically use Enterprise Services Sevices, the successor of COM +. To build Web services security, using the ws-addressing and ws-security specifications, developers can build applications that employ WSE3.0. To create a message-based, queued application, Windows-based developers should use Microsoft Message Queuing (MSMQ). The specific characteristics of each technology and WCF pairs such as the following table:

ASMX

. NET Remoting

Corporate Services

WSE

Msmq

Indigo

WEB Services

X

X

. NET-. NET Communication

X

X

Distributed transactions, and so on.

X

X

WS-* Specification support

X

X

Queue Message Transmission

X

X

WCF supports all the features of previous distributed technologies, making it more flexible and comprehensive for building scores.

"2.2" interoperability with non- Microsoft Applications

Because Indigo's basic communication mechanism is SOAP, indigo applications can communicate with a large number of other software running in a variety of contexts. Applications built on Indigo can interact with all of the following programs:

1) Indigo applications running in different processes on the same Windows computer.

2) An Indigo application that runs on another Windows computer.

3) applications built on other technologies, such as application servers that support standard WEB services, built on the Java 2 Enterprise Edition (EE).

4) Indigo applications can also interoperate with applications built based on Indigo's previous. NET Web Services (ASMX).

This allows WCF to interact with other applications through standard SOAP messages, well supporting cross-platform cross-operating system objectives. Of course WCF supports security, things, Mtom features, which are the main concepts in the previous WSE, and we found that WCF also extends and supports it. facilitates the forward compatibility of technology.

"2.3" explicit support for service-oriented development

Service orientation is a broad area that encompasses service-oriented applications and a broader concept of service-oriented architecture (SOA). Always adhere to four cardinal principles during the design of this technology:

1. Shared schemas, not classes: Unlike legacy distributed object technologies, services interact with their clients only through well-defined XML interfaces. Passing complete classes, methods, and all of the same behavior across service boundaries is forbidden.

2. The service is autonomous: the service and its clients agree to the interface between them, but are independent of each other. They can be written in different languages, can be used in different runtime environments (such as the CLR and Java virtual machines), can run on different operating systems, and can be different in other ways.

3. The boundary is explicit: one of the goals of distributed object technology [such as distributed COM (DCOM)] is to make the remote object look as if it were a local object as much as possible. While this approach simplifies development in some ways by providing a common programming model, it also hides the unavoidable distinction between local objects and remote objects. The service avoids this problem by making the interaction between the service and its clients more apparent. Hiding distributed features is not an end.

4. Adopt policy-based compatibility: deciding which options to use between systems should depend on the ws-policy mechanism.

Ws-policy in WSE3.0 we should be more familiar with policy configuration to support and implement Web services security. There are many technical concepts that we can find in the early days of distributed technology.

"3" WCF Service Framework Model

Let's take a look at the basic structure of WCF services. The basic structure of the WCF service is as follows:

Each Indigo service is comprised of three parts:

1. Service class: Written in C # or vb.net or other CLR-based languages, implementing one or more methods. Typically includes service contracts, operational contracts, and data contracts.
2. Host: An application domain and process in which the service will run. Asp.net,exe,wpf,windows forms,nt service,com+ as host.
3. Endpoint: The address exposed by the service that is used by the client to access the service. The commonly mentioned concept is ABC, the abbreviation for the first letter of address, binding, and contract.

An important concept in an endpoint is binding, which is a key part of implementing communication. The protocols that define message traffic include HTTP, TCP, UDP, MSMQ, and so on, which users can define according to their own needs.

"4" Implementation Code analysis

Some of the basic concepts, features, and framework models related to WCF are described above, and we will now implement an application instance of WCF specifically. Procedures for client and WCF service communication

The client invokes the WCF service through the proxy, and the proxy locates the specific service through the service address, calling to execute a specific service method.

The demo part of our code is also divided into 3 parts, the first is the service class (contract part), followed by the host (self-defined a console program), the client (console).

"4.1" Contract (contract)

The code that defines the implementation includes the service contract Iwcfservice, the operation contract OperationContract, and the data contract DataContract. The specific code is implemented as follows:


The ServiceContract property and all other properties used by Indigo are defined in the System.ServiceModel namespace.
So this example begins by using a using statement to reference the namespace.
Namespace Wcfservice
{
1. Service Contract
[ServiceContract (Namespace = "http://www.cnblogs.com/frank_xl/")]
public interface Iwcfservice
{
Operation contract
[OperationContract]
String SayHello (string name);
Operation contract
[OperationContract]
String sayhellotouser (user user);

}
2. Service class, integrated interface. Implementing a contract
public class Wcfservice:iwcfservice
{
Ways to implement an interface definition
public string SayHello (string name)
{
Console.WriteLine ("Hello! {0},using string ", name);
Return "hello!" + name;
}
Ways to implement an interface definition
public string sayhellotouser (user user)
{
Console.WriteLine ("Hello! {0} {1},using datacontract ", user. FirstName, user. LastName);
Return "hello!" + user. FirstName + "" + user. LastName;
}
}
3. Data contract//serialized into XML as metadata encapsulated in the service
[DataContract]
public struct User
{
[DataMember]
public string FirstName;
[DataMember]
public string LastName;
}

}

"4.2" host (Managed process)

The Wcfhost host process hosts the service Wcfservice, and the project type is also a console application (you can also create additional project types). Here you add a reference to the Wcfservice class library. Managed dynamically creates an instance of the service in a reflective fashion. The specific code is as follows:


Self-hosted or user-defined program hosting services such as IIS, Was,windows services, etc.
public class Wcfhost
{
static void Main (string[] args)
{

Reflection mode to create a service instance,
Using-mode life instances, which release unmanaged resources at the end of the object life cycle
using (ServiceHost host = new ServiceHost (typeof (Wcfservice.wcfservice)))
{
Registering multiple base sites with the same service
Add service and URI, user resource Identifier
Uri tcpaddress = new Uri ("Http://localhost:8001/WCFService");
Uri httpaddress = new Uri ("Http://localhost:8002/WCFService");
Uri httpaddresslisening = new Uri ("Http://localhost:8002/WCFService");
Host. AddServiceEndpoint (typeof (Wcfservice.iwcfservice), New Wshttpbinding (), tcpaddress);
Host. AddServiceEndpoint (typeof (Wcfservice.iwcfservice), New Wshttpbinding (), httpaddress);
Determine if and open the connection, open the listening port if it is not already open
if (host. State!=communicationstate.opening)
Host. Open ();
Show running status
Console.WriteLine ("Host is runing! and state is {0} ", host. State);
Wait for input to stop service
Console.read ();
}
}
}

It is important to note that the binding protocol is the same as the settings in the configuration file, otherwise there will be compile errors or run-time exceptions. The end of compilation will generate an EXE for executable files.

"4.3" Client

Client to add a reference to the above services, if you first set up a service, improper configuration is prone to the problem of reference service failure, configuration service-related errors and solutions I also organized into WCF distributed development Common error resolution, we need to refer to, is in the face of suffering can message me to communicate.

The specific implementation code for the client is as follows:


Class WCFClient
{
static void Main (string[] args)
{

Iwcfservice proxy = Channelfactory<iwcfservice>. CreateChannel (New Wshttpbinding (),
New EndpointAddress ("Net.tcp://localhost:8001/wcfservice"));
New EndpointAddress ("Http://localhost:8002/WCFService"));

Wcfserviceclient wcfserviceproxy = new Wcfserviceclient ("Wshttpbinding_iwcfservice1");


Calling the SayHello service via proxy
Console.WriteLine (Wcfserviceproxy.sayhello ("Frank Xu Lei");
Invoking Sayhellotouser through proxy calls, passing objects
User user = new user ();
User. FirstName = "Lei";
User. LastName = "Frank";
Console.WriteLine (Wcfserviceproxy.sayhellotouser (user));
Console.read ();
}
}

There are two ways to invoke a WCF service, one to implement the client service proxy itself, and to add the address of the service correctly. The second is that when the client adds the service successfully, the VS2008 integrated development tool will automatically generate the server-side operation contract, service contract and so on, including the client service proxy class, which is more critical. The specific method is to add the service app to the client project right-click. Enter the address that the service listens on. When you find a service, adding a reference generates a list of the files that the service references.

"4.4" Run Result:

First, you start the service host process Wcfhost.

Next run the client, the result:

"5" Summary

The result shows that the call succeeds, including calling the simple SayHello method directly and calling the Sayhellotouser method through the data contract.

This series of articles is named "WCF distributed development step to win". prior to this, I had compiled the WCF Distributed Development Prerequisites (4) and WSE3.0 Building Web Services Security (4) series, which introduced the technologies related to Microsoft Distributed development: COM + (Enterprise Services),. Net Remoting, MSMQ Message Queuing, Web services, WSE3.0, and other related knowledge. The goal is to lay a solid foundation for learning about WCF. Take steps to Win!    This is also the reason why this series of articles named the true purpose! If you are. Net developer, Lao Xu's advice is to have time to look at Microsoft's early distributed development: COM + (Enterprise services),. Net Remoting, MSMQ Message Queuing, Web services, WSE3.0 and other related technologies. These technologies look old, and they are still widely used in practice at present. And you want to truly be an expert in the field of enterprise application integration, which is essential.

Finally, the sample code for this section is given for your reference. Download connection:/files/frank_xl/wcfservicefrankxulei.rar. Hope to be helpful to everyone, also welcome message exchange ~ If you feel good, I hope you support and continue to pay attention to "WCF distributed development step-by-step to win" the follow-up article, thank you ~

WCF distributed development step for Win (1): WCF Distributed Framework Foundation concept

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.