Showing posts with label solution architecture. Show all posts
Showing posts with label solution architecture. Show all posts

Wednesday, 15 August 2018

VB.NET Extending the MailMessage to have the ability to save the mail message

Problem:

The System.Net.Mail.MailMessage is badly missing the option of saving the Message as a file. Though there is a workaournd by using the SpecifiedPickupDirectory option of the SmtpClient to write the mail message ot the specific folder. This option however doesn't have flexibility in naming the files. In a multi-threaded parallel execution environment, it is difficult to identify and rename the specific files post writing to disk. There ought to be a better solution to handle this problem.

Solution:

You can extend the MailMessage class and add a Save method yourself. The Send method of the SmtpClient creates and uses MailWriter object to write out the mail message and it uses fileMailWriter when the SpecificPickupDirectory is specified as the DeliveryMethod. Create your own MailWriter object using FileStream and invoke the internal Send method, passing your MailWriter object. Check out this Code Project Article for more details. The code samples given there are for the C# language. Just in case you need, given below the same code for the Extension in VB.NET.


Imports System
Imports System.Net.Mail
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.IO
Imports System.Reflection
Imports System.Runtime.CompilerServices

Module MailMessageExtension
    <Extension()>
    Public Sub Save(ByVal msg As MailMessage, FileName As String)

        Dim asm As Assembly = New SmtpClient().GetType().Assembly
        Dim _mailWriterType As Type = asm.GetType("System.Net.Mail.MailWriter")


        Using _fileStream As FileStream = New FileStream(FileName, FileMode.Create)


            ' Get reflection info for MailWriter contructor
            Dim _mailWriterContructor As ConstructorInfo = _mailWriterType.GetConstructor(BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, New Type() {GetType(Stream)}, Nothing)

            ' Construct MailWriter object with our FileStream
            Dim _mailWriter As Object = _mailWriterContructor.Invoke(New Object() {_fileStream})

            ' Get reflection info for Send() method on MailMessage
            Dim _sendMethod As MethodInfo = New MailMessage().GetType().GetMethod("Send", BindingFlags.Instance Or BindingFlags.NonPublic)


            ' Call method passing in MailWriter

            _sendMethod.Invoke(msg, BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, New Object() {_mailWriter, True, True}, Nothing)

            ' Finally get reflection info for Close() method on our MailWriter
            Dim _closeMethod As MethodInfo = _mailWriter.GetType().GetMethod("Close", BindingFlags.Instance Or BindingFlags.NonPublic)

            ' Call close method
            _closeMethod.Invoke(_mailWriter, BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, New Object() {}, Nothing)
        End Using

    End Sub
End Module


You may observe that you need to create extensions as a module.

Sunday, 14 July 2013

Testing SOAP Webservice APIs

Question:

I am working on an integration project where the components that we build need to consume a set of Webservice APIs. While the vendor has shared the documentation and test accounts for us, I am curious if there would be a simple tool which help me to browse through the various methods and test the same on the fly. I know that we can quickly build a test driver using the proxy class for the given wsdl. However, a generic tool might be of great use as it saves time and it can be used for different services as well.

By: Kathiravan Venkatesan

Response:

As you have acknowledged, it is quite easy to quickly build a proxy component and then write simple methods around the exposed web methods to test the webservice APIs. And yes there are tools to test SOAP web services as well that can be used to test a multitude of service APIs. One such tool that I have used in the past is SoapUI by SmartBear, which has a professional and a free open source edition. The free version is good enough to browse through the various web methods and test them as well.

I would suggest you to explore the features of SoapUI to know more about the tool's capabilities. The usage is simple and straight forward. Once you create a new project the tool helps build a tree of web methods based on the wsdl of the service. You can create as many test data against each services and execute instantly to see the output as well Moreover, the test data can be dynamic from a wide range of external data sources. Thus this tool can be very handy for performing the functional testing of webservices.

In addition, the tool can be used to perform load testing and security testing as well. The tool can also supports simulation, i.e. mock the services, which will be very useful during the development stage. There is more to it and I would suggest you to explore, though some of the features are part of the pro version.

Friday, 10 May 2013

JMeter for Scalability Testing


Question:

In on of your own blog post, you have indicated the use of JMeter for Stress Testing a Game Application. I have a web application to be tested for its scalability. Do you recommend using JMeter for simulating load? You may also share some thoughts on its ease of setting up and usage and generally about Scalability testing. You may also recommend using any other tools together with JMeter.

Question By: Natarajan Ganesh

Response:

Scalability testing is about testing how the application scales in or out under different load conditions, and yes we need tools to simulate the load. It is about specifically testing the deployment model of the application as to how it makes use of additional computing resources when the load increases. Needless to mention, it involves testing both scale up and scale down. The actual test cases for scalability testing depend on the application design and architecture as to how it is expected to scale.

For example, in case of a cluster of application servers used, the test case should be to verify that the load on all the servers in the cluster are evenly distributed or as designed and that no server in the cluster is idles out when one or more other servers are peaking beyond a threshold. Another related test case could be to examine the application behavior when one of the server in the cluster is pulled out.

Thus scalability testing involves in addition to simulating required load or stress on the application, examining whether the scale up or down happens as designed or architected.

Yes, JMeter is a good tool for simulating the necessary load or stress on the application. Setting up JMeter and using it is easy and intuitive. The following simple steps would help you to get JMeter up and running the tests on a Windows box:


  1. Make sure that you have JRE or JDK installed the bin folder is included in the system path.
  2. Navigate to the bin folder of the JMeter installation folder and run the JMeter.bat. Just in case if it errors out, view or edit the JMeter.bat file to see if there is any other settings that need to be modified.
  3. Right Click on the Test Plan in the vertical explorer pane on the left and add a Thread Group (Add -> Threads -> Thread Group).
  4. Right Click on the newly added thread group and add a Sampler -> HTTP Request. In the resulting window make sure that you fill out appropriate vaules. Repeat this step to add as many HTTP Requests to be sampled.
  5. Use an appropriate Listener to gather the data. This can be done again by adding a Listener to the Thread Group.


You could observe from the Add menu that you have options to add configuration elements, pre and post processors, assertions and logic controllers. Depending on your test scenario, use a combination of these components an build your test plan accordingly. Consult the JMeter documentation to to know more about effectively using JMeter.

While JMeter is just a tool simulate the stress or load, you need tools to gather server perormance data and as well as the application performance. While the native performance monitoring tools on Windows could b very useful, for Linux, you can try out NewRelic, a hosted remote monitoring tool. As I have described in one of my blog post, the application should have been designed to generate necessary data for further review and examination to spot probable or potential bottlenecks that need to be further tweaked.

Good luck with your testing.

Sunday, 14 April 2013

How to Integrate a POS device in a .NET Windows Forms Application

Question:

One of our recent project requires integrating our Windows Forms application with point of sale devices like barcode scanner, check reader etc. I could find out that there is a POS.NET library that can be used for the purpose, but it seems to have been built for .NET 1.12 and no updates after that. Can you suggest me if there is any other .NET libraries that can be used for the purpose. You may also help me by sharing your thoughts and ideas.

Question by Selvamurugan N.

Answer:

Answering your first part of the question - Why there are n't any updates to this library? - I can only guess the answer for this and that I feel is due to the fact the device manufacturers lag behind in updating the communication protocols, which is more so because typically these devices are built for a low cost specific purpose operating environments. For instance, the POS terminals don't have to be on a Windows 7, it would just be enough for them to be on Windows XP or even older operating system. That probably is the reason, why the Library continues to be based on COM components and does not need an update.

On using the POS.NET library, yes, you can still use it in the later frameworks of .NET, but be aware of some of the constraints or issues as described in this blog post. The blog author calls out issues with 64 bit environment and also with .NET Framework 4.0. Don't miss out the comments, as there are some valid opinions and observations in the form of comments. You may also want to check out the Microsoft's publication on using the POS.NET titled as Creating a Proof of Concept POS Application.

If that does not suit you, then go ahead and learn to access and work with USB devices using HID (Human Interface Device), but you need to know the communication protocols and the data structures as specified by the device manufacturer. This will for sure will give you a better control over your integration, but you would be re-inventing the wheel. Here is a nice introduction to working with USB devices in .NET.

My recommendation is that check out if the device vendor provides you a .NET or a COM Library and if yes, using that will be the best option as some device manufacturers may have built certain proprietary protocols, which would work best with their own drivers. If that is not the case, then evaluate POS.NET whether it fits your use cases. Most of the times it should be fine, unless you meet up with some of the constraints when using with later versions of .NET Framework. If left with no choice, use HID interface to have your requirements met. You may also want to check out this msdn article on using Alternate Input Devices.