Skip to content

Interfaces und abstrakte Klassen als Parameter – Teil 1

Anfang der Woche hat mich ein Arbeitskollege danach gefragt, ob es möglich ist Interfaces oder abstrakte Klassen als Parameter an (WCF-)Service-Methoden zu übergeben. Zunächst war mir nicht ganz klar, was er eigentlich vor hatte, dabei liegt es klar auf der Hand. Die Frage war, ob man das Prinzip des Polymorphismus der Objektorientierung auch auf WCF-Services anwenden kann.

Meine erste Antwort: Ja, klar!

Nicht ganz so klar, wie sich später herausstellte …
Continued…

Categories: Programming, Web.

Tags: , , , ,

“DRIVE XV: A Tribute To Automatic For The People” hurts

Gestern abend laß ich im Visions Weekly, dass Stereogum “DRIVE XV: A Tribute To Automatic For The People” veröffentlicht haben. Ich musste nicht zweimal überlegen und habe mir das Album sofort runtergeladen. Das Original steht schließlich nicht umsonst in meinem Plattenschrank – ein Meisterwerk. Also schnell alle MP3s runtergeladen und auf meinen iPod gespielt, Kopfhörer auf und … AHHHHHHHHHH … Schmerz … Ohrenkrebs!

Ich musste mich wirklich selbst zwingen auch nur einen Song komplett zu hören. Eine absolute Tortur.
Einige Songs sind kaum wiedererkennbar. Bei anderen habe ich das Gefühl, es stand bei der Aufnahme eine jaulende Katze vor’m Mikrophone. Dieses Tribute hat mir echt den gestrigen Abend verdorben.

Hört selbst (oder besser nicht!):

you should see the stereogum.com drive xv player here if you have flash

Categories: Music.

Tags: ,

Using xs:choice in WCF-Services

At the very beginning of the ongoing work of my "Projektgruppe" we had to convert an existing XSD into a DataContract compliant schema. The XSD was part of the WSDL of one of our services which we developed using a contract-first approach. One complex type contained a xs:choice element which is not supported by DataContracts (see Data Contract Schema Reference). Therefore we decided to change our XSD. Retrospectively, maybe this was not the best decision. We discussed the usage of polymorphism with the help of xsd extension but didn't use it at all.

Nevertheless, the good old XmlSerializer supports xs:choice! Here's a very simple example:

C#:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.Text;
  7. using System.Xml.Serialization;
  8. using System.Xml;
  9. using System.Xml.Schema;
  10.  
  11. namespace Choice
  12. {
  13.     [ServiceContract(Name="ChoiceService", Namespace="http://dev.janus-net.de/example/choice")]
  14.     [XmlSerializerFormat(Style = OperationFormatStyle.Document)]
  15.     public interface IChoiceService
  16.     {
  17.         [OperationContract()]
  18.         void DoSomething(XSDChoice choice);
  19.     }
  20.  
  21.     [ServiceBehavior(Name="ChoiceService", Namespace="http://dev.janus-net.de/example/choice")]
  22.     public class ChoiceService : IChoiceService
  23.     {
  24.         public void DoSomething(XSDChoice choice)
  25.         {
  26.             System.Console.WriteLine(choice.ToString());
  27.         }
  28.     }
  29.  
  30.     [Serializable]
  31.     public class XSDChoice
  32.     {
  33.  
  34.         [XmlChoiceIdentifier("ChoiceType")]
  35.         [XmlElement("Choice1", typeof(string), IsNullable = false)]
  36.         [XmlElement("Choice2", typeof(double), IsNullable = false)]
  37.         public object MyChoice;
  38.  
  39.         [XmlIgnore]
  40.         [XmlElement(IsNullable=false)]
  41.         public ItemChoiceType ChoiceType;
  42.  
  43.         public override string  ToString()
  44.         {
  45.             string info = null != MyChoice ?
  46.                 (":Type<" + MyChoice.GetType() + ">,Value<" + MyChoice.ToString() + ">")
  47.                 : (":Type<object>,Value<null>");
  48.             if (ChoiceType == ItemChoiceType.Choice1)
  49.             {
  50.                 return "Choice1:" + info;
  51.             }
  52.             else if (ChoiceType == ItemChoiceType.Choice2)
  53.             {
  54.                 return "Choice2:" + info;
  55.             }
  56.             else
  57.             {
  58.                 return info;
  59.             }
  60.         }
  61.     }
  62.  
  63.     [XmlType(IncludeInSchema = false)]
  64.     public enum ItemChoiceType
  65.     {
  66.         None,
  67.         Choice1,
  68.         Choice2
  69.     }
  70. }

This will result in the following XSD:

XML:
  1. <xs:complexType name="XSDChoice">
  2.     <xs:sequence>
  3.         <xs:choice minOccurs="1" maxOccurs="1">
  4.             <xs:element minOccurs="1" maxOccurs="1" name="Choice2" type="xs:double"/>
  5.             <xs:element minOccurs="0" maxOccurs="1" name="Choice1" type="xs:string"/>
  6.         </xs:choice>
  7.     </xs:sequence>
  8. </xs:complexType>

You can even do much more complex things using the XmlSerializer in conjunction with Service Contracts. Designing Service Contracts with WCF by Michele Leroux Bustamante gives a very very good introduction.

Categories: Programming, Web.

Tags: , , , , , , , , ,

FatalExecutionEngineError on calling HttpQueryServiceConfiguration

The last few days I struggled a lot with WCF endpoints while configuring them running with least privileges. As already mentioned in my post WCF: Namespace Reservierung there are several tools simplifying this procedure. Nevertheless I wanted to write my own little tool with a GUI. The tool works fine so far (go and grep a copy from here) but there's something really annoying.

It's this error I get when calling HttpQueryServiceConfiguration:

FatalExecutionEngineError was detected
Message: The runtime has encountered a fatal error. The address of the error was at "0x79e8a634", on thread "0x1be8". The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or on-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

When executing httpcfg query urlacl I get these two reservations:

URL : http://*:2869/
ACL : D:(A;;GX;;;LS)

URL : http://+:80/Temporary_Listen_Addresses/
ACL : D:(A;;GX;;;WD)

On executing my code (see below) everything is ok. I get exactly the same results as above. However, after adding a third reservation with httpcfg set urlacl ... I get this annoying error.

At this point I should mention that I'm not very familiar with pinvoke and interop, but I want this to work. So, what am I missing or doing wrong? Any answers or suggestions will be appreciated.

Continued...

Categories: Programming.

Tags: , , , , ,

Warum muss eine abstrakte Klasse in C# alle Methoden einer Schnittstelle implementieren?

Ein Kommilitone von mir, der sonst hauptsächlich im Java-Umfeld programmiert, stellte mir heute die Frage, warum er folgendes in C# nicht machen kann:

C#:
  1. interface IInterface
  2.     {
  3.         void Method();
  4.     }
  5.  
  6.     abstract class Base : IInterface
  7.     {
  8.     }

Folgendes funktioniert nämlich in Java problemlos:
Continued...

Categories: Programming.

Tags: , ,