2

我在控制台应用程序(.NET 4.0)中托管 WCF 服务。服务代码(来自msdn示例):

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;
    }
}

接下来是控制台应用程序代码:

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();
                }
            }
        }
    }
}

还有我的两个问题:

1.当我打开 _http://localhost:3980 我得到: 在此处输入图像描述 如何启用元数据发布?见戴博回答。

  1. 现在如何从该服务获取数据 - (从msdn示例中的服务获取数据)?嵌入任何 Web 框架(如Nancy)或使用 HttpListener?
4

3 回答 3

2

您需要确保正确设置了 WCF Web 配置

您需要为 http 获取启用元数据,检查您的 web 配置 system.serviceModel -> behavior -> serviceBehaviors -> behavior -> serviceMetadata

并确保您拥有:

<serviceMetadata httpGetEnabled="true"/>

对于第 2 部分,您可以获得数据,您可以执行类似的操作

   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;
    }
于 2013-10-18T06:34:11.940 回答
0

现在如何从该服务获取数据

这取决于您想从哪里获取这些数据。从客户端呈现的网页中,您可以使用一些jQuery Ajax函数。

如果你想从服务器端使用它,你可以使用 anHttpWebRequest或类似的。

于 2013-10-18T09:16:37.513 回答
0

你的第一个问题已经解决了:但如果你们两个在一起会更好。它用于元数据生成:

对于第 2 部分:为了向客户端获取数据,您使用 HttpWebRequest 并从客户端调用服务。

于 2013-10-18T09:10:09.593 回答