Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to access the response value with in the WCF callback handler while communicating with the WCF Service hosted as server from Client Side in asp.net with c# using Visual Studio 2017.

I am trying to communicate client server side over a web socket.

I have hosted the WCF Web Service which is being hosted. I want to communicate with the server from the client side as a web application and get the response to a text-box.

The output is obtained with in the call back handler but it is not accessible to the main method outside it


Code set in the code block below will illustrate it even further

What I have tried:

Server Side:
[ServiceContract(CallbackContract = typeof(IServiceCallback))]
    public interface IService1
    {
        [OperationContract(IsOneWay = true)]
        Task Hello(string messsage);
    }

    public interface IServiceCallback
    {
        [OperationContract(IsOneWay = true)]
        Task OnHello(string message);
    }


<pre>  Task IService1.Hello(string messsage)
        {
            var callback = OperationContext.Current.GetCallbackChannel<IServiceCallback>();
            return callback.OnHello("You Said:" + messsage);


        }


}

Client Side:
protected void btnGetResponse_Click(object sender, EventArgs e)
        {
            var context = new InstanceContext(new WCFServiceCallbackHandler());
            var client = new ServiceReference1.Service1Client(context);

           

            bool exit = false;
            Console.WriteLine("Enter X to exit:");
            Console.WriteLine("Write and enter to send something to server:");

           
            while (!exit)
            {
                Console.WriteLine("Client:");

                String stringToSend = txtMsgToServer.Text;
                if (stringToSend.ToUpper() != "X")
                {
                    client.Hello(stringToSend);
                }
                else { exit = true; }
               
            }

            
           

          
        }
          
    }

    public class WCFServiceCallbackHandler : ServiceReference1.IService1Callback
    {
        public string result { get; set; }

        void ServiceReference1.IService1Callback.OnHello(string message)
        {
            Console.WriteLine(message);
                    result = message;
             
            //HttpContext context = HttpContext.Current;
            //context.Session["message"] = message;

        }
Posted
Comments
F-ES Sitecore 23-May-18 10:30am    
Is the client side code an asp.net page? If so why are you using Console commands? You'll probably find this easier if you don't do things asynchronously with callbacks and instead made the call to the WCF function synchronous. That way you'll get the response in your code-behind page and can output the result to the controls fairly easily.
ranio 24-May-18 0:03am    
Async call is needed as there are multiple threads running in the service already set. Client side was set to asp.net page itself. Console commands used are those used as part of the console application. But I need to use as a web application to set it in the asp.net page code . I am getting the response message but unable to pass that variable outside since the scope of it is limited in the call back handler class.

Client Side:
var context = new InstanceContext(new WCFServiceCallbackHandler());
var client = new ServiceReference1.Service1Client(context);



bool exit = false;
Console.WriteLine("Enter X to exit:");
Console.WriteLine("Write and enter to send something to server:");


while (!exit)
{
Console.WriteLine("Client:");

String stringToSend = txtMsgToServer.Text;
if (stringToSend.ToUpper() != "X")
{
client.Hello(stringToSend);
}
else { exit = true; }

}





}


WCF Call back Handler to invoke WCF Service server side:
public class WCFServiceCallbackHandler : ServiceReference1.IService1Callback
{
public string result { get; set; }

void ServiceReference1.IService1Callback.OnHello(string message)
{
Console.WriteLine(message);
result = message;

//HttpContext context = HttpContext.Current;
//context.Session["message"] = message;

}

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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