19 Januar 2016

How to Send Mails in ASP .NET 5 on Azure

Unfortunately neither SendGrid nor System.Net.Mail is supported in the actual version of ASP .NET 5 (RC).

So how to send mails then?
One solution is to use MailKit https://github.com/jstedfast/MailKit

To use MailKit just add "MailKit": "1.3.0-beta5" to your project.json

What you need is an SMTP server. On Azure you can create a SendGrid account and use the host name and the credentials provided in the Configurations section. SendGrid on Azure is free for up to 25'000 mails per month!

The code to send a mail is straight forward.

private void SendEmail()
{
  var message = new MimeMessage();
  message.From.Add(new MailboxAddress("Peter Muster", "peter@muster.ch"));
  message.To.Add(new MailboxAddress("Petra Muster", "petra@muster.ch"));
  message.Subject = "Some Subject";

  message.Body = new TextPart("plain")
  {
    Text = string.Format(@"Hello,

Just a little message for you"
  };

  using (var client = new SmtpClient())
  {
    client.Connect("smtp.sendgrid.net", 587, false);
    client.AuthenticationMechanisms.Remove("XOAUTH2");
    client.Authenticate("your_id@azure.com", "password");

    client.Send(message);
    client.Disconnect(true);
  }
}


18 Januar 2016

Logging in ASP .NET 5 on Azure

Currently ASP .NET 5 is not able to log on Azure.

Fortunately there is a workaround described here https://github.com/aspnet/Home/issues/773

The trick is to use Serilog an open source logging framework for the .NET platform.

12 Januar 2016

ASP .NET 5 Continuous Deployment from Visual Studio Online to Azure

ASP .NET 5 uses DNX and the standard build in Visual Studio Code does not work.

You have to create your on build as describe in this article:
https://msdn.microsoft.com/Library/vs/alm/Build/azure/deploy-aspnet5

But in the tutorial there is missing one important step:
In the Visual Studio Build Step you have to uncheck "Restore NuGet Packages"

Without this you will get

##[error]Error parsing solution file at C:\a\1\s\src\simashr\simashr.xproj: Exception has been thrown by the target of an invocation.
##[error]Unexpected exit code 1 returned from tool NuGet.exe

21 August 2015

Rules for Programmers and Does the Developers Age Matter

I found two very interesting articles.

The first one (in German) discusses if the age of a developer matters and what the pros and cons are of young or old. https://jaxenter.de/aeltere-pogrammierer-jugend-versus-erfahrung-16464

In the second you will find 11 rules for better code. I completely agree on all 11 rules!
http://simpleprogrammer.com/2015/03/16/11-rules-all-programmers-should-live-by/

11 Dezember 2014

Which parts of Java EE belong to the application server?

Yesterday I was at the Developer Day of Swiss Railways.

Florian Seidl talked about future enterprise software architecture where he doubt that the application still plays a central role in Java EE.

A lot of people argue that it is a pain to relay on a out dated full blown Java EE application server and therefore use a Spring/Tomcat stack or simply use Spring on top of the application server.

It seems that there is a problem in how Java EE is defined. In my opinion it would make sense to divide all the Java EE specs in two parts.

One part should contain all the specs that are directly related to the application server and on the other side there should be the specs where the application code is built on top.

App server related topics are web containter, threading, JTA, JCA, JAAS, JMS etc.
Application related are i.e. JPA, CDI, EJB, JSF.

The idea behind the separation is that one could update the application related implementations without changing the application server version.

For example I don't see any reason for not using JPA 2.1 in a Java EE 5 application server.

Sure these lists are just a first guess and incomplete but I would be interested what you think.

06 Dezember 2014

Contract First Web Service with NetBeans

From time to time I have to implement some SOAP based web services. Usually these web services are used for application integration so the contract first approach is used where one creates the WSDL  document first.

To create the server stub from the WSDL document I found out that using Maven in combination with NetBeans is the most straight forward approach.

Just choose "New File" from the menu:



After clicking on Next you need to enter the name of the service to create and provide the WSDL document:


Now just hit finish and have a look at what the wizard has generated.

First the web service server stub:

package ch.simas.demo.ws;

import javax.ejb.Stateless;
import javax.jws.WebService;

@WebService(serviceName = "CalculatorService", portName = "ICalculator", endpointInterface = "org.example.ICalculator", targetNamespace = "http://Example.org", wsdlLocation = "META-INF/wsdl/calculator.wsdl")
@Stateless
public class CalculatorService {

    public java.lang.Integer add(java.lang.Integer a, java.lang.Integer b) {
        //TODO implement this method
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    public java.lang.Integer subtract(java.lang.Integer a, java.lang.Integer b) {
        //TODO implement this method
        throw new UnsupportedOperationException("Not implemented yet.");
    }
    
}

That's not very impressive but let's have a look at the pom.xml

<plugin>
  <groupId>org.jvnet.jax-ws-commons</groupId>
  <artifactId>jaxws-maven-plugin</artifactId>
  <version>2.3</version>
  <executions>
    <execution>
      <goals>
        <goal>wsimport</goal>
      </goals>
      <configuration>
        <wsdlFiles>
          <wsdlFile>calculator.wsdl</wsdlFile>
        </wsdlFiles>
 <staleFile>${project.build.directory}/jaxws/stale/calculator.stale</staleFile>
      </configuration>
      <id>wsimport-generate-calculator</id>
      <phase>generate-sources</phase>
    </execution>
  </executions>
  <dependencies>
    <dependency>
      <groupId>javax.xml</groupId>
      <artifactId>webservices-api</artifactId>
      <version>2.0</version>
    </dependency>
  </dependencies>
  <configuration>                          <sourceDestDir>${project.build.directory}/generated-sources/jaxws-wsimport</sourceDestDir>
    <xnocompile>true</xnocompile>
    <verbose>true</verbose>
    <extension>true</extension>
    <catalog>${basedir}/src/jax-ws-catalog.xml</catalog>
    <target>2.0</target>
  </configuration>
</plugin>

As you can see all the necessary configuration for the jaxws-maven-plugin is done!

In my opinion NetBeans has far the best integration of Maven and you don't have to configure both your IDE and Maven because NetBeans relays on the configuration in the pom.xml.

28 Juli 2014

Include XML Schema Reference in XML to get IDE Code Completion

One thing that I find very often is the missing XML schema reference in XML files.
The advantage of including the reference is code completion in your IDE because with the xml schema the IDE knows about the structure of the XML document.

Example:
<?xml version="1.0" encoding="UTF-8"?>
<batch:job
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns:batch='http://xmlns.jcp.org/xml/ns/javaee'
    xsi:schemaLocation='http://xmlns.jcp.org/xml/ns/javaee http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/jobXML_1_0.xsd'>

    <batch:step id="step1">
        <batch:chunk>
            <batch:reader ref="Reader" />
            <batch:processor ref="Processor" />
            <batch:writer ref="Writer" />
        </batch:chunk>
    </batch:step>
</batch:job>



An even worse pratice is using XML without a schema. But more about that in another post...