30 Mayıs 2011 Pazartesi

WCF Duplex callback ile mesaj almak ve göndermek

<system.serviceModel>

        <protocolMapping>

                <add scheme="http" binding="wsDualHttpBinding" />

        </protocolMapping>

        <services>

                <service name="Plugins.Intranet.Library.SupportService" behaviorConfiguration="SupportServiceBehaviors" >

                        <endpoint contract="Plugins.Intranet.Library.ISupportService" binding="wsDualHttpBinding" />

                </service>

        </services>

        <behaviors>

                <serviceBehaviors>

                        <behavior name="SupportServiceBehaviors" >

                               <serviceMetadata httpGetEnabled="true" />

                               <serviceDebug includeExceptionDetailInFaults="True" />

                        </behavior>

                </serviceBehaviors>

        </behaviors>

</system.serviceModel>

 

WCF Svc file

<%@ServiceHost language="c#" Debug="true" Service="Plugins.Intranet.Library.SupportService" %>

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

 

namespace Plugins.Intranet.Library

{

    // The callback interface is used to send messages from service back to client.

    // The Result operation will return the current result after each operation.

    // The Equation opertion will return the complete equation after Clear() is called.

    public interface ISupportServiceDuplexCallback

    {

        [OperationContract(IsOneWay = true)]

        void NewMessage(SupportMessage msg);

    }

 

    // Define a duplex service contract.

    // A duplex contract consists of two interfaces.

    // The primary interface is used to send messages from the client to the service.

    // The callback interface is used to send messages from the service back to the client.

    // ICalculatorDuplex allows one to perform multiple operations on a running result.

    // The result is sent back after each operation on the ICalculatorCallback interface.

    [ServiceContract(Namespace = "http://intranet.metafor",

    SessionMode = SessionMode.Required,

    CallbackContract = typeof(ISupportServiceDuplexCallback))]

    public interface ISupportService

    {

 

        [OperationContract(IsOneWay = true)]

        void TestService();

        [OperationContract(IsOneWay = true)]

        void GetMessages(string UserID);

 

    }

 

    // Service class which implements a duplex service contract.

    // Use an InstanceContextMode of PerSession to store the result

    // An instance of the service will be bound to each duplex session

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]

    public class SupportService : ISupportService

    {

        public int ServiceStatus { get; set; }

 

        public void TestService()

        {

            ServiceStatus= 11;

        }

 

        public void GetMessages(string UserID)

        {

            // dummy message for test

            var buf = new SupportMessage()

            {

                Title="Test Message Title",

                Description= "Lorem ipsum",

                CreateTime= DateTime.Now

            };

 

            Callback.NewMessage(buf);

        }

 

        ISupportServiceDuplexCallback Callback

        {

            get

            {

                return OperationContext.Current.GetCallbackChannel<ISupportServiceDuplexCallback>();

            }

        }

 

 

    }

 

    public class SupportMessage

    {

        public string Sender { get; set; }

        public string Title { get; set; }

        public string Description { get; set; }

        public int      Category { get; set; }

        public DateTime CreateTime { get; set; }

 

    }

 

 

}

 

http://localhost/metafor/plugins/SupportService.svc

web result

C#

class Test

{

    static void Main()

    {

        SupportServiceClient client = new SupportServiceClient();

 

        // Hizmetteki işlemleri çağırmak için 'client' değişkenini kullanın.

 

        // Her zaman istemciyi kapatın.

        client.Close();

    }

}

 

Visual Basic

Class Test

    Shared Sub Main()

        Dim client As SupportServiceClient = New SupportServiceClient()

        ' Hizmetteki işlemleri çağırmak için 'client' değişkenini kullanın.

 

        ' Her zaman istemciyi kapatın.

        client.Close()

    End Sub

End Class

 

Client code :

Svcutil.exe http://localhost/metafor/plugins/SupportService.svc

Output.config

<system.serviceModel>

        <bindings>

                <wsDualHttpBinding>

                        <binding name="WSDualHttpBinding_ISupportService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">

                               <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>

                               <reliableSession ordered="true" inactivityTimeout="00:10:00"/>

                               <security mode="Message">

                                       <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default"/>

                               </security>

                        </binding>

                </wsDualHttpBinding>

        </bindings>

        <client>

                <endpoint address="http://localhost/metafor/plugins/SupportService.svc" binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_ISupportService" contract="ISupportService" name="WSDualHttpBinding_ISupportService">

                        <identity>

                               <servicePrincipalName value="host/localhost"/>

                        </identity>

                </endpoint>

        </client>

</system.serviceModel>

 

namespace Plugins.Intranet.Library

{

    public class SupportSelfCallback : ISupportServiceCallback

    {

 

        public delegate void MessageReceivedEventHandler(object sender, MessageReceivedEventArgs e);

        public event MessageReceivedEventHandler MessageReceived;

 

        protected void RaiseMessageReceived(object propertyName)

        {

            MessageReceivedEventHandler messageChanged = this.MessageReceived;

            if ((MessageReceived != null))

            {

                MessageReceived(this, new MessageReceivedEventArgs(propertyName));

            }

        }

 

        #region ISupportServiceCallback Members

 

        public void NewMessage(ServiceReferenceSupport.SupportMessage msg)

        {

            RaiseMessageReceived(msg);

        }

 

        #endregion

    }

 

    public class MessageReceivedEventArgs : EventArgs

    {

        public MessageReceivedEventArgs(object message)

        {

            this.MessageField = message;

        }

 

        private object MessageField;

        public virtual object Message { get { return MessageField; } }

    }

 

}

 

End of code

 

    SupportSelfCallback duplexCallBack = new SupportSelfCallback();

    duplexCallBack.MessageReceived += new SupportSelfCallback.MessageReceivedEventHandler(duplexCallBack_MessageReceived);

    // Construct InstanceContext to handle messages on callback interface.

    InstanceContext instanceContext = new InstanceContext(duplexCallBack);

 

    SupportServiceClient client = new SupportServiceClient(instanceContext);

   

    void duplexCallBack_MessageReceived(object sender, MessageReceivedEventArgs e)

    {

        // do stuff

    }