none
How to execute custom code in my WCF service published with "BizTalk WCF Service Publishing Wizard"

    Question

  • Hello,

    I've published an orchestration using "BizTalk WCF Service Publishing Wizard". It only generates a ".svc" file and some ".xml" and ".xsd" files including the schemas.

    The main problem I have is that this wizard doesn't generate any kind of code (.cs) so I can't implement custom actions. I need to execute code just before my wcf service calls the biztalk orchestration. I have read some articles/blogs which pointed to modify machine.config adding "behaviorExtensions" and customize transport type to "WCF-CustomIsolated" in the Receive Location. However, I haven't found any code/config sample which really fits this case.

    Anybody could help me? What exactly I have to add to execute custom cs code before the orchestration starts?

    My server is:

    - Windows 7 Professional SP1
    - Biztalk 2010
    - SQL Server 2008
    - .NET Framework 4

    Thanks in advance.
    Alex

    mercredi 29 février 2012 13:18

Réponses

  • Hi,

    I don't know why, but it finally works...This is the steps and the code if anybody needs it:

    1- C# class:

    public class WCFBehavior : 
            BehaviorExtensionElement,
            IServiceBehavior,
            IEndpointBehavior,
            IDispatchMessageInspector,
            IClientMessageInspector
        {        
            private static readonly object _httpAccessDeined = new object();
            private static readonly object _accessDenied = new object();        
    
            #region Interfaces
    
            #region BehaviorExtensionElement
            public override Type BehaviorType
            {
                get
                {
                    return typeof(WCFBehavior);
                }
            }
    
            protected override object CreateBehavior()
            {
                return new WCFBehavior();
            }
            #endregion
    
            #region IEndpointBehavior Members
    
            public void AddBindingParameters(
                ServiceEndpoint endpoint,
                BindingParameterCollection bindingParameters)
            {
    
            }
    
            public void ApplyClientBehavior(
                ServiceEndpoint endpoint,
                ClientRuntime clientRuntime)
            {
                clientRuntime.MessageInspectors.Add(this);
            }
    
            public void ApplyDispatchBehavior(
                ServiceEndpoint endpoint,
                EndpointDispatcher endpointDispatcher)
            {
                endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
    
            }
    
            public void Validate(
                ServiceEndpoint endpoint)
            {
            }
    
            #endregion
    
            #region IDispatchMessageInspector Members
    
            #region AfterReceiveRequest
            public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
            {
                try
                {
                     code to execute before Biztalk
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return null;
            }
            #endregion
    
            #region BeforeSendReply
            public void BeforeSendReply(ref Message reply, object correlationState)
            {
                
            }
            #endregion
    
            #endregion
    
            #region IServiceBehavior
            void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
            {
    
            }
    
            void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
    
            }
    
            void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
    
            }
            #endregion
    
            #region IClientMessageInspector
            void IClientMessageInspector.AfterReceiveReply(ref Message reply, object correlationState)
            {
            }
    
            object IClientMessageInspector.BeforeSendRequest(ref Message request, IClientChannel channel)
            {
                return null;
            }
            #endregion
    
            #endregion

    2: Machine.config

    <system.serviceModel>
    		<extensions>
    			<behaviorExtensions>
    <add name="WCFBehavior" type="WCFBehavior.WCFBehavior, WCFBehavior, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXXXXXXXXXXXX"/>							

    3: Biztalk: Add the custom extension as "Endpoint Behavior" in your custom isolated receive location.

    Thank you!

    Alex

    jeudi 8 mars 2012 08:28

Toutes les réponses

  • Hi,

    To have some code that has to be executed before the message hits the orchestration you either have a custom pipeline or behaviour. It depends on what you will find best fit for purpose, what is easiest to maintain, what you feel comfortable with (coding a pipeline or behaviour). You will find examples of pipeline development and/or behaviour online.

    HTH

    Steef-Jan Wiggers

    MVP & MCTS BizTalk Server 2010

    http://soa-thoughts.blogspot.com/ | @SteefJan

    If this answers your question please mark it accordingly


    BizTalk

    mercredi 29 février 2012 20:16
    Modérateur
  • Hi Steef,

    Thanks for your response. I am trying to do it using behaviors with my WCF-CustomIsolated Request-Response Receive Location. I have followed pages as:

    - http://btsguru.blogspot.com/2011/09/how-to-throw-typed-fault-exceptions.html

    - http://msdn.microsoft.com/en-us/magazine/cc163302.aspx#S6

    However, my class doesn't appear when I try to add a custom Service Behaviour in WCF-CustomIsolatedTransport Properties. I also have modified the machine.config file.

    Thank you in advance

    Alex

    mercredi 29 février 2012 21:25
  • Hi Alex,

    There is a very nice article Customizing and Extending the BizTalk WCF Adapters , I guess this might give you some more help!

    HTH

    Naushad

    • Proposé comme réponse Naushad.Alam jeudi 8 mars 2012 08:57
    • Non proposé comme réponse Naushad.Alam jeudi 8 mars 2012 10:22
    mercredi 7 mars 2012 14:09
  • Hi,

    I don't know why, but it finally works...This is the steps and the code if anybody needs it:

    1- C# class:

    public class WCFBehavior : 
            BehaviorExtensionElement,
            IServiceBehavior,
            IEndpointBehavior,
            IDispatchMessageInspector,
            IClientMessageInspector
        {        
            private static readonly object _httpAccessDeined = new object();
            private static readonly object _accessDenied = new object();        
    
            #region Interfaces
    
            #region BehaviorExtensionElement
            public override Type BehaviorType
            {
                get
                {
                    return typeof(WCFBehavior);
                }
            }
    
            protected override object CreateBehavior()
            {
                return new WCFBehavior();
            }
            #endregion
    
            #region IEndpointBehavior Members
    
            public void AddBindingParameters(
                ServiceEndpoint endpoint,
                BindingParameterCollection bindingParameters)
            {
    
            }
    
            public void ApplyClientBehavior(
                ServiceEndpoint endpoint,
                ClientRuntime clientRuntime)
            {
                clientRuntime.MessageInspectors.Add(this);
            }
    
            public void ApplyDispatchBehavior(
                ServiceEndpoint endpoint,
                EndpointDispatcher endpointDispatcher)
            {
                endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
    
            }
    
            public void Validate(
                ServiceEndpoint endpoint)
            {
            }
    
            #endregion
    
            #region IDispatchMessageInspector Members
    
            #region AfterReceiveRequest
            public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
            {
                try
                {
                     code to execute before Biztalk
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return null;
            }
            #endregion
    
            #region BeforeSendReply
            public void BeforeSendReply(ref Message reply, object correlationState)
            {
                
            }
            #endregion
    
            #endregion
    
            #region IServiceBehavior
            void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
            {
    
            }
    
            void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
    
            }
    
            void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
    
            }
            #endregion
    
            #region IClientMessageInspector
            void IClientMessageInspector.AfterReceiveReply(ref Message reply, object correlationState)
            {
            }
    
            object IClientMessageInspector.BeforeSendRequest(ref Message request, IClientChannel channel)
            {
                return null;
            }
            #endregion
    
            #endregion

    2: Machine.config

    <system.serviceModel>
    		<extensions>
    			<behaviorExtensions>
    <add name="WCFBehavior" type="WCFBehavior.WCFBehavior, WCFBehavior, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXXXXXXXXXXXX"/>							

    3: Biztalk: Add the custom extension as "Endpoint Behavior" in your custom isolated receive location.

    Thank you!

    Alex

    jeudi 8 mars 2012 08:28