Deploy WCF in IIS in NLB Environment

Source: Internet
Author: User
Tags dedicated ip
  • Environment conditions:

    1. WINXP sp2.cn
    2. Visual Studio team System 2008 beta2.en
    3. VPC 2007.en
    4. Win2k3 Server SP2
  • Preparations:
    1. Install vs2008 on a WINXP SP2 machine;
    2. Install VPC 2007 on WINXP SP2;
    3. Prepare two win2k3 Server Virtual Environments in VPC 2007 and install. Net 3.5 framework beta2 runtime for them:

       

      Virtual Machine environment

      Virtual Machine environment B

      Device Name

      Win2K3-I

      Win2K3-II

      Machine name

      Server-01

      Server-02

      Memory Allocation

      256 m

      256 m

      IP Address Allocation

      192.168.4.10

      192.168.4.11

  • The following is an overview of the WCF-based service-oriented architecture Demo:
    Vs2008 provides two types of project templates for WCF: the WCF Service application under the Web category, the WCF Service library under the WCF category, and the WCF Service template related to the workflow.

    WCF Service AppliactionProject template: This template uses IIS as the host of the WCF Service to provide external services. By default, wshttpbinding of HTTP protocol is used, that is, WebServices. However, this project template not only contains *. SVC service declarations,

    WCF Service LibraryProject template: This template creates the contract interface and service implementation class by default. Add a WCF Service type item to this type of project. The iservice interface and service implementation class are added at the same time. The iservice interface file contains two classes: servicecontract contract interface and datacontract data contract class. The service class is the implementation of servicecontract interface in the iservice interface file. According to the concept of the WCF design, the interface file iservice defines the service contract Contract, which is the basis for service callers, that is, service interfaces and data entities, contract contract.
    Designed according to the WCF Service libray template, service interfaces and data entities are defined together, and the specific implementation of service interfaces is also included inProgramCentralized. Obviously, this is not a design that meets low coupling requirements. Therefore, in this example, we will separate these parts to create a project separately.

    In this WCF example, the entire architecture is divided into six projects. It is not explicitly stated that the class librayr project is based on. NET 3.5.

      1. Fetion. Web. Framework. datacontract
        Brief description: The data contract datacontract is defined as a business entity in a traditional project.
        Project Reference: system. runtime. serialization
      2. Fetion. Web. Framework. servicecontract
        Brief description: the service contract servicecontract is an interface used to define the operation interface of the service.
        Project Reference: system. servicemodel
        Fetion. Web. Framework. datacontract
      3. Fetion. Web. Framework. businessService
        Brief description: service implementation refers to the specific implementation of the servicecontract interface of the service contract, including the actual service operations of the Service.
        Project Reference: fetion. Web. Framework. datacontract
        Fetion. Web. Framework. servicecontract
      4. Fetion. Web. Framework. servicecontainer
        Brief description: Service container, that is, the host of the WCF. Create a project using the WCF Service Appliaction Project template, and modify the project to meet the requirements.
        Project type: WCF Service Appliaction under the Web category
        Project Reference: fetion. Web. Framework. businessService
      5. Fetion. Web. Framework. servicewrapper
        Brief description: Client Service proxy, which encapsulates server services, including permission security, data encryption and compression, and cache.
        Project Reference: system. servicemodel
        Fetion. Web. Framework. datacontract
        Fetion. Web. Framework. servicecontract
      6. Fetion. Web. Framework. uiconsole
        Brief description: The UI console program is used to test the WCF Service in place of the final UI Layer.
        Project type: Console Appliaction under Windows
        Project Reference: system. servicemodel
        Fetion. Web. Framework. datacontract
        Fetion. Web. Framework. servicecontract
        Fetion. Web. Framework. servicewrapper

        Shows the entire project structure:

    • Fetion. Web. Framework. datacontract
      Fetion. web. framework. datacontract contains the data entity objects to be passed to the front-end. In this example, a class named hostinfo is used as the data carrier. hostinfo has only two attributes, which are used to record the Host Name and Host IP address, it is easy to determine the server from which the server is executed during subsequent load balancing tests. [Datacontract] marks the hostinfo class as a data contract class of WCF, which is very similar to the commonly used serializable attribute and can mark the class as serializable; [datamember] specifies the attribute of the data field to be exposed as a data contract.

    • Fetion. Web. Framework. servicecontract
      Fetion. web. framework. servicecontract contains all interface interfaces, and uses [servicecontract] to mark the interface as a service contract of WCF, and uses [operationcontract] to mark the methods in it as the specific operation contract. Servicecontract defines the service structure framework. On the one hand, it restricts the specific implementation of the service, and on the other hand, it can be used as the call credential of the Service caller. (Svcutil.exe is recommended to generate service call creden.exe in msdn's and wcfs' data. I do not agree with this, and I will elaborate on it later when calling the client)

    • Fetion. Web. Framework. businessService
      Fetion. Web. Framework. businessService is the specific implementation of fetion. Web. Framework. servicecontract. In this example, a simple business logic is implemented: Get the machine name and IP address of the current server, fill in the data entity defined in datacontract, and provide it to the service caller.
      Note: In this example, the business fields and data access that a system should have are shielded, and the final encapsulation of the business should be completed at this layer and released to the caller for use.

    • Fetion. Web. Framework. servicecontainer
      Fetion. Web. Framework. servicecontainer is the host container of WCF, which relies on IIS. WCF and. net remoting is the same. It can use any application as the host: Console Appliaction, Windows Service, winform, and Web Service applicaton. However, to use the security and load balancing features provided by IIS, in this example, IIS is used as the host of the WCF Service.
      First, add a WCF Service Appliaction project. After adding a project reference, delete the iservice1.cs and service1.svc that are added by default. CS file, and then modify service1.svc file name to serverenvironmentservice. SVC, and open the file (if service1.svc has not been deleted. CS, you cannot open the SVC file). Modify the service attribute to the referenced service implementation class fetion. web. framework. businessService. serverenvironment and delete the codebehind attribute.
      The default template uses wshttpbinding, while the default security mode of wshttpbinding is message. In message mode, Windows integrated verification is the default credentials, so the Web is not modified. config, which uses the Default Security Authentication mode. (See the appendix)
      Set this project as a startup project. In IE, enter http: // localhost: 3060/serverenvironmentservice. SVC to verify that the service is provided normally.


    • Fetion. Web. Framework. servicewrapper
      Fetion. Web. Framework. servicewrapper is the call end of the WCF Service. Vs2008 provides many convenient methods for calling WCF: 1. Directly select add service reference from the project, and generate a proxy class and app for calling the service similar to add web references. config to add the file to the project, you can call the WCF services through the generated proxy class.
      No matter which method provided above, a proxy class will be regenerated on the WCF services caller, including the service interface and data entity object class, although easy to use, but it changes the integrity and consistency of the entire framework. In fact, the re-generated proxy class and entity class are repeated implementations of fetion. Web. Framework. servicecontract and fetion. Web. Framework. datacontract. If the object in the front and back ends is not the same object, it may cause some trouble during development, resulting in mutual assignment between two originally consistent object objects.
      In addition, the automatically generated proxy class completely encapsulates the call. Operations on data compression, cache, encryption, and permissions cannot be performed if you want to add more. Therefore, we recommend that you encapsulate the operation call class on the servicewrapper layer.
      In the demo, use the extension method for all system. servicemodel. clientbase Added a method: credential (), which encapsulates Code. The Wrapper class of the corresponding service inherits and implements system. servicemodel. clientbase. And servicecontract iserverenvironment interfaces.

      Wrapperrightextension. CS

      Serverenvironmentservicewrapper. CS


    • Fetion. Web. Framework. uiconsole
      Fetion. Web. Framework. uiconsole, as the final UI display, calls the servicewrapper encapsulated method to remotely call the WCF Service. In its app. config, write configuration information related to the call to the WCF Service, such as binding, service Uri, and service interface class.

      After completing the demo of the WCF Service Project, deploy the project to the win2k3 environment and create a load balancing cluster for the two virtual machine environments, the implementation of the WCF Service is deployed in the NLB environment.

    • Publish a WCF Service to win2k3 Server

      For convenience, we decided to use the publish function provided by vs2008. To use this function, we need to install the FrontPage extension on IIS of win2k3 server.
      In fetion. web. framework. select publish in the servicecontainer project. In the details of the target location item, select remote site and enter: http: // 192.168.4.10/fetionframework. If this is the first release, you also need to click New web site to confirm. After you click the publish button, you must have deployed the WCF Service on server-01. Enter http: // 192.168.4.10/fetionframework/serverenvironmentservice. SVC in IE to verify whether the deployment is correct. (Note: You must check the. NET Framework version used on the default site and set it to. NET Framework 2.0 ).
      Repeat the above process to complete the deployment of server-02.

    • Use the console to deploy access Verification

      Use fetion. web. framework. for access verification on server-01 and server-02 on the uiconsole, you must create two windows accounts: IISADMIN and password: 8848 on server-01 and server-02 respectively, the default Users Group. Modify the address attribute in APP. config of uiconsole to http: // 192.168.4.10/fetionframework/serverenvironmentservice. SVC to verify that server-01 access is normal.
      Repeat the preceding process to verify server-02.

    • Server-01 and server-02 Server Load balancer NLB Environments

      View the NIC properties of server-01, select: Network Load balancer, and click Properties to configure. In cluster IP settings, enter the IP address 192.168.4.100, subnet mask 255.255.255.0, and multicast in cluster operation mode. Then, set the dedicated IP Address Configuration in host parameters to 192.168.4.10 (that is, the local IP address ); finally, change the port range in the port rule to 80 ~ 8080. After setting the network load balancing attributes, click the Internet Protocol (TCP/IP) attributes to set: in advanced, add the IP address 192.168.4.100, mask 255.255.0. Confirm after completion and wait for the system to be configured.

      After configuring server-01, configure Server-02 again. You only need to set the IP address of server-02 when setting the dedicated IP address in host parameters, 192.168.4.11. In this way, the most simplified NLB Server Load balancer environment is established.

    • Use the uiconsole to verify that the NLB environment is successfully set up.

      Modify the app of the uiconsole program. the address attribute in config is http: // 192.168.4.100/fetionframework/serverenvironmentservice. SVC, run the console program, you should be able to get the machine name and IP address of one of the win2k3 server. At this time, close the server and run the console program again. If the console program can still be accessed normally, if the machine name and IP address of the other server are obtained, the test is successful.

    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.