in

Community Blogs

Blogs of different SQL/Developers Community Members

This Blog

Syndication

DamirDobric

lipanj 2009 - Posts

  • Message Addressing in WCF

    This post contains few examples of the same request of one service by using of different types of addressing. For this reason I configured a custom binding
    with one encoding element and one http transport element. For different tests I used different addressing methods.

    Here is the sample code:

    CustomBinding custBinding = new CustomBinding();

    TextMessageEncodingBindingElement txtElem = new
    TextMessageEncodingBindingElement();

    // Here I used all supported addressing
    txtElem.MessageVersion = MessageVersion.Soap11WSAddressing10;
               

    custBinding.Elements.Add(txtElem);

     

    HttpTransportBindingElement basicHttpBinding =
    new HttpTransportBindingElement();

    custBinding.Elements.Add(basicHttpBinding);


    And, here is the result:

    Soap11

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

      <s:Header>

        <ActivityId CorrelationId="045e4ff1-c907-4d64-84af-fef25052683e" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">d003468d-7fad-40bd-9ffd-2a99862c9b41</ActivityId>

      </s:Header>

      <s:Body>

        <QueryMessage xmlns="cevapproduction">

          <CevapQuery xmlns:a="http://schemas.datacontract.org/2004/07/Server" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

            <a:Cijena>0</a:Cijena>

            <a:KajmakYesNo>true</a:KajmakYesNo>

          </CevapQuery>

        </QueryMessage>

      </s:Body>

    </s:Envelope>

     

     

    Soap11WSAddressing10

     

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://www.w3.org/2005/08/addressing">

      <s:Header>

        <a:Action s:mustUnderstand="1">cevapproduction/ICevap/ImaLiCevapa</a:Action>

        <a:MessageID>urn:uuid:f2d9e6c0-80a2-44f0-a2eb-6e5aa1808712</a:MessageID>

        <ActivityId CorrelationId="95641fb0-b3f1-4e26-8038-947a06aee055" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">da1ff29d-6235-4c1b-85c8-daebf827a6fd</ActivityId>

        <a:ReplyTo>

          <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>

        </a:ReplyTo>

        <a:To s:mustUnderstand="1">http://dado-nb0:8001/cevapservice/encodingsample1</a:To>

      </s:Header>

      <s:Body>

        <QueryMessage xmlns="cevapproduction">

          <CevapQuery xmlns:b="http://schemas.datacontract.org/2004/07/Server" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

            <b:Cijena>0</b:Cijena>

            <b:KajmakYesNo>true</b:KajmakYesNo>

          </CevapQuery>

        </QueryMessage>

      </s:Body>

    </s:Envelope>

     

    Soap11WSAddressingAugust2004

     

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing">

      <s:Header>

        <a:Action s:mustUnderstand="1">cevapproduction/ICevap/ImaLiCevapa</a:Action>

        <a:MessageID>urn:uuid:b047d205-2a7c-4b35-8c52-bac97960292e</a:MessageID>

        <ActivityId CorrelationId="c80c7ba7-fd39-436c-98a8-3f32f6860ba8" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">3e5025d3-afad-497b-a992-aa49dabf0be7</ActivityId>

        <a:ReplyTo>

          <a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>

        </a:ReplyTo>

        <a:To s:mustUnderstand="1">http://dado-nb0:8001/cevapservice/encodingsample1</a:To>

      </s:Header>

      <s:Body>

        <QueryMessage xmlns="cevapproduction">

          <CevapQuery xmlns:b="http://schemas.datacontract.org/2004/07/Server" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

            <b:Cijena>0</b:Cijena>

            <b:KajmakYesNo>true</b:KajmakYesNo>

          </CevapQuery>

        </QueryMessage>

      </s:Body>

    </s:Envelope>

     

    Soap12

     

    <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">

      <s:Header>

        <ActivityId CorrelationId="0283f490-30e1-4759-8d26-1c4a44a14765" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">8358b1d2-b95c-4bbc-9c65-c9f2b4a29366</ActivityId>

      </s:Header>

      <s:Body>

        <QueryMessage xmlns="cevapproduction">

          <CevapQuery xmlns:a="http://schemas.datacontract.org/2004/07/Server" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

            <a:Cijena>0</a:Cijena>

            <a:KajmakYesNo>true</a:KajmakYesNo>

          </CevapQuery>

        </QueryMessage>

      </s:Body>

    </s:Envelope>

     

    Soap12WSAddressing10

     

    <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">

      <s:Header>

        <a:Action s:mustUnderstand="1">cevapproduction/ICevap/ImaLiCevapa</a:Action>

        <a:MessageID>urn:uuid:7ab8eabf-cd15-4b6c-8c23-180243cf3ccd</a:MessageID>

        <ActivityId CorrelationId="1c0e317c-52e9-4855-beff-1f27081d2306" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">99b55e02-3396-4f9e-911f-4025f3119468</ActivityId>

        <a:ReplyTo>

          <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>

        </a:ReplyTo>

        <a:To s:mustUnderstand="1">http://dado-nb0:8001/cevapservice/encodingsample1</a:To>

      </s:Header>

      <s:Body>

        <QueryMessage xmlns="cevapproduction">

          <CevapQuery xmlns:b="http://schemas.datacontract.org/2004/07/Server" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

            <b:Cijena>0</b:Cijena>

            <b:KajmakYesNo>true</b:KajmakYesNo>

          </CevapQuery>

        </QueryMessage>

      </s:Body>

    </s:Envelope>

     

    Soap12WSAddressingAugust2004

     

    <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing">

      <s:Header>

        <a:Action s:mustUnderstand="1">cevapproduction/ICevap/ImaLiCevapa</a:Action>

        <a:MessageID>urn:uuid:23bda306-55e5-48c3-9244-9f1a6616bc27</a:MessageID>

        <ActivityId CorrelationId="b6d711dc-2368-4630-bbc6-8e67f1531de6" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">b92455ec-d044-4a71-b893-87aa039354b6</ActivityId>

        <a:ReplyTo>

          <a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>

        </a:ReplyTo>

        <a:To s:mustUnderstand="1">http://dado-nb0:8001/cevapservice/encodingsample1</a:To>

      </s:Header>

      <s:Body>

        <QueryMessage xmlns="cevapproduction">

          <CevapQuery xmlns:b="http://schemas.datacontract.org/2004/07/Server" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

            <b:Cijena>0</b:Cijena>

            <b:KajmakYesNo>true</b:KajmakYesNo>

          </CevapQuery>

        </QueryMessage>

      </s:Body>

    </s:Envelope>

     

    Posted lip 28 2009, 11:44 by anonymous
    Filed under:
  • What is AXUM?

    Axum is an incubation project from Microsoft’s Parallel Computing Platform that aims to validate a safe and productive parallel programming model for the .NET framework. It’s a language that builds upon the architecture of the web and the principles of isolation, actors, and message-passing to increase application safety, responsiveness, scalability and developer productivity. Other advanced concepts we are exploring are data flow networks, asynchronous methods, and type annotations for taming side-effects. Axum Lite: Contains the Axum command-line compiler and Axum runtime as well as the sample projects. Programmer's Guide: Use this simple and easy to follow programmer's guide to learn how to create safe, scalable, and responsive applications with the Axum language. Language Specification: A detailed specification of the Axum language.

    http://www.microsoft.com/downloads/details.aspx?FamilyID=CFE70D5D-37AA-4C4C-8EEB-D4576C41BAA2&displaylang=en#QuickInfoContainer

  • Issues in Custom Message Encoder

    When implementing custom message encoder there are few hidden things which are different than in an implementation of the custom channel.
    First issue you could have is for example in implementation of BuildChannelListener in the class which derives from MessageEncodingBindingElement.
    Usually this method either is not implemented or it creates the inner channel:

    public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
    {
     
    TChannel innerChannel = base.BuildChannelListener<TChannel>(context);
      return new MyChannel(innerChannel);

    }

    This works perfectly as long you are working with usual bindings or transport elements. Unfortinatelly, one MessageEncoder is a special type of binding with special status on the stack. In a case of encoder, yoi do not have to put it in the channel stack. Instead, register the encoder by adding it to binding parameter list and WCF will do the rest:

    public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
    {
     
    context.BindingParameters.Add(this);
     
    return base.BuildChannelListener<TChannel>(context);
    }

    Second problem you can get is following error:

    None of the binding elements in binding 'CustomBinding' define a message version. At least one binding element must define a message version and return it from the GetProperty<MessageVersion> method.

     

    To solve it, implement a property GetProperty<T> in the class which derives from MessageEncodingBindingElement.
    Here is exact code which show how to do this. Please also note the cast (T)(object).

    public override T GetProperty<T>(BindingContext context)
    {
     
    if (typeof(T) == typeof(MessageVersion))
     
    {
       
    return (T)(object)m_MessageVersion;
     
    }
     
    else
     
    {
        
    return base.GetProperty<T>(context);
     
    }
    }

  • Workflow 4.0 Persistence

    Workflow 4.0 offers a powerful persistence features, which are at this moment not in detail documented.  For this reason I build a short sample, which demonstrate persistence of the workflow driven by workflow host. The workflow example shown in this post is implemented as fully declarative (all in XAML) workflow. Persistence of the workflow is explicitly requested by using of Persist activity.
    Next picture shows the workflow used in this test and full XAML can be downloaded here.

    image 

    The workflow receives one integer as argument. The entered integer value is decremented in the while-loop, which contains a Delay activity and Persist-activity.
    Without of Persist activity, workflow would be started, the entered value would be decremented in the loop and so on to the end of workflow.
    Assuming that the loop in the workflow instead of delay activity would implement some long running business, the probability the things could become more complicated. Because the business id by definition important, the probability of an error increase with the increasing of time of running of the workflow. The longer running time of the workflow the higher error probability.

    Now the question is, “what will happen if the machine running the workflow is switched off somewhere in the middle of execution”? The workflow and business results in the loop would be lost. To prevent losing of data the Persistence feature can be used as shown in this example.
    When Persist-activity is invoked the workflow will be safely persisted. Depending on persistence provider The safety of business data depends directly on persistence provider. At this moment WF4.0 provides out of the box SqlPersistenceProviderFactory able to create PersistanceProvider  which safely persist all of data in the Sql Server database.

    This is the theory, which will work. However more interesting is the code in host, which will start and load persisted workflow.
    Here is the full code:

    image

    In the code shown above I do following. First I try to load the instance thinking that it has already been persisted. If it is very first start  of application  this call will file. This is a reason why I wrap this call in exception handler. After that I will create the new instance of workflow of use the existing one which has been loaded by WorkflowInstance.Load(..).

    On very first call you will get following exception:

    InstanceNotFoundException
    ”The requested operation could not complete because instance '45b7d274-1409-41f3-87cf-1924d465fa01' could not be found.”

    I figured out, that this exception is sometimes throw even if the workflow instance is properly persisted. In this case just repeat the call and all should work fine.

    Please note that if the instance is newly created (has not been persisted yet = first app start) you have to add persistence provider in the list of extensions. Do not do this on instances loaded form the persistence store, because they already have added instance provider in this list.

    After all, I do usual staffs: I subscribe some handlers, run workflow and wait to the end. But take a look in OnCompletedHandler. You will notice there following:

    persProv.Delete(myInstance, TimeSpan.FromSeconds(5));

    This line of code ensures that the completed and persisted instance in the store is removed from the store. Depending on your business requirement you might keep it in the store or delete it. By default completed instances remains in the store. You can try for fun to load a completed instance and start it again. It will all work fine.

    Now, set the breakpoint in the workflow designer at the Persist-activity and start the application. Press F5 few times and take a look on variable CuurentValue. The value will be decrease each time F5 has been pressed. Now just kill the application or stop it. Open the database specified in the connection string in very first line and look for your instance in the table InstanceData. You should notice the instance there.
    Now disconnect from database (detach) and start the application again. This time the exception InstanceNotFoundException will not be thrown (otherwise repeat this step), press F5 and the program will stop again at the Persist-activity. Take a look at the value of variable CurrentValue, and you will see it is at the same value before the simulated crash of application.

    I very hope this helps to make better understanding of workflow persistence.

    Here is the ASCII version of the host code:

    public static void StartWFWithPersistanceActivity() { Guid wfInstId = new Guid("45b7d274-1409-41f3-87cf-1924d465fa71"); m_PersFactory = new SqlPersistenceProviderFactory(@"Data Source=. \SQLEXPRESS;AttachDbFilename=C:\MyProjects \SampleInstanceStore.mdf;Integrated Security=True", false, false, TimeSpan.FromSeconds(60)); m_PersFactory.Open(); SequenceWithPersistActivity wf = new SequenceWithPersistActivity() PersistenceProvider persProv = null; persProv = m_PersFactory.CreateProvider(wfInstId); WorkflowInstance myInstance = null; try { myInstance = WorkflowInstance.Load(wf, persProv); } catch (Exception ex) { } if (myInstance == null) { Dictionary args = new Dictionary() args.Add("InputValue", 10); myInstance = new WorkflowInstance(wf, args, wfInstId); myInstance.Extensions.Add(persProv); } myInstance.OnCompleted = delegate(WorkflowCompletedEventArgs e) { Console.WriteLine("Completed"); persProv.Delete(myInstance, TimeSpan.FromSeconds(5)); syncEvent.Set(); }; myInstance.OnAborted = delegate(WorkflowAbortedEventArgs e) { Console.WriteLine(e.Reason); syncEvent.Set(); }; myInstance.OnUnhandledException = delegate (WorkflowUnhandledExceptionEventArgs e) { Console.WriteLine(e.UnhandledException.ToString()); return UnhandledExceptionAction.Terminate; }; myInstance.Run(); syncEvent.WaitOne(); }

    Posted lip 21 2009, 12:24 by anonymous
    Filed under:
  • Security Restrictions on Web Service Calls

    If you are looking for restrictions on the Web service calls that members of the RFID User group can make from various proxies here is very good description on MSDN.

  • WF4.0: How to use specific DataContract?

    Using of specific custom contracts in WF4.0 BETA1 seem to be for many of us a feature which is not very intuitive.
    Personally, I would  always like to use well known good approach “Contract First”. Workflow Foundation approach silently seems to be some kind of Workflow First approach. This might be cool and interesting approach. Unfortunately, at least at this moment I still prefer “Contract First”, which is currently not possible.
    More about what I would like to have is described here. Few of things are possible, but few not?!
    But, one good thing is that you can at least style your own your own Data Contracts. To do that create a CS-file and define your Data Contracts there.

    image

    Then add variable of that type, for example CandidateResume. As a Value of Receive-Activity add that variable in Expression-Box.
    image

    Posted lip 13 2009, 10:57 by anonymous
    Filed under:
  • Be aware of “extendedProtectionPolicy” on Windows 7 BETA 1

    When working with web services based on WCF you may get following or similar error after importing of references:

    {System.Xml.XmlException: 'Element' is an invalid XmlNodeType. Line 8, position 26.
       at System.Xml.XmlReader.ReadEndElement()
       at System.Configuration.ConfigurationElement.ReadEmptyElement(XmlReader reader)
       at System.ServiceModel.Configuration.BasicHttpSecurityElement.ReadXml(XmlReader reader)
       at System.ServiceModel.Configuration.BasicHttpBindingElement.ReadXml(XmlReader reader)
       at System.ServiceModel.Configuration.BasicHttpBinding

    The problem is caused by element

    <transport>
         <extendedProtectionPolicy policyEnforcement="Never" />
    </transport>

    At this point of time it is sure that this happen on Windows7 BETA one and there is no an official workaround. To make it working remove this attribute after importing of references.

    More about this attribute you can find here.

  • Windows Vista Service Pack 2 and Windows Server 2008 Service Pack 2 Available for Download

    For all of you who still have to fight with VISTA here is a temporary solution which could make your life better.

    The Service Pack 2 for VISTA and, the latest service pack for both Windows Server 2008, is now available for public download. SP2 supports new types of hardware and emerging hardware standards, includes all of the updates that have been delivered since SP1, and simplifies deployment for consumers, developers, and IT professionals. For more details, see the TechNet page for Service Pack 2 for Windows Server 2008 and Windows Vista.

    Microsoft also announced that within 30 days of the May 11 release of Windows Server 2008 RC revealed at Tech·Ed, the System Center Virtual Machine Manager 2008 R2 Release Candidate would be available.
    VMM 2008 R2 RC builds on the new platform enhancements in Windows Server 2008 R2 such as Live Migration, Clustered Shared Volumes (CSV), hot addition and removal of storage, network optimization, and remote desktop services (RDS).

  • “OneWay” Operation style mismatch

    When working with service you sometimes may get following error

    The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.”

    The good thing is that this error does not come very often, as long you tooling works fine. However in some advanced interop scenarios you will have build your proxies, contracts etc with different tools or manually. When working this way, the error above can happen more often than you like. In this post I will show one short and advanced case, which briefly describe why happen behind the stage in Service Model.

    Assume there is a provider which implements service based on this contract:

      [ServiceContract]
      public interface IMissmatchedInterface
      {
            [OperationContract(IsOneWay = true)]
           
    void HelloWorld(string msg);
      }
     

    It is obvious that that the service will listen on oneway messages in the operation HelloWorld. Usually, one would create a proxy for this servic eby using of Visuals Studio “Add Service Reference” functionality and the svcprosvcutil would create semantically the same interface on the client side.

    But imagine now, that for some reason the service has to have IsOneWay property set to FALSE and the client should have the same property set to TRUE. Onb could ask why this should ever be a requirement in universe?  Well, if the underlying channel is for example some queuing system like MSMQ or MQ this might be very important. For example one WCF channel can support reliable messaging like wsHttpBinding. Such binding internally may require that the the message is never of type one way. This is reasonable, because in oneway scenario message is fired and forgotten. That means no reliability and robust messaging is possible.

    Internally WCF will never call back the channel to perform some action after the message has been delivered to operation, if the message is one way. For more information about this take a look here.

    Assume there is client which do not have a requirement to perform reliable messaging, but there is a service which requires reliable messaging. How this can work? It can, it is in big enterprises a possible scenario. For example client can fire some messages and forgot them, but the service is responsible to process all messages which arrived without of loosing of any. It is just about a service level agreement.

    In this case operation style OneWay can be different at the client and service. And exactly this scenario causes the error described above.

    Here is why and how?

    Assume there is a client which has following interface:

      [ServiceContract]
      public interface IMissmatchedInterface
      {
            [OperationContract(IsOneWay = false)]
           
    void HelloWorld(string msg);
      }
     

    The only difference is mismatch in operation style. If the client is set to OneWay=FALSE and Service to OneWay=TRUE, the message above will appear. This is exactly the problem. The client sends message and expects the response, which will never come back. Depending on what kind of binding is used, for example HTTP, the HTTP listener on the service side will always send response message which would be (because OneWay=true at service side) is HTTP message without of SOAP envelope. This message would cause the client to throw the exception shown above.

  • Adding behaviors programmatically

    Operation behavior(s) can be programmatically added either on the service or on the consumer side.

    Service

    When you have an instance of the host, grab out the service description (host.Description), then find the endpoint which hosts the contract. In the list of operations of the contract there is a place where to add operation behaviors. Here is an example:

      host1.Description.EndpointsNo.Contract.Operations[M].Behaviors.Add(behavior);

     

    If you do not have host instance (for example hosting in IIS) then you should implement your custom host and perform the same action as described above.

    Consumer

    If you want to add an operation behavior on the client side the official documentation is a bit confusing. Adding behavior before the factory is created is finde, but there is not description how to do that. One factory is opened by invoking of factory.CreateChannel(). If you ever implemented your own communication object this happens in OnCreateChannelmethod of your factory.

    Anyhow, the behavior has to be added before you call factory.CreateChannel(), and that is after

    factory = new ChannelFactory<IMyExtendedService>(binding, address);

    So, here is the ultimate one-line-code solution:

     factory.Endpoint.Contract.OperationsNo.Behaviors.Add(myBehavior as IOperationBehavior);

     

    For more information see http://msdn.microsoft.com/en-us/library/system.servicemodel.description.ioperationbehavior.aspx.

    Posted lip 03 2009, 12:01 by anonymous
    Filed under:
Copyright of SQL/Developers Community
Powered by Community Server (Commercial Edition), by Telligent Systems