2013-10-18 104 views
2

我在控制台应用程序(.NET 4.0)中托管WCF服务。服务代码(从msdn为例):在控制台/ WinForms中通过ServiceHost托管WCF服务

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

我的2个问题:

1. 当我打开_http://本地主机:3980我得到了: enter image description here如何启用元数据发布?请参阅戴博克的答案。

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

回答

1

你需要确保你WCF web配置设置正确

您将需要启用metat数据HTTP获取,检查你的web配置在 system.serviceModel - >行为 - > serviceBehaviors - >性能 - > 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; 
    } 
+0

谢谢!我的第一个问题是关闭的。在第二个问题中,我的意思是用户加载html页面,并使用像http://msdn.microsoft.com/en-us/library/bb472488.aspx中的服务。 – amaranth

+0

你的意思是你在使用南希?这个例子可以放在一个库中,并按照上面的方式使用。你迄今为止做了什么努力。你有一些示例代码? –

+0

目前我不使用南希或其他框架,我想知道可能不使用任何库的主机HTML与XHR请求WCF服务? – amaranth

0

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

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

0

现在如何从这项服务

,从要得到这个数据取决于获取数据。从客户端呈现的网页上,您可以使用jQuery Ajax的一些功能。

如果你想从服务器端消费这个,你可以使用HttpWebRequest或类似的东西。

+0

即我可以从任何HTML页面(例如,从本地磁盘打开)通过使用XHR调用我的WCF服务方法并获取数据,如http://msdn.microsoft.com/en-us/ library/bb472488.aspx?谢谢 – amaranth

+0

也许,你有没有试过? – CodeCaster

+0

不,我不知道从哪一方开始:( – amaranth

相关问题