'3. Implementation/Web Service'에 해당되는 글 3건
- 2009.11.27 Create a simple file transfer Web service with .NET
- 2009.06.22 웹 서비스 트랜잭션
- 2009.06.14 Registry 와 Repository 의 차이점
Create a simple file transfer Web service with .NET
![](https://tistory1.daumcdn.net/tistory/205853/skin/images/bg_clear.gif)
Wednesday, August 31, 2005 10:34 AM
The beauty of .NET is its simplistic approach towards solution development. With just a few lines of code, you can create powerful solutions without expending too much effort into the how's and why's of the inner workings of modular design.
Standard types ensure that all the modules will work in concert, and the Framework ensures that components will communicate with each other in a nice fashion. One way to observe this "active translation" is a .NET Web service. In this article, I'll develop a Web service that will receive and produce files as binary data.
Sending binary data across the Internet does have its disadvantages. For instance, different operating systems respond to binary data in different ways. In order to avoid the confusion, the base64 encoding scheme was incorporated to encode binary data so all operating systems could translate the data to their native tongues.
In order to send files across the wire before, you usually had to open the file, read the contents into a buffer, encode the buffer, and send the encoded data down the wire. In legacy ASP apps, I would open files with an ADODB.Stream object, encode the stream using the CAPICOM.Utilities functionality, and either set the return value of the function as a String or write the encoded string to the Response buffer. That's a lot of overhead.
With .NET, you use the File class in the System.IO namespace to open the file. This returns a Stream object to which you can cast as a BinaryReader or BinaryWriter object. Using the BinaryReader object's ReadBytes method, you can obtain a byte array that you can use as the return value on the Web method. Here's the code:
<%@ WebService language="C#" class="FileRW"
Debug="true" %> using System; using System.Web.Services; using System.Xml.Serialization; using System.IO; [WebService(Namespace="http://someplace.com/FileIO/")] public class FileRW : WebService { [WebMethod] public byte[] GetFile(string filename) { BinaryReader binReader = new BinaryReader(File.Open(Server.MapPath(filename), FileMode.Open, FileAccess.Read)); binReader.BaseStream.Position = 0; byte[] binFile = binReader.ReadBytes(Convert.ToInt32(binReader.BaseStream.Length)); binReader.Close(); return binFile; } [WebMethod] public void PutFile(byte[] buffer, string filename) { BinaryWriter binWriter = new BinaryWriter(File.Open(Server.MapPath(filename), FileMode.CreateNew, FileAccess.ReadWrite)); binWriter.Write(buffer); binWriter.Close(); } } |
The GetFile() method accepts a string parameter, filename. The current virtual directory is used to obtain the file using the filename parameter. The file is opened as a BinaryReader type with Read access. The total bytes of the file are stored to a local variable of type byte[] - a byte array. The file stream is closed, and the Web method returns the byte array.
The PutFile() method accepts a byte array, buffer, and a string, filename. A file with the name filename is created as a BinaryWriter type with ReadWrite access. The byte array is written to the file stream and the file stream is closed.
Now test the code by navigating to the .asmx file you just created using your browser. You should be prompted with links to the two Web methods. Then, test the GetFile() method. Enter a file name that you know exists on the virtual root of the Web service application. Click on the Invoke button, and you'll see the base64 encoded string of the file contents.
In order to test the PutFile() method, I created a Web service client using the ASP.NET Web Matrix IDE, which is a free download from Microsoft. Follow the instructions in the Create a Web service client tutorial to create a Web Service Proxy using the Web Service Proxy Generator; however, use the namespace and from above and the URL to your Web service for the WSDL URL. Set the Output Directory to the same directory as your Web service virtual root and then generate the proxy.
Create a new ASP.NET Web page. Add the following code to your new page:
<%@ Page Language="C#" Debug="true" %> <script runat="server"> // Insert page code here // void Button1_Click(object sender, EventArgs e) { string filename = "c:\\temp\\" + Guid.NewGuid().ToString() + ".tmp"; file1.PostedFile.SaveAs(filename); System.IO.BinaryReader br = new System.IO.BinaryReader(System.IO.File.Open(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read)); FileIO.FileRW frw = new FileIO.FileRW(); frw.Credentials = System.Net.CredentialCache.DefaultCredentials; br.BaseStream.Position = 0; Response.Write(br.BaseStream.Length); byte[] buffer = br.ReadBytes(Convert.ToInt32(br.BaseStream.Length)); br.Close(); frw.PutFile(buffer, file1.PostedFile.FileName.Substring(file1.PostedFile.FileName.LastIndexOf("\\") + 1)); } </script> <html> <head> </head> <body> <form enctype="multipart/form-data" runat="server"> <p> <input id="file1" type="file" name="file1" runat="server"/> </p> <p> <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button> </p> <!-- Insert content here --> </form> </body> </html> |
You'll probably have to adjust the permissions in your web.config file to impersonate or allow full control to the ASP.NET machine account to the upload directory - C:\temp\. Also, it's important to notice that if you choose to impersonate, which is how I set up my local server, you need to pass the authenticated credentials along to the Web service. This is done by the line: frw.Credentials = System.Net.CredentialCache.DefaultCredentials;. If you don't do this, you'll probably spend a lot of time debugging. Run the client page and try uploading a file.
The client page saves the uploaded file
(file1.PostedFile) to a local temporary file. It then opens a
buffer and posts the buffer to our Web service with the uploaded
file's FileName. You can see that handling uploaded files is easy
as well.
From : http://www.zdnetasia.com/techguide/webdev/0,39044903,39251815,00.htm
동시에 여러 곳에서 일어나는 변화를 마치 하나의 변화인 것처럼 관리하는 프로세스로, 트랜잭션과 관련해서 일어나는 모든 변화는 '모두 일어나거나' 아니면 '하나도 일어나지 말아야'합니다. 이를 트랜잭션의 'All or Nothing' 원칙이라고 한다.
트랜잭션 개념
ACID (Atomicity, Consistency, Isolation, Durability)
원자성 | 트랜잭션 하나가 완료되었을 때 해당 데이터는 그 트랜잭션 처리가 완료한 후의 상태이거나, 트랜잭션 처리를 수행하기 전의 상태이거나 둘 중 하나이어야 하는 특성이다. |
일관성 | 트랜잭션이 처리되든지, 잘 처리되지 않든지 상관없이 데이터 내용은 일관성을 유지해야 한다는 특성. |
독립성 | 복수의 트랜잭션을 동시에 실행했을 경우와 순차적으로 실행했을 경우의 처리 결과가 일치해야 한다는 특성, 즉 생신 도중의 데이터는 다른 트랜 잭션에서 볼 수 없어야 함. |
내구성 | 일단 하나의 트랜잭션이 완료되면, 이후 발생하는 장애로 인해 데이터 내용이 변하지 않아야 한다는 특성. |
출처 : SDS e-Campus 웹 서비스 기본 과정 중
Registry (레지스트리) 는 사용자를 리소스로 리디렉션하지만 repository 는 실제로 정보를 저장하는 정보 저장소이다.
Microsoft Windows 레지스트리에는 기본 설정과 매개 변수가 포함되지만 궁극적으로 응용 프로그램을 리소스나 바이너리로 안내한다. 프로그램 ID를 기준으로 COM 구성 요소를 조회하면 클래스 ID를 찾게 되고 이를 통해 바이너리 자체가 있는 위치를 알 수 있다.