.NET 3.5, BizTalk Server 2009, Development, SOA, WCF

Oh BizTalk, why dost thou mock me?

In any given integration project many different parties are involved. In some cases these parties have standard endpoints against which BizTalk operates and sometimes these endpoints are built as you go in the participating systems to meet a new demand. This is true for other types of projects as well – things get finished at different times – and you need to be able to work independent of each other during that time. What you often do determine very early in the project are the design of messages, schemas or APIs through which you exchange information. In many cases a BizTalk project can effectively use FILE send or receive ports that serve the same purpose as a One-Way send or receive port to say SAP would do at a later stage. But how about solicit-response send ports? What do you exchange these for?


In this post I’d like to introduce a way to handle solicit-response through the use of a catch all WCF Service. There any many ways in which to mock a service. The prime benefit with this approach is that you will be able to model your BizTalk solution the way you want it to be, without having to have access to the real service, and exchange the solicit-response port for it’s production or acceptance test counter parts at your convenience.


To achieve this we need something that let’s us send in any message, and based on some part of the message determine what message to send back, and the send it as a response.


WCF is a perfect candidate. It has functionality both to allow us to create methods that handles all incoming requests by specifying a wildcard (*) as the Action property, and accept any message and can send any message as a return. Using the Message base class it also allows us to easily create the response message, and populate the body of the message from the contents of a stream, such as a file (See the ‘Creating Messages from XmlReaders’ topic in the link).


So what I did was I created a WCF Service, and added a method to catch all messages:

[OperationContract(Action = “*“, ReplyAction=”*“)]
Message CatchAll(Message message);

In the implementation for this method I have include the following code:

FileStream stream = new FileStream(ResponseHelper.GetResponseFile(requestAction), FileMode.Open);
XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas());
MessageVersion ver = OperationContext.Current.IncomingMessageVersion;
return Message.CreateMessage(ver, ResponseHelper.GetResponseAction(requestAction), xdr);

I have a small helper class that just helps me get data from the config, the config in turn looks like this:

<configSections>
  <section name=”requestResponseHandling
type=”ServiceHost.RequestResponseHandlingConfigSection, ServiceHost“/>
</configSections>
<requestResponseHandling>
  <actionList>
    <add requestAction=”http://tempuri.org/IService1/GetData2
responseAction=”http://tempuri.org/IService1/GetData2Response
responseLocation=”GetData2Response.xml” />
  </actionList>
</requestResponseHandling>

This enables me to add new responses to incoming requests without needing to rebuild the service to incorporate a new response. You could go crazy here with code and undertakings to reply based on some context or what not. In the case presented here I’m just making it simple and returning the same static message for all request that matches a certain requestAction.


Finally, put this in the host of your choice. In my case I’ve got IIS so I’m hosting it there. That will also cause changes to the web.config to automatically get loaded, so that’s happy times.


Using this from a client is super easy. Just point the address of the client endpoint towards this service instead. The only thing that might not be super simple (though still fairly simple) is that you need to know what the meat of the response will look like when serialized as a response (the body of the response message). That is you need to Generate a sample response message from your wsdl.


Now let’s look at how we can utilize this to mock services in BizTalk Server. Oh, but wait, that sounds like it would be a bit of work to do, but… no, that isn’t the case. In fact, once you have configured the WCF service the only thing you need to do is to point your Send port at this service instead of the system that would otherwise be there in it’s place. Loop closed.

.NET 3.5, WCF

Contruct ChannelFactory takes too long with config

Recently we experienced an issue with a service where the first call took a long time to complete. Subsequent calls would complete fine and fast, but the first call took a long time. Using Service Trace Viewer told us that the problem lay at constructing ChannelFactory.


image


The detailed trace of this activity didn’t give us any more hints to what was causing the problem.


image


All we could see was that the time appeared to all disappear when WCF tries to get the configuration for service.


The code for this (I’m just using the automatically generated Service1 template to illustrate) was as follows:

proxy = new Service1Client();
Console.WriteLine(proxy.GetData(22));

This is strange indeeded. Since loading the config seemed to be the issue, we changed the code to do this programmatically instead.

EndpointAddress address = new EndpointAddress(“net.tcp://localhost/SimpleService/Service1“);
proxy = ChannelFactory<IService1>.CreateChannel(new NetTcpBinding(), address);

Running this and looking at the trace for this revealed that this was a much more performant way of doing this.


image


For the scenario we had this solution was fine – but it’s not really a solution, it’s a workaround.


I’m still at a loss to describe why this happened on these servers, since the same thing worked quite differently on other servers. There, both solutions performed the same. So this is obviously something connected to some setting or circumstance that differs on those servers when compared to others.


If anyone has insight into this, or suggestions, please share.

.NET 3.5, Download, WCF

How-to: Get status from a windows service?

I was asked: “How can I call into a Windows Service to get it’s status?”


A little more background said that status was to be retrieved by clients (a website) on the same machine.


My response was to use WCF with the NetNamedPipeBinding. Since the recipient of my response, by his own accord, was unaccustomed to WCF, I decided to write up a small app that showed the concept.


The key thing here is that service expose an endpoint at an address that looks like this: net.pipe://localhost/ServiceMonitoringEndpoints/<name_of_service>. Name of service will be the same name as the ServiceName property of the ServiceInstaller component. This enables us to et the status of an arbitrary service just by knowing its name. First we look at the status through ServiceController and then, if the service is running, we call the WCF service to get the actual status, in this case in the form of a string. Pretty simple, wrapped up in a Library component ready to be used by both Windows Service servers and their clients. The Windows Service server is configured through its config file in my sample, but that’s something you can alter should that be one of your requirements.


Basically the server configuration is (mex of course not really necessary):

<services>
  <service behaviorConfiguration=“includeExBehavior”
      name=“SharedMonitoringUtilityLibrary.MonitoringService”>
    <endpoint address=“” binding=“netNamedPipeBinding”
              contract=“SharedMonitoringUtilityLibrary.IMonitoringService” />
    <endpoint address=“mex” binding=“mexNamedPipeBinding”
              contract=“IMetadataExchange” />
  </service>
</services>

and the it uses one of ServiceHosts constructors to supply that takes type and address:

protected override void OnStart(string[] args)
{
    host = new ServiceHost(typeof(SharedMonitoringUtilityLibrary.MonitoringService),
        new Uri(“net.pipe://localhost/ServiceMonitoringEndpoints/MonitorMeWithWCFService_Service1/“));
    host.Open();
    MonitoringService.Status = “Started at ” + DateTime.Now.ToString();
}
protected override void OnStop()
{
    if (host != null)
        host.Close();
}

as you can see from the code above, I’m simply using a static method to supply the status to the running WCF Service. This particular point of interaction wasn’t my primary point of this post or code. A better option could be getting an instance of the host from the other direction using an overloaded ServiceHost and Get the host using OperationContext.InstanceContext.Host.. Another option might even be where the server is a client to it’s own service, using a set method to set the status. What you don’t want to do is make the service hostdependent. Just be aware that my choice above is clearly limiting, and that you have plenty of other options.


The client is dynamically configured to look for and call the service using the following method:

Binding binding = new NetNamedPipeBinding();
EndpointAddress endpoint = new EndpointAddress(
    string.Format(@”net.pipe://localhost/ServiceMonitoringEndpoints/{0}/“, serviceName));
MonitoringServiceClient client = new MonitoringServiceClient(binding, endpoint);
statusInfo = client.GetStatus();

Although it’s surrounded by other things as well in the actual sample code, like the call to ServiceController.


Here is the sample code.


It contains three projects. The Windows Service Server, the library and a simple console client. Compile the sample code, install the service using installutil and run the client. Now that you’ve validated it and it’s running you can begin expanding it.


Additional Resources:
Hosting WCF Service in Windows Service screencast
Hosting and consuming WCF services
NamedPipe activation

.NET 3.5, Learning

Getting up to speed with .NET 3.5

If you haven’t had the opprtunity yet to dive into the .NET Framework 3.5 there is a training kit from Microsoft covering the enhancements to the Framework. There is also this other training kit that covers a larger part of the .NET 3.0/3.5 Framework and Visual Studio 2008. Using Live Search to look for training kits unfortunatly reveals that there aren’t that many training kits around. Too bad. There should be more. It’s a good way to start learning a new technology on your own.


Talking about Live Search, Microsoft is really trying their best to get more people using their Live plattform. The latest offer I’ve come across is searchperks – where you get points for searching the web, which you can then redeem for something that you wan’t. If you are a really busy searcher you can get stuff like xbox controls.

.NET 3.5, Download, LINQ

LINQ Material

The presentation made available for download through this post is in swedish. The code however is “international”. First of I would like to list what I think are two of the best getting started with LINQ resources available online.



With that said, the material provided here is the LINQ presentation and demo code that I used for the LINQ presentation I held.


Presentation: The mechanics of LINQ.pptx (in swedish)
Code: linqdemo.zip