Create a Web site service (Web services)
Web Services is based on some standard (usually SOAP) that enables applications to exchange data over HTTP. Web services are made up of Web methods, which expose these methods and can be run on the network, and, to a certain extent, can be seen as a function of F # because the site method has a name, parameters, and results, parameters and results are described in metadata (metadata), The Web service exposes these as well, because the client can know how to invoke it.
Creating a Web site service is simple. In fact, the main problem with creating a Web site service might be how to expose code through a Web server. When a Web server receives a request for a file as a URL, it must tell the Web server which. NET class The request should be mapped to. You typically run a specific F # class with an. asmx file in response to a request for this web site service if the Web server obtains a request for the. asmx file. The exact implementation method differs depending on the development environment and the Web server hosting the service.
Visual Studio has a built-in Web site server, so to create a new site site, simply select the new Web site, file, and then select the location of the site. A site can only run pages written in C # or Visual Basic. NET, so you also need to add an F # project to your solution, and then manually modify the solution file so that it resides in the site directory. It sounds complicated, it's easy to do, just copy the. fsharpp file to the Web site directory, open the. sln file with Notepad, and modify the path to the. fsproj file. Figure 11-3 is the. sln file before and after the modification, and the original file on the left.
Figure 11-3 How to modify the. sln file
After that, you need to configure the output of the project file as a class library and write to the bin subdirectory. This seems to be a certain effort, but in the future just press F5 on the line, the project will be automatically compiled and run.
[
1, the output for the class library, this one as long as the confirmation can be, because when the project will be asked to choose, so, only when the class library is not selected, now only to modify;
2, write to the bin subdirectory, you need to enter in the post-build event command line:
Copy $ (targetfilename) E:\WebSites\ASP.NET\bin
This assumes that E:\WebSites\ASP.NET is the directory where the site resides and that a new bin subdirectory has been created.
]
Without Visual Studio, the next best thing to do is to use Internet Information Services (Internet Information Services,iis) to host the site, Microsoft's own Web server for Windows. In a sense, this is easier than hosting with Visual Studio, but it's not easy to run after the code is finished. In order to host the code in IIS, you need to create an IIS virtual directory, and a subdirectory bin, and then copy the. asmx page and the Web. config file into this virtual directory.
The service itself is simple. It should be a class that inherits from System.Web.Service.WebService, has a parameterless constructor, and should be labeled as System.Web.Service.WebServiceAttribute. If you intend to expose a Web site service, you must set the namespace for the property (Namespace), which is http://tempuri.org by default, and even if you do not intend to expose the service, setting this property will also benefit the manageability of the Web site service. Then, the members of the class, that is, the Web site method, with System.Web.Service.WebMethodAttribute tag [original WebServiceAttribute error], it also has a lot of useful properties, It is particularly worth setting the description (Description) property, which enables the client of the service to know what to obtain.
Listing 11–7 defines a simple web site service, creates a type service, and it has a member of addition's services, and the parameters must appear in the style of the tuple.
Listing 11-7 creating a simple Web site service
namespace Strangelights.webservices
Open System.Web.Services
[<WebService(Namespace =
"Http://strangelights.com/FSharp/Foundations/WebServices") ;]
type Service =
Inherit WebService
New () = {}
[<WebMethod(Description ="perfomsinteger addition");]
member S.addition ((x: int), (y: int)) = x + y
[
Need to cite system.web, System.Web.Services, system.enterpriseservices;
Put X. addition to s.addition; Of course, it is not necessarily s, as long as it does not have the same letters as the X and y that appear later.
]
To let the site server discover the site service, you need to create an. asmx file. The following is an example of an. asmx file, and most importantly, setting the Class property to the name of the service. When the server receives a request for this file, it will tune the corresponding service:
<%@ webserviceclass= "Strangelights.WebServices.Service"%>
If you run the service locally, you can test it simply by opening it in the browser. In the browser, you will see the interface shown in 11-4, which provides the parameter values for the Web service and invokes the service.
Figure 11-4 Calling the local Web site service
Using 46 and 28 as the parameter values, call this service to produce the following XML:
<?xml version= "1.0" encoding= "Utf-8"?>
<intxmlns= "Http://strangelights.com/FSharp/Foundations/WebServices" >74</int>
In general, sending small amounts of data over a network is inefficient because every request sent must have a fixed amount of metadata. Therefore, a better approach is to build an application that cannot be "chatty", that is, the application should send a large request instead of sending a number of small requests.
The Web service will attempt to serialize the method return value of any. NET object (serialize) into XML, however, the result may be somewhat unpredictable, so the resulting XML data cannot contain all the expected fields, so it is difficult to use in other programming languages. To avoid this, it is best to use the XSD schema to define the objects that you want to pass across the site service. An XSD schema is an XML document that describes the structure of an XML document, describes what fields are mandatory, what order fields should appear, and so on, which becomes part of the site service definition. All the people who use this website service can understand what field he will expect from this site, and it is best to have a field in the defined Web service that contains binary or hexadecimal data, because users who serve the site have more opportunities to understand the meaning of the results.
While it is possible to define an XML schema yourself, Visual Studio has some graphical and textual ways of creating schemas, and there are many predefined architectures on the web that can be downloaded, and it is possible to build on them. For example, the example below will be seen using Rnaml, which can be found on http://www-lbit.iro.umontreal.ca/rnaml/, which is intended to exchange RNA (RNA) in XML form, A structure that is provided by people who are information about a substance studied by molecular biologists. After downloading the schema from the site, you can use the command-line tool Xsd.exe to convert the schema. NET form of representation. This tool generates a # class to represent XML data, and in general, each tag in the XML becomes a class of. NET; you can then compile the C # file into a. NET assembly for use by F # or any other. NET Library. The completed command line might look like this:
XSD rnaml.xsd/classes
Note that you need to rename the downloaded schema file Rnaml.xml to rnaml.xsd for the tool to run correctly.
The following example shows how to create a Web site service that returns a yeast RNA molecular structure, which is a reduced version of the series example on the Rnaml website (http://www-lbit.iro.umontreal.ca/rnaml/). As before, you need to create an. asmx file, link the requested file to a. NET type, and process the request:
<%@ webserviceclass= "Strangelights.WebServices.DnaWebService"%>
Listing 11-8 shows this Web service, and you can see that the basic components of this site are the same as the simple Web services that preceded them. As before, there is a class definition labeled WebService attribute; The code does the work of defining the method Getyeastmolecule, where we create and publish different objects that are defined in the library created with the Rnaml.xsd file. such as molecular objects and sequence objects.
Listing 11-8 Creating a Web service that returns the definition of RNA molecules
Namespace Strangelights.webservices
Open System.Web.Services
The Web service class
[<webservice (namespace=
"Http://strangelights.com/FSharp/Foundations/DnaWebService") ;]
type Dnawebservice () =
Inherit WebService ()
//The Web method
[<webmethod (Description = "Gets arepresentation of a yeast molecule");]
member x.getyeastmolecule () =
//The code that populates the yeast XML structure
Let tax = new taxonomy (domain = "Eukaryota", Kingdom ="Fungi",
Phylum = "Ascomycota", "class" = "Saccharomycetes",
Order = "Saccharomycetales",
Family = "Saccharomycetaceae",
Genus = "Saccharomyces",
Species = "Saccharomycescerevisiae")
Let id = new identity (name = "Saccharomycescerevisiae trna-phe",
Taxonomy = Tax)
Let yeast = new molecule (id = "yeast-trna-phe", identity = ID)
Let NumRange1 = new numberingrange (start = "1", Item ="Ten")
Let NumRange2 = new numberingrange (start = "One", Item ="$")
Let Numsys = new numberingsystem (id="natural", usedinfile=true)
Numsys.items <-[|box numRange1; boxnumrange2|]
Let Seqdata = new seqdata ()
Seqdata.value <- "gcggauuuagcucaguuggg agagcgccag acugaagauc
UGGAGGUCCU GUGUUCGAUC Cacagaauucgcacca "
Let seq = new sequence (Numberingsystem = [|numsys|], Seqdata =seqdata)
Yeast.sequence <-[|seq|]
Yeast
Also, as simple as this, you can use the Web-based test method to run the preceding code to produce the following XML:
<? XML version ="1.0"encoding="utf-8"?>
< molecule Xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"id=" Yeast-trna-phe">
< Identity >
< name > Saccharomycescerevisiae trna-phe</name>
< Taxonomy >
< Domain > Eukaryota</domain>
< Kingdom > Fungi</Kingdom>
< Phylum > Ascomycota</phylum>
< class > Saccharomycetes</class>
< Order > Saccharomycetales</order>
< Family > Saccharomycetaceae</family>
< Genus > Saccharomyces</genus>
< species > Saccharomyces cerevisiae</species>
</ Taxonomy >
</ Identity >
< sequence >
< Numbering-system ID ="natural"used-in-file="true">
< Numbering-range >
< Start >1</start>
< End ></end>
</ Numbering-range >
< Numbering-range >
< Start ></start>
< End ></end>
</ Numbering-range >
</ Numbering-system >
< Seq-data > GCGGAUUUAGCUCAGUUGGG agagcgccag acugaagaucuggagguccu guguucgauc cacagaauuc gcacca</seq-data >
</ sequence >
</ molecule >
This example returns a static, unchanging XML document that is not particularly useful, but it is easy to see the potential of such a program. If you do not use the Getyeastmolecule method, but use a more realistic method Getmolecule, the name of the molecule as a parameter, you can retrieve the details of the molecule in the database, return the results of the molecular data. The benefit is that the result data can be used by programs running on any platform. Now that the sample site has provided programming interfaces for C + + and Java processing data, we have seen that processing this data in F # is straightforward. Of course, this technology is not limited to molecular biology, and can be found in almost all areas of use of XML architecture, science, engineering, mathematics and finance.
There are several ways to enhance the protection of web services, to protect the site services can be guaranteed to only some users access, or the transmission of information on the network to be encrypted or signed. One approach is to upgrade to Windows Communication Foundation (Windows Communication FOUNDATION,WCF), which is similar to Web services, but provides more flexibility in this area, which is discussed in the next section, and the other way is to configure our own Web server. Address these security requirements.
[
Need to cite system.web, System.Web.Services, System.EnterpriseServices, and Rnaml;
However, since it was not downloaded to the Rnamk.xml, the subsequent work was impossible.
]