Generating source code

At this point, we have specified WCF as the technology for the service implementation, but we still haven't generated any code. The Solution Explorer only has few nodes, as shown in the following image:

Generating source code

The solution item ProjectMapping.xml is an empty file, with no project included. Next, we will create the framework projects for the service, and generate lots of source code from the models.

Creating the service projects

Before we can generate the source code from the models, we need to create a few projects, and add them to the solution. Follow these steps to create all of the required projects:

  1. From the Solution Explorer, right-click on the top node EasyNorthwind solution item.
  2. Select Add | New Project from the context menu.
  3. On the pop-up Add New Project window, select Guidance Packages | Service Factory: Modeling Edition as the project type, and WCF Implementation Projects as the template.
  4. Enter MyWCF.EasyNorthwind as the Name, and leave the Location as the default directory D:SOAwithWCFandLINQProjectsEasyNorthwind. The Add New Project dialog box should pop up on your screen:
    Creating the service projects
  5. Click OK.

    Service Factory will generate ten projects according to the Service Contract and Data Contract Models, including the Business Entities projects, Business Logic projects, Data Access Layer projects, Data Contracts projects, Fault Contracts projects, Message Contract projects, Service Contracts projects, and Service Implementation projects. The Solution Explorer is now full of projects, as shown in the following image:

Creating the service projects

Inside each project, the Service Factory has added the required references, and some configuration files. For example, the ServiceContracts project has references to MyWCF.EasyNorthwind.DataContracts, MyWCF.EasyNorthwind.FaultContracts, and MyWCF.EasyNorthwind.MessageContracts, as well as System.Runtime.Serialization, and System.ServiceModel.

It has also generated a hosting application and a testing client for the service, which we will customize later.

Linking contract models to projects

Now that we have all of the projects generated, we can tell the Service and Data Contract Models which project they should link to.

  • Open the Service Contract Model diagram, click on an empty space in the design pane, and from the Properties window, change the property Project Mapping Table to MyWCF.EasyNorthwind.

    The project mapping table, MyWCF.EasyNorthwind, is an element within the ProjectMapping.xml file. This element was added to the project mapping XML file when we created the projects in the previous section. It maps from solution projects to a set of predefined roles. So, after we specify the Project Mapping Table of the service contract model to this element, and later generate the code, the Service Factory will know which project the generated artifacts should go to.

Linking contract models to projectssource code, service factoryservice projects, creating
  • Open the Data Contract Model diagram, click on an empty space in the design pane, and from the Properties window, change the property Project Mapping Table to MyWCF.EasyNorthwind.

Validating the contract models

At this point, we have finished modeling our service contracts, and have also specified technologies for the service implementation. Both models should now be valid.

To see the validation results, right-click on an empty space in the design pane for either of the contract models, select Validate All from the context menu, and you will see the following messages in the output window:

------ Validation started: Model elements validated: 17 ------

======== Validation complete: 0 errors, 0 warnings, 0 information messages =======

If you have tried to validate any model prior to this point, you may have seen a few validation errors, such as "Project Mapping Table property is empty".

You can compile and build the solution successfully now, though it is not yet complete (actually, there is no source code to build in the solution at this point).

Generating the source code

The Service Factory has now generated ten projects, but it hasn't generated any source code from the Service and Data Contract Models. You can build and run it now, but it won't be functioning as a WCF service. In this section, we will ask Service Factory to generate all of the necessary source code for the service.

  1. Open the Data Contract Model.
  2. Right-click on the design pane.
  3. Select Generate Code from the context menu.

    The Factory will generate source code for all of the defined Data contracts and Fault contracts in the model.

    Generating the source code
  4. Open the Service Contract Model.
  5. Right-click on the design pane.
  6. Select Generate Code from the context menu.

    The Factory will generate source code for all of the service interfaces, service implementations, operations, messages, and any other necessary files.

You may see a warning message saying that a custom tool failed. Do not worry about this warning. It's ok for us to move on.

Generating the source code

You can also right-click on any shape in the Contract Model diagrams, and select Generate Code for that shape only. This feature is helpful if, later on, you need to change some of the models.

Now, open a project under the Service Interface folder, such as MyWCF.EasyNorthwind.DataContracts. You will see the generated files under the GeneratedCode folder.

This will generate the source code for all of the service interface projects, including the Data Contracts project, Fault Contracts project, Message Contracts project, Service Contracts project, and Service Implementation projects. If you check the business entities project, the business logic project, or the data access project, you will find that no source code has been generated yet. There is no code generated for the test client, or the host application either. So the EasyNorthwind solution is still not a complete WCF service. In the next chapter, we will complete it by generating and customizing all of the other projects.

Build the solution again. There should be no errors.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset