Jan 30, 2009

Expression evaluation, thinking again

I recently discussed .NET runtime expression evaluation. Integrating some sample code into your productive applications may have some smells; so this may not be your prefered way to solve simple business logic expression evaluation issues.

Reading a blog from Brad Abrams about Avoiding Custom Delegates explaining

Expression<…> represents function definitions that can be compiled and subsequently invoked at runtime but can also be serialized and passed to remote processes. Continuing with our example:

            Expression<Func<int, int, double>> expression = (x, y) => (double)x / (double)y;

            Func<int, int, double> divide = expression.Compile();

            Console.WriteLine(divide(2, 3));

Microsoft .NET Services (Azure .NET Service Bus) samples and relay bindings

After some initial issues getting a Microsoft .NET Services invitation code and creating a solution on partially non-working servers I finally wanted to run some samples from the SDK.

Being behind a NAT/firewall infrastructure (no open custom ports other than 80/443) of our organisation (yes, indeed the net.tcp samples all failed starting the service) I tried to get wsHttpRelayBinding sample working.

The service is quite simple:


        static void Main(string[] args)


        {


            string serviceBusSolutionName = GetServiceBusSolutionName();


            Uri address = new Uri(String.Format("http://{0}/services/{1}/EchoService/", ServiceBusEnvironment.DefaultRelayHostName, serviceBusSolutionName));


 


            ServiceHost host = new ServiceHost(typeof(EchoService), address);


            host.Open();


 


            Console.WriteLine("Service address: " + address);


            Console.WriteLine("Press [Enter] to exit");


            Console.ReadLine();


 


            host.Close();


        }




with the following username/password configuration:


<?xml version="1.0" encoding="utf-8" ?>


<configuration>


  <system.serviceModel>


    <behaviors>


      <endpointBehaviors>


        <behavior name="UserNamePasswordCredentials">


          <transportClientEndpointBehavior credentialType="UserNamePassword">


            <clientCredentials>


              <userNamePassword userName="solution" password="password" /> 


            </clientCredentials>


          </transportClientEndpointBehavior>


        </behavior>


      </endpointBehaviors>


    </behaviors>


 


    <bindings>


      <!-- Application Binding -->


      <wsHttpRelayBinding>


        <binding name="default">


          <security mode="None"/>


        </binding>


      </wsHttpRelayBinding>


    </bindings>


 


    <services>


      <!-- Application Service -->


      <service name="Microsoft.ServiceBus.Samples.EchoService">


        <endpoint name="RelayEndpoint"


                  contract="Microsoft.ServiceBus.Samples.IEchoContract"


                  binding="wsHttpRelayBinding"


                  bindingConfiguration="default"


                  behaviorConfiguration="UserNamePasswordCredentials"


                  address="" />


      </service>


    </services>


 


  </system.serviceModel>


</configuration>




Checking the code looks like HTTP communication to me, that should pass our security perimeter infrastructure. Getting the following exception mentioning about net.tcp gives me some thinking.

System.ServiceModel.EndpointNotFoundException was unhandled
Message="Could not connect to net.tcp://servicebus.windows.net:828/services/solution/UserNameAuthenticationService/. The connection attempt lasted for a time span of 00:00:21.0021000. TCP error code 10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 65.55.54.16:828. "
Source="Microsoft.ServiceBus"
StackTrace:
at Microsoft.ServiceBus.RelayedOnewayTcpClient.Connect()
at Microsoft.ServiceBus.RelayedOnewayTcpClient.EnsureChannel()
at Microsoft.ServiceBus.RelayedOnewayTcpClient.OnOpen(TimeSpan timeout)
at Microsoft.ServiceBus.Channels.CommunicationObject.Open(TimeSpan timeout)
at Microsoft.ServiceBus.Channels.CommunicationObject.Open()
at Microsoft.ServiceBus.RelayedOnewayTcpListener.OnOpen(TimeSpan timeout)
...

Using TCP port 828...

Is there some probing going on, even when specifying a HTTP binding? Trying again from home.

Jan 23, 2009

C# runtime expression evaluation

I recently found out about a XAML declarative value converter for WPF which is based on a Visual Studio Linq lambda expression sample called DynamicQuery (Dynamic.cs is bundled with VS2008 as part of \Samples\1033\CSharpSamples.zip).



For a rule evaluation (engine) investigation I created a simple application that accepts a list of expression parameters and calculates the result of a formula:



Takes a couple of [ms] the first time but performs great after the initial hit.

Jan 19, 2009

WebBrowser in WPF


Sketching a very simple RSS reader for a demo today, I just faced the question on how to display HTML in the Windows Forms WebBrowser control in a WPF application.

You just need to wrap it into a WindowsFormsHost element:




<DockPanel Grid.Column="1" Margin="10" TextBlock.FontFamily="Verdana" >


    <TextBlock FontSize="16" FontWeight="Bold" x:Name="RssTitle" DockPanel.Dock="Top"/>


    <WindowsFormsHost>


        <wf:WebBrowser x:Name="RssBrowser" />


    </WindowsFormsHost>


</DockPanel>




Setting the namespace for wf to

xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"




and then call

this.RssBrowser.DocumentText = syndicationItem.Summary.Text;



from code. Does anybode know how to set the font family, by the way?