views:

290

answers:

1

Hi, i have developed a wf-wcf services with a code activity and in it i want to retrive the current url of the service. If i disabling the persistence feature of appfabric i can retrive the url using

HttpContext.Current.Request.Url.ToString()

If the persistence feature is enabled the httpcontext is null.

There is a different way to retrive the url of th wcf that host my code activity? Thanks in advace

+2  A: 

You need to implment IReceiveMessageCallback and add that to an activity's context. In the OnReceiveMessage you get passed the current OperationContext allowing you to inspect the incoming message.

This WF4 samples shows how to do this.

Example code:

using System.Activities;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activities;

namespace DeclarativeServiceLibrary2
{
    public sealed class GetWCFMessageTo : NativeActivity
    {
        public Receive Receive { get; set; }

        public OutArgument<string> WcfTo { get; set; }

        protected override void Execute(NativeActivityContext context)
        {
            context.Properties.Add("ReceiveMessageCallback", new ReceiveMessageCallback());
            context.ScheduleActivity(Receive, CompletionCallback);
        }

        private void CompletionCallback(NativeActivityContext context, ActivityInstance completedInstance)
        {
            var receiveMessageCallback = context.Properties.Find("ReceiveMessageCallback") as ReceiveMessageCallback;
            WcfTo.Set(context, receiveMessageCallback.WcfRequestTo);
        }
    }

    [DataContract]
    class ReceiveMessageCallback : IReceiveMessageCallback
    {
        public string WcfRequestTo { get; private set; }

        public void OnReceiveMessage(OperationContext operationContext, ExecutionProperties activityExecutionProperties)
        {
            WcfRequestTo = operationContext.RequestContext.RequestMessage.Headers.To.ToString();
        }
    }
}

and the sample workflow XAMLX

<WorkflowService mc:Ignorable="sap" ConfigurationName="Service1" sap:VirtualizedContainerService.HintSize="307,306" Name="Service1" mva:VisualBasic.Settings="Assembly references and imported namespaces serialized as XML namespaces" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/servicemodel" xmlns:d="clr-namespace:DeclarativeServiceLibrary2;assembly=DeclarativeServiceLibrary2" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="clr-namespace:Microsoft.VisualBasic;assembly=System" xmlns:mva="clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities" xmlns:p="http://tempuri.org/" xmlns:p1="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:s1="clr-namespace:System;assembly=System" xmlns:s2="clr-namespace:System;assembly=System.Xml" xmlns:s3="clr-namespace:System;assembly=System.Core" xmlns:s4="clr-namespace:System;assembly=System.ServiceModel" xmlns:sa="clr-namespace:System.Activities;assembly=System.Activities" xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities" xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=System" xmlns:scg1="clr-namespace:System.Collections.Generic;assembly=System.ServiceModel" xmlns:scg2="clr-namespace:System.Collections.Generic;assembly=System.Core" xmlns:scg3="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:sd="clr-namespace:System.Data;assembly=System.Data" xmlns:sl="clr-namespace:System.Linq;assembly=System.Core" xmlns:st="clr-namespace:System.Text;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  <p1:Sequence DisplayName="Sequential Service" sad:XamlDebuggerXmlReader.FileName="c:\temp\DeclarativeServiceLibrary2\DeclarativeServiceLibrary2\Service1.xamlx" sap:VirtualizedContainerService.HintSize="277,276" mva:VisualBasic.Settings="Assembly references and imported namespaces serialized as XML namespaces">
    <p1:Sequence.Variables>
      <p1:Variable x:TypeArguments="CorrelationHandle" Name="handle" />
      <p1:Variable x:TypeArguments="x:Int32" Name="data" />
      <p1:Variable x:TypeArguments="x:String" Name="WcfRequestTo" />
    </p1:Sequence.Variables>
    <sap:WorkflowViewStateService.ViewState>
      <scg3:Dictionary x:TypeArguments="x:String, x:Object">
        <x:Boolean x:Key="IsExpanded">True</x:Boolean>
      </scg3:Dictionary>
    </sap:WorkflowViewStateService.ViewState>
    <d:GetWCFMessageTo sap:VirtualizedContainerService.HintSize="255,22" WcfTo="[WcfRequestTo]">
      <d:GetWCFMessageTo.Receive>
        <Receive x:Name="__ReferenceID0" CanCreateInstance="True" DisplayName="ReceiveRequest" sap:VirtualizedContainerService.HintSize="255,90" OperationName="GetData" ServiceContractName="p:IService">
          <Receive.CorrelationInitializers>
            <RequestReplyCorrelationInitializer CorrelationHandle="[handle]" />
          </Receive.CorrelationInitializers>
          <ReceiveMessageContent>
            <p1:OutArgument x:TypeArguments="x:Int32">[data]</p1:OutArgument>
          </ReceiveMessageContent>
        </Receive>
      </d:GetWCFMessageTo.Receive>
    </d:GetWCFMessageTo>
    <SendReply Request="{x:Reference __ReferenceID0}" DisplayName="SendResponse" sap:VirtualizedContainerService.HintSize="255,90">
      <SendMessageContent>
        <p1:InArgument x:TypeArguments="x:String">["Received " &amp; data &amp; " WCF To header: " &amp; WcfRequestTo]</p1:InArgument>
      </SendMessageContent>
    </SendReply>
  </p1:Sequence>
</WorkflowService>
Maurice
i have two question with this approach:1)this approach is suitable only with NativeActivity is right?2)in the example the method OnReceiveMessage not have a return value and i don't understand how i can return the url to the activity.
tartafe
You add an OutArgument to the activity and use that in your workflow. See the example code in the updated answer
Maurice
it works! thanks a lot. In general this approach is more complicated that other used for example in wf 3.5 in MOSS runtime. I hope than as soon as possible will be developed more usable built in activity or simplified the entire framework in general introducing an "Ambient Context" for example for now i hope than people like you help the newbie like me :)
tartafe