2

I'm hosting WCF service in console application (.NET 4.0). Service code (from msdn example):

using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace WCFServiceHost
{
    [ServiceContract(Namespace = "WCFServiceHost")]
    public interface ICalculator
    {
        [WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        MathResult DoMathJson(double n1, double n2);

        [WebInvoke(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped)]
        MathResult DoMathXml(double n1, double n2);

    }

    public class CalculatorService : ICalculator
    {

        public MathResult DoMathJson(double n1, double n2)
        {
            return DoMath(n1, n2);
        }

        public MathResult DoMathXml(double n1, double n2)
        {
            return DoMath(n1, n2);
        }

        private MathResult DoMath(double n1, double n2)
        {
            MathResult mr = new MathResult();
            mr.sum = n1 + n2;
            mr.difference = n1 - n2;
            mr.product = n1 * n2;
            mr.quotient = n1 / n2;
            return mr;
        }
    }

    [DataContract]
    public class MathResult
    {
        [DataMember]
        public double sum;

        [DataMember]
        public double difference;

        [DataMember]
        public double product;

        [DataMember]
        public double quotient;
    }
}

Next the console app code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace WCFServiceHost
{
    class Program
    {
        public static void Main()
        {
            var adrs = new Uri[1];
            adrs[0] = new Uri("http://localhost:3980");
            using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), adrs))
            {
                try
                {
                    // Open the ServiceHost to start listening for messages.
                    serviceHost.Open();

                    // The service can now be accessed.
                    Console.WriteLine("The service is ready.");
                    Console.WriteLine("Press <ENTER> to terminate service.");
                    Console.ReadLine();

                    // Close the ServiceHost.
                    serviceHost.Close();
                }
                catch (TimeoutException timeProblem)
                {
                    Console.WriteLine(timeProblem.Message);
                    Console.ReadLine();
                }
                catch (CommunicationException commProblem)
                {
                    Console.WriteLine(commProblem.Message);
                    Console.ReadLine();
                }
            }
        }
    }
}

And my 2 questions:

1. When I'm open _http://localhost:3980 I've get: enter image description here How to enable metadata publishing? See Dai Bok answer.

  1. Now how to get data from this service - (to get data from service like in msdn example)? Embed any web framework (like Nancy) or use HttpListener?
amaranth
  • 979
  • 2
  • 22
  • 40

3 Answers3

2

You need to make sure you WCF web config is set up correctly

You will need to enable metat data for http gets, check you web config in the system.serviceModel -> behaviors -> serviceBehaviors -> behavior -> serviceMetadata

and make sure you have:

<serviceMetadata httpGetEnabled="true"/>

For Part 2, you can get the data, you can do something like

   public MathResult GetResult(int a, int b) {
        var status = new MathResult();
        try {
                    var myBinding = new WSHttpBinding();
                    var myEndpoint =
                        new EndpointAddress(
                            new Uri("http://localhost:3980/"));
                    var myChannelFactory = new ChannelFactory<ICalculator>(myBinding, myEndpoint);
                    ICalculator client = myChannelFactory.CreateChannel();
            status = client.DoMathJson(a,b);
        } catch (Exception e) {
            //do something proper here 
        }
        return status;
    }
Dai Bok
  • 3,451
  • 2
  • 53
  • 70
  • Thank you! My first question is closed. In second question I mean that user load html page and use thus service like in http://msdn.microsoft.com/en-us/library/bb472488.aspx. – amaranth Oct 18 '13 at 07:02
  • Do you mean you are using nancy? The example about could be put in a library and used as above. What have you tried to do so far. Do you have some example code? – Dai Bok Oct 18 '13 at 07:37
  • Currently I'm not use Nancy or another framework, I'm want to know may be it possible not use any library for host html with XHR request to WCF service? – amaranth Oct 18 '13 at 08:20
  • In client side I'm use html and pure javascript, no any JS framework))) – amaranth Oct 18 '13 at 08:42
  • I see, you will most probably run into issues making cross domain calls, it is called the same orgin policy. Not sure if you know about this? Basically a html page on http://localhost/calc.html can not talk via javascript to http://localhost:3980/. You can get it to work, but you will need to do alot more work to make cross domain calls - http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy. What have you tried so far? Can you show the code? – Dai Bok Oct 18 '13 at 11:43
  • Well, I want host html page with javascript in my console/winforms application, in that html page shows data from my WCF service. Thank you. – amaranth Oct 18 '13 at 12:25
  • If you just want to render HTml you can make the request in a win form easily. And generate the HTML yourself. Have a button that you press that calls my method example above. You get a math result object back - use that data to generate your custom html – Dai Bok Oct 18 '13 at 13:13
  • Generate HTML on server-side in my console/WinForms application? And how to show this html to user? – amaranth Oct 18 '13 at 18:18
  • If you are using a windows form application, you can user a browser control "System.Windows.Forms.WebBrowser" Drag that from the tool box on your windows form, then set the control document text to your html. eg - webBrowser.DocumentText = "

    hi

    – Dai Bok Oct 23 '13 at 10:52
  • I'm use console/WinForms app as hosting environment. Users must browse html page from this app (embed HTTP Server?) and get data from WCF service by using XHR object in javascript. Ыomething like this... – amaranth Oct 24 '13 at 05:13
0

Your first question is resolved: But it would be better if you will both together. It is used for Metadata Generation:

For Part 2:In order to get data to client, you use HttpWebRequest and invoke the service from client.

Sudhir.net
  • 83
  • 9
0

Now how to get data from this service

That depends on from where you want to get this data. From a webpage rendered on the client, you could use some of the jQuery Ajax functions.

If you want to consume this from the server side, you could use an HttpWebRequest or the likes.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • i.e. I can from any html page (for example, opened from disk locally) by using XHR call my WCF service methods and get data like in http://msdn.microsoft.com/en-us/library/bb472488.aspx? Thanks – amaranth Oct 18 '13 at 09:22
  • No, I don't know from which side start that :( – amaranth Oct 18 '13 at 09:29