The program that calls the agent animation in ASP. NET (C #)

Source: Internet
Author: User
Tags list of attributes net command hosting com
1. Reference
Using agentobjects; // Add reference
Using agentserverobjects;
2. Write Program

Agentobjects. Agent axagent = new agentobjects. Agent ();
Iagentctlcharacterex character;
Axagent. characters. Load ("Genie. ACS", (object) "Genie. ACS"); // error location
Character = axagent. characters ["Genie"];
Character. languageid = 0x409;
// Display the character.
Character. Show (null );
Character. Speak ("Hello World", null );
Response. Write (character. Name );
Character. Play ("wave ");
Character. Play ("hide ");
// Why does the program always make an error when loading characters!

**************************************** ********************************
As we all know, ASP. NET is created based on the universal language Runtime Library, that is, the so-called Managed execution environment. Generated Code It is called managed code. The compiler can Source code The description contains metadata, and the Runtime library obtains the managed code information from metadata. The components we write are generally created in an unmanaged environment. These components often do not contain metadata information. How can we use these metadata information in managed code?

To use unmanaged code in a hosted environment, you must first manually generate metadata information for these existing components. A tool is required to generate metadata. Metadata can be obtained from somewhere. The source of this metadata is the type library. In fact, all components today have a Type Library.

To illustrate how to generate metadata, take the Microsoft Agent component as an example. This component is similar to the Office Assistant, which can complete a variety of human actions and support speech recognition and speech synthesis. This component is installed by default in general operating systems. If your system deletes this component, you can download it at http://microsoft.com/msagent/downloads.htm.

1. Create metadata first

The Microsoft Agent control is installed in the % SystemRoot % \ msagent \ directory by default. We can first locate the directory from the Visual Studio. NET command prompt menu item in the Start Menu. Execute the following command in this directory.

Tlbimp agentctl. dll/out: agentctl_proxy.dll
With this command, we can create a proxy file agentctl_proxy.dll containing metadata information. Note that this is not a real dynamic connection library file, but an assembly file (assembly), that is, our agent for hosting COM components.

You can also create an aximp.exe program using the. NET Framework. The command line is as follows, so that two files are generated, as shown in. Note that the agentobjects. dll and agentctl_proxy.dll files are identical.

Aximp % SystemRoot % \ msagent \ agentctl. dll



2. install it in ASP. NET
There are two ways to install metadata files in the ASP. NET environment:

First, copy the file to the bin directory of the application.

The second is to copy it to the ASP. NET directory.

The second method has a defect. You must insert a @ Assembly statement on each ASP. NET page using the component. Save to the bin directory to avoid this. Assembly is loaded to all ASP. NET pages of the application. Therefore, we 'd better put it in the bin directory.

3. Il Disassembly)

Now it's almost time for most programmers of VB or VBScript to have a headache. We need to use a counter compiler to generate some important information.

The namespace of assembly (using the @ Import Statement)

Exact Class Name (this may be very different from ASP !)

Data Types of methods and attributes

Exact spelling of methods and attributes. Note that in C #, bodytext and bodytext are two completely different variables.

To generate the above information, you must use the Il (intermediate language) disassembler中. After generating the meta data, you can move them to the desired directory and directly access ildasm.exe to activate this command. You can then use file | open to select the corresponding file. The displayed result is similar to that obtained by OLE/COM viewer.





Note that the generated assembly files are different, and the names of namespaces are different. On ASP. NET pages, @ import is often used to import namespaces, but their classes, attributes, and methods are the same. For ASP. NET programmers, the entire blue symbol is very important, because they represent a class or an interface. If we expand these classes or interfaces, we will get a list of attributes and methods.

All the methods we see have an exact data type description. The first is the method name. The colon is followed by the parameters required by the return value type in parentheses.

When we talk about using the GET _ and SET _ methods to describe attributes, VBScript programmers may be confused. We know that each attribute actually corresponds to a private member in the class, the read and write operations of attributes are implemented through these two functions.



Each attribute is marked with a red flag. Double-click this attribute to get the attribute definition. We can see that the attribute can be get, set, or get/set.

4. Use of components
After we have a clear understanding of metadata, the rest of our work is to use this component in ASP. NET. The following is a piece of code using msagent:

<% @ Page Language = "C #" %>
<% @ Import namespace = "agentobjects" %>
<%
Axagentobjects. axagent = new axagentobjects. axagent ();
Iagentctlcharacterex character;
Axagent. characters. Load ("Genie", (object) "Genie. ACS ");
Character = axagent. characters ["Genie"];
Character. languageid = 0x409;
// Display the character.
Character. Show (null );
Character. Speak ("Hello World", null );
Response. Write (character. Name );
Character. Play ("wave ");
Character. Play ("hide ");
%>
After the Type Library is imported, we will create an instance of this class. The remaining usage is basically the same as that of ASP application components.

The calling methods of all COM components are basically the same, just as in hosting applications (C #, Visual Basic. add a COM component reference, and the system will generate an encapsulation for these unmanaged code. In fact, the above step is also executed, that is, the imported type library. In fact, this method can be used in all managed applications, including hosted C ++ code, C #, Visual Basic.. NET environment ,. the interoperability of the. NET Framework protects the existing investment of component developers and provides a way of thinking for our managed program design. That is, we can encapsulate many functions that are difficult to implement or implement using managed code (for example, calling the API function to read and write the physical sector of a floppy disk) into an unmanaged COM component, indirectly implemented through component calls.

Related Article

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.