以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Dot NET,C#,ASP,VB 』  (http://bbs.xml.org.cn/list.asp?boardid=43)
----  Using a transacted C# component (leveraging COM+)  (http://bbs.xml.org.cn/dispbbs.asp?boardid=43&rootid=&id=11746)


--  作者:admin
--  发布时间:11/9/2004 2:25:00 AM

--  Using a transacted C# component (leveraging COM+)


发信人: Jobs (温少), 信区: DotNET        
标  题: Using a transacted C# component (leveraging COM+)
发信站: BBS 水木清华站 (Wed May  9 00:04:56 2001)


Using a transacted C# component (leveraging COM+) from ASP.NET

Scott Guthrie
January 4, 2001

Level: Beginner/Intermediate

  

Several people have asked questions like "is COM+ dead", "how does  
COM+ relate to .NET", etc. The answer is that COM+ is definitely not  
gone. It remains and will continue to be an important part of the  
overall programming model, and it can be fully leveraged from ASP.NET  
and the overall .NET Framework. You can continue to create COM+  
components using VS6 and other compilers and then call them from ASP.NET
pages. You can also take advantage of COM+ services with new classes  
built using the .NET Framework.  

You will find that leveraging these services, for example: COM+  
transactions and COM+ object pooling, has become a whole lot easier  
using ASP.NET and the .NET Framework. For example, it is now possible to
avoid having to manually install a transacted component using the  
COM+ Component Services Administration tool. Instead, if you want a  
component to support transactions, you can simply add a "Transaction"  
attribute to the top of a class and copy the compiled component into the
"bin" directory of your ASP.NET web application. When the class is  
first activated, it will automatically be registered with COM+ for you,
which means "xcopy deployment" of both non-transacted and transacted  
components.  

You can also now take advantage of the COM+ "AutoComplete" feature  
within your .NET components simply by marking activating methods  
within an "AutoComplete" attribute. Assuming no exceptions get thrown  
during the method's execution -- the transaction will automatically  
commit for you. If you want to force termination of a transaction,  
simply raise or throw an exception to abort it.

The below sample demonstrates how to build a simple transacted C#  
class and use it within an ASP.NET page written using VB. Note that I've
only tried this on Win2k; I'm not sure whether this works on NT4 in  
beta1:



Account.cs:



using System;
using Microsoft.ComServices;


// COM+ app name as it appears in the COM+ catalog
[assembly: ApplicationName("TestApp")]

[Transaction(TransactionOption.Required)]

public class Account : ServicedComponent  

{

    [AutoComplete]

    public String Debit(int amount)  
    {
        // Todo: Do some Database work, throw an
        // exception to abort the transaction.
        // otherwise the transaction will commit.
        // Use the ContextUtil class to return the  
        // transaction id
        return ContextUtil.TransactionId.ToString();
    }
}



To compile the above class, simply execute the following code within the
root directory of an ASP.NET application:  


mkdir bin  
csc /target:library /out:bin\Account.dll Account.cs
/r:Microsoft.ComServices.dll  


then invoke the component from the following ASP.NET Page (no  
registration required):



Trans.aspx:  



<html>

  <script language="VB" runat=server>

    Sub Page_Load(Sender As Object, E as EventArgs)
        Dim MyAccount as New Account()

        TransId.Text = MyAccount.Debit(343)
    End Sub

  </script>
<body>
    <h1>C# Transacted Component within ASP.NET</h1>
    <b> Transaction ID: </b> <asp:label id="TransId" runat=server/>
</body>
</html>  



The transaction id of the completed transaction should be rendered  
cleanly into the output. To see how the COM+ auto-registration mechanism
worked, open up the "Component Services" admin tool within the  
"Programs->Administrative Tools" item on the Start Menu. Expand the  
"Component Services" tab to "Computers->My Computer" and then to "COM+  
Applications".

Notice the presence of the "TestApp" COM+ Application/Package. This  
was created in response to line 4 of the Account.cs file (which had an  
assembly attribute indicating the COM+ App name to create). Expand  
this to see the "Account" component listed within it. Notice that the  
properties of the component (for example: transaction=required) within  
the admin tool match those set within the Account.cs file.

Then click on the "Distributed Transaction Coordinator->Transaction  
Statistics" item within the admin tool. Notice that the transaction  
count increases each time you hit the page.

Note that the above sample demonstrates calling a transacted component  
from a non-transacted ASP.NET Page. Note that if you want the ASP.NET  
page itself to be transacted as well (for example: if you were calling  
multiple transacted components from it and wanted them to all share  
the same transaction), simply add the <%@ Page Transaction="Required" %>
attribute to the top of the file.


  


--

※ 来源:·BBS 水木清华站 smth.org·[FROM: 210.39.3.110]
上一篇
返回上一页
回到目录
回到页首
下一篇



W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
3,671.875ms