Click here to Skip to main content
15,891,473 members

How to use pollingDuplex of WCF in ASP.NET

Fayaz7Wonders asked:

Open original thread
C#

Hi I have a service which is implementing pollingduplex of wcf.I am able to use it in silverlight By giving Binding and Address as it is async call.But I am not able to use it ASP.Net .So can any one help me in consuming it in ASP.net
C#
public partial class UIChat : System.Web.UI.Page, IServiceCallback
    {
        public EndpointAddress servAddress = null;
        public PollingDuplexHttpBinding binding = null;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnJoin_Click(object sender, EventArgs e)
        {
            servAddress = new EndpointAddress("http://localhost:59849/MyService.svc");
           var binding = new PollingDuplexHttpBinding();
            binding.DuplexMode = PollingDuplexMode.MultipleMessagesPerPoll;

                                                        
             InstanceContext objIC = new InstanceContext(this);
               ServiceClient obj = new ServiceClient(objIC,binding,servAddress);
            string data= obj.JoinChat(txtJoin.Text);
            lblUser.Text = data;
        }


        public void ReciveMessage(string message)
        {
            throw new NotImplementedException();
        }

        public void UserJoined(string[] users)
        {
            lblUser.Text = users[0].ToString();
        }

        public void UserLeft(string[] users)
        {
            throw new NotImplementedException();
        }
    }
}






Web.config
XML
<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.serviceModel>
    
    <extensions>
      <bindingExtensions>
        <add name="pollingDuplexHttpBinding"
                              type="System.ServiceModel.Configuration.PollingDuplexHttpBindingCollectionElement,System.ServiceModel.PollingDuplex, Version=4.0.0.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      </bindingExtensions>
    </extensions>
    <services>
      <service name="WebChatService.MyService" behaviorConfiguration="ServBehave">
        <endpoint
           address=""
            binding="pollingDuplexHttpBinding"
            contract="WebChatService.IService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" ></endpoint>
        <!--<endpoint
            address=""
             binding="mexHttpBinding"
             contract="IMetadataExchange"/>-->
      </service>
    </services>
    <!--<bindings>
        <pollingDuplexHttpBinding></pollingDuplexHttpBinding>
          
      </bindings>-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServBehave">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="false"
            />

  </system.serviceModel>
</configuration>


Interface Part
C#
using System.Collections.Generic;
using System.ServiceModel;
using System.Runtime.Serialization;

namespace WebChatService
{
    /// <summary>
    /// Service Interface: Holds signature of methods in service
    /// which are hosted for others to use them.
    /// </summary>
    [ServiceContract(CallbackContract = typeof(IServiceCallback))]
    public interface IService
    {

        [OperationContract]
        string JoinChat(string emailId);

        [OperationContract(IsOneWay = true)]
        void LeaveChat(string emailId);

        [OperationContract(IsOneWay = true)]
        void RemoveUserFromChat(string emailId);

    }

    /// <summary>
    /// Callback Interface: Holds methods which are needed by service 
    /// to call the UI back in case of any event is held in service.
    /// </summary>
    /// 
    
    public interface IServiceCallback
    {
        [OperationContract(IsOneWay = true)]
        void ReciveMessage(string message);

        [OperationContract(IsOneWay = true)]
        void UserJoined(IEnumerable<string> users);

        [OperationContract(IsOneWay = true)]
        void UserLeft(IEnumerable<string> users);
    }
}
Tags: WCF, ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900