Hiển thị các bài đăng có nhãn WCF. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn WCF. Hiển thị tất cả bài đăng

Thứ Sáu, 18 tháng 6, 2010

Combine Microsoft ReportViewer, WCF and ASP.NET

Scenario:

- We have a existing Website portal using ASP.NET

- We have a reporting desktop application using Microsoft ReportViewer

- We want to share some report from reporting application to website portal

Solution:

- We use WCF as interface to integrated between ASP.NET and desktop applications

- WCF will provides reports as streaming images data.

Implementation:

Step 1: Create WCF Services

- Create new WCF Service Application from Visual Studio 2010

A21319952F68E79E_862_0[1]

- Define Service Contract

[ServiceContract]
public interface IService1
{
    [OperationContract]
    Stream GenerateImage();        
} 

- Implement Service Contract. We must to export LocalReport to image stream



public System.IO.Stream GenerateImage()
        {
            LocalReport report = new LocalReport();
            //string path = @"D:\Report1.rdlc";
            string path = HttpContext.Current.Server.MapPath("~/Report1.rdlc");
            report.ReportPath = path;
 
            Warning[] warnings;
            string[] streamids;
            string mimeType;
            string encoding;
            string extension;
 
            byte[] bytes = report.Render(
               "Image", null, out mimeType, out encoding,
                out extension,
               out streamids, out warnings);
            Stream stream = new MemoryStream(bytes);
            return stream;
        }

- Enable WCF Streaming in configuration file



<basicHttpBinding>
  <binding name="HttpStreaming" maxReceivedMessageSize="67108864"
           transferMode="Streamed"/>
</basicHttpBinding>

- Create a new Report1.rdlc as following:


A21319952F68E79E_862_1[1]


- Step 2: Test the Service on Windows Form:


- Create new Windows Forms


- Add services reference to your service


- Add a PictureBox control to Form interface then add following code in code behind



public Form1()
        {
            InitializeComponent();
            ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
            Stream memList = client.GenerateImage();           
           
            System.Drawing.Image image = System.Drawing.Image.FromStream(memList);                  
            pictureBox1.Image = image;
        }

- Run the Form you will see:


A21319952F68E79E_862_2[1]


- Step 3: Rendering Image to website portal


- Similar in Windows Form, ASP.NET can display image from stream by very simple code:



protected void Page_Load(object sender, EventArgs e)
        {
            ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
            Stream memList = client.GenerateImage();
         
            System.Drawing.Image image = System.Drawing.Image.FromStream(memList);
            image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);            
        }

- You will see:


A21319952F68E79E_862_3[1]


Conclusion:


There are three technical used to resolve this scenario:


- Export ReportViewer to image stream


- Enable Streaming in WCF


- Rendering Image from streaming data.


Reference:


- How to: Enable Streaming


- LocalReport.Render Method (String, String, out String, out String, out String, out String[], out Warning[])

Thứ Bảy, 19 tháng 9, 2009

WCF và tính tương thích với ASP.NET

Một sự khác biệt to lớn giữa các Web Services truyền thống ASMX và WCF Services là khả năng kích hoạt của chúng. Với WCF các services có thể được kích hoạt thông qua các giao thức ngoài HTTP như net.tcp, net.pipe hoặc WAS (Windows Activation Service). Trong khi ASMX service chỉ có thể được kích hoạt thông qua HTTP, và do đó nó liên kết chặt chẽ với ASP.NET HTTP pipeline. Điều này khiến cho các WCF services trở nên khá linh hoạt và động lập trong quá trình triển khai. Chúng có thể được deploy ở IIS, Windows Services hoặc các host tự tạo. Ngay cả khi WCF service được triển khai trên môi trường IIS, bên trong một ASP.NET application thì nó cũng không phụ thuộc vào các features của ASP.NET. Chúng ta dễ dàng nhận thấy rằng, các features trong ASP.NET như:

  • Authentication
  • Url/File authorization
  • Impersonation
  • Session state
  • Request cache
  • Globalization
  • Custom HttpModules

    Các features này thuộc về các thư viện HTTP:

  • System.Web.HttpApplication
  • System.Web.HttpContext

    Các features này sẽ không có khả năng sử dụng nếu như chúng ta không thiết lập ASP.NET Compatible Mode cho WCF. Khi đó, với HttpContext, đối tượng Current là luôn luôn null trong WCF. Để sử dụng các features này trong WCF, chúng ta cấu hình ASP.NET Compatible mode cho WCF. WCF cung cấp khả năng hỗ trợ ASMX thông qua các phương thức hosting khác nhau

    • Mixed Transports Mode
    • ASP.NET compatibility mode

    Để thiết lập ASP.NET Compatible Mode, chúng ta tham khảo hướng dẫn trên MSDN:

    http://msdn.microsoft.com/en-us/library/aa702682.aspx

    Reference: ASP.NET Compatibility Mode

  • Thứ Ba, 18 tháng 8, 2009

    WCF and ASP.NET Membership error: An error occurred when verifying security for the message

    System.ServiceModel.Security.MessageSecurityException: An unsecured or incorrect
    ly secured fault was received from the other party. See the inner FaultException
    for the fault code and detail. ---> System.ServiceModel.FaultException: An erro
    r occurred when verifying security for the message.
       --- End of inner exception stack trace ---

     

    I got this error message when deploy and use WCF with ASP.NET Membership over Internet. My WCF service use wsHttpBinding and Security mode = TransportWithMessageCredential. My machine for test are Windows 7, Visual Studio 2008 SP1, SQL Express 2005. I’ve spent more time to looking for solution. After that, It’s worked fine! Here are work around and solutions

    Open Event Viewer

    Following error was appeared:

    Failed to generate a user instance of SQL Server due to failure in retrieving the user's local application data path. Please make sure the user has a local user profile on the computer. The connection will be closed. [CLIENT: <local machine>]

    A21319952F68E79E_525_0[1]

    This is the root of problem.

    Solution:

    Add following account to SQL Server

    - IIS APPPOOL\DefaultAppPool

    - Local System

    - Local Services

    2. Open the Application Pool of your website used.

    A21319952F68E79E_525_1[1]

    You see: Identity: ApplicationPoolIdentity (Windows 7 IIS)

    A21319952F68E79E_525_2[1]

    3. Change the Identity to one of other account then iisreset

    A21319952F68E79E_525_3[1]

    Hope this help!

    Thứ Năm, 14 tháng 5, 2009

    WCF và nguyên lý ABC

     

    A21319952F68E79E_308_0[1]