Pete On Software

RSS Feed

Archive for July, 2008

Web Services Software Factory (4 of 5)

Note: All links current as of the time of this blog post

We are in the home stretch. If you haven’t already, you may want to take a look at tutorial parts 1, 2, and 3 before beginning this one.

At this point, we’ve defined our service, created Data Contracts and Service Contracts, implemented the serivce, and programmed the code behind the endpoints. All we have left to do before this service is fully functional is to create the Host Contracts, which will set up the actual endpoints for us.

In your Solution Explorer, right click on the MyCryptographyService.model and choose Add –> New Model. In the dialog, select Host Model and complete the the form and click finish.
New Host Contract Model Dialog

This brings up a new designer surface for the Host Designer.
Host Model Designer

The very first thing we need to do is to right click on the Host Model in the Host Explorer and select “Add New Host Application”.
Add New Host Application Menu

This will create HostApplication1. Right click on it and view properties. Change the name to “CryptographyServiceWS” and the Implementation Technology to “WCF Extensions”. For Implementation Project you need to pick the project that is going to be the one the people will actually connect to. You can create your own Web Services project if you’d like, but there is one already included for us, so I will use that one for the purposes of this experience. Expand the dropdown as shown below and choose MyCrytographyService.Host.
Choose a Host Project

Return to the Host Explorer, right click on CryptographyServiceWS and choose “Add New Service Reference”. This creates ServiceReference1.
Add Service Reference to Host

Right click on ServiceReference1, choose properties and set its name to CryptService. Select to Enable Metadata Publishing to true. For Service Implementation Type, click the ellipses to bring up this dialog and choose CryptService.
DSL Model Element Selector

Go back to the Host Explorer. Right click on CryptService and choose “Add New Endpoint”. Endpont1 will appear. Right click on Endpoint1, select properties and change its name to Crypt, leave Binding Type at basicHttpBinding, and enter “Basic” in the Address field. As the tooltip says, the Address field allows you to append something to the name of the endpoint so that you can create many different endpoints, each to the same code. The point being to allow users to connect with many different bindings, but to do that you must create an endpoint for each. Rather than have many different endpoint names, you can just add a suffix. Additionally, this can also be used to version the endpoints on your service.

That’s it. When you click again on CryptService in the Host Explorer, you see this screen on the left. Click “Generate Service” and if you’ve done everything correctly, you should be set to go. If you do not see this screen, that means you have left out a step. Please review the tutorial up to this point and if you are still stuck, leave me a comment and I will try to help.
Auto Generate Service

Now, if you go back to your Solution Explorer and navigate to the Tests folder and look inside the MyCryptographyService.Hosts project, you will see the CryptService.svc file.

This is the “page” that people will connect to. In fact, if you right click on it and choose “View in Browser” you should see the page below.
CryptService Page in Browser

If you click the link that is provided on that page, you can see the WSDL from the service itself.
CryptService WSDL

That’s it. Your WCF service is now fully functional. In the final piece of the tutorial, we’ll make a C# Console Application that calls our service and gives us back some results.

Web Services Software Factory (3 of 5)

If you missed the beginning of the series, you should check out Part 1 and Part 2 before beginning this part if you want to follow along.

Today’s tutorial will create the service contracts for our WCF service and actually implement the methods (read: write some code that does something).

First, in your Solution Explorer, right click on MyCryptographyService.model and Add–>New Model.
Right Click Add New Model.

You will need to fill out the form like you see below.
Create New Service Contract Model

If you don’t already have your property window handy, right click in the white designer surface and click properties. Just like with our Data Contracts, select our Project Mapping Table and choose WCF Extension from the “Implementation Technology” dropdown. Set the “Serializer Type” to DataContractSerializer.
Service Contract Model Properties

Since you should be familiar with how the designer surface works (from part 2 of the tutorial), I’m going to move through this part a little more quickly. The toolbox is context sensitive and now displays tools relevant to Service Contracts.
Service Contract Toolbox Context

Drag a Service (name: CryptService), a Service Contract (name: CryptService), an Operation (name: EncryptString), and two messages (EncryptStringRequest and EncryptStringResponse) out onto the designer surface. Arrange and them as shown below.
EncryptString Model

Use the connector tool to connect the objects. Drag a connection between the Service Contract and the Service. Drag another connection between the Operation and the Service Contract. This part is a little bit tricky. You need to associate the “Request” and “Reponse” messages to the operation. However, up to this point, which direction you connected the object did not matter. For the messages, it does. Start on the “Request” message and drag to the Operation. Then for the other connection, start on the Operation and drag it to the “Response” message. You have now finished modeling this Operation.
EncryptString Model Complete

You should drag 2 more Operations onto the surface and call them DecryptString and HashString. Drag 4 more Messages onto the surface and call them DecryptStringRequest, DecryptStringResponse, HashStringRequest, and HashStringResponse. Connect them up like you did for the EncryptString (attaching the Operations to the Service Contract).
Service Model Sans Payload

Now you are almost done. You need need to load up the messages with their payloads. Right click on EncryptStringRequest and Add–>Data Contract Message Part. Name it EncryptionObject. While it is highlighted, examine the Properties Panel. Next to Type, click the Elipses (…) to bring up this dialog box. Choose EncryptionObject from the box and click OK.
Add Encryption Object to EncryptStringRequest
Add EncryptionObject Type

You should also add an EncryptionObject to DecryptStringRequest and a HashObject to HashStringRequest. Add Primitive Message Parts to each of the three responses and call each one “ReturnValue”. (I realize I haven’t come with the most “enterprisey” names during this tutorial… I’m sorry).
Completed Service Contract Model

Now, right click somewhere in the white of the Visual Designer and choose Validate All. If that comes back with errors, recheck your work. If it comes back clean, right click again and choose Generate Code. You will then have additional generated code appear in MyCryptographyService.ServiceImplementation and MyCryptographyService.ServiceContracts.

Next, right click on the MyCryptographyService.BusinessLogic project and choose to add a new class. Call it CryptographyServiceBusinessLogic. To save some time (and since this code isn’t the point of the tutorial), I’ve included the code here for you.

Next, right click on the MyCryptographyService.DataAccess project and add a new class called CryptographyServiceManager. The code for it is below.

Lastly, we only need to do one more thing before this step of the tutorial is complete. We need to implement the methods that tie into what will be our endpoints. When we defined our service contracts, we defined some methods. We need to write the implementation of those methods that tie them to our business layer (and ultimately to our data access layer).

However, since the generated code can and will change every time we make any changes to the service contract designer, we have to create another code file and create a partial class with the same name as the generated one. Inside, we will override the methods, write our own implementation, assign values to the response, and return it. Create a class in the MyCryptographyService.ServiceImplementation project called CryptImplementation.cs. Then you should have the following code inside.

Notice that this is the class that also uses the translators that you wrote. They allow the class that is the DataContract to be mapped to the class that is the BusinessEntity. The code here should be pretty straightforward, but if you have any questions, please leave them in the comments and I will respond as quickly as possible.

Part 4 should be coming shortly. Until then.

  Recent Entries »